19 using System.Collections;
20 using System.Collections.Generic;
38 private readonly ArrayAccess _arrayAccess;
39 private readonly MapAccess _mapAccess;
41 private readonly Dictionary<RecordSchema,WriteItem> _recordWriters =
new Dictionary<RecordSchema,WriteItem>();
45 _writer( datum, encoder );
51 _arrayAccess = arrayAccess;
52 _mapAccess = mapAccess;
53 _writer = ResolveWriter(schema);
63 return (v, e) => Write<bool>( v, schema.
Tag, e.WriteBoolean );
65 return (v, e) => Write<int>( v, schema.
Tag, e.WriteInt );
67 return (v, e) => Write<long>( v, schema.
Tag, e.WriteLong );
69 return (v, e) => Write<float>( v, schema.
Tag, e.WriteFloat );
71 return (v, e) => Write<double>( v, schema.
Tag, e.WriteDouble );
73 return (v, e) => Write<string>( v, schema.
Tag, e.WriteString );
75 return (v, e) => Write<byte[]>( v, schema.
Tag, e.WriteBytes );
90 return (v, e) => error(schema, v);
101 if (value !=
null)
throw TypeMismatch(value,
"null",
"null");
113 if (!(value is S))
throw TypeMismatch(value, tag.ToString(), typeof(S).ToString());
126 if (_recordWriters.TryGetValue(recordSchema, out recordResolver))
128 return recordResolver;
130 var writeSteps =
new RecordFieldWriter[recordSchema.
Fields.Count];
133 _recordWriters.Add(recordSchema, recordResolver);
136 foreach (
Field field
in recordSchema)
138 var record =
new RecordFieldWriter
143 writeSteps[index++] = record;
146 return recordResolver;
185 var itemWriter = ResolveWriter(schema.
ItemSchema);
186 return (d,e) => WriteArray(itemWriter, d, e);
189 private void WriteArray(
WriteItem itemWriter,
object array,
Encoder encoder)
191 _arrayAccess.EnsureArrayObject(array);
192 long l = _arrayAccess.GetArrayLength(array);
193 encoder.WriteArrayStart();
194 encoder.SetItemCount(l);
195 _arrayAccess.WriteArrayValues(array, itemWriter, encoder);
196 encoder.WriteArrayEnd();
201 var itemWriter = ResolveWriter(mapSchema.
ValueSchema);
202 return (v, e) =>
WriteMap(itemWriter, v, e);
214 _mapAccess.EnsureMapObject(value);
215 encoder.WriteMapStart();
216 encoder.SetItemCount(_mapAccess.GetMapSize(value));
217 _mapAccess.WriteMapValues(value, itemWriter, encoder);
218 encoder.WriteMapEnd();
224 var branchSchemas = unionSchema.
Schemas.ToArray();
225 var branchWriters =
new WriteItem[branchSchemas.Length];
227 foreach (var branch
in branchSchemas)
229 branchWriters[branchIndex++] = ResolveWriter(branch);
233 return (v, e) => WriteUnion(unionSchema, branchSchemas, branchWriters, v, e);
243 private void WriteUnion(UnionSchema unionSchema,
Schema[] branchSchemas,
WriteItem[] branchWriters,
object value,
Encoder encoder)
245 int index = ResolveUnion(unionSchema, branchSchemas, value);
246 encoder.WriteUnionIndex(index);
247 branchWriters[index](value, encoder);
260 for (
int i = 0; i < branchSchemas.Length; i++)
264 throw new AvroException(
"Cannot find a match for " + obj.GetType() +
" in " + us);
278 return new AvroException(type +
" required to write against " + schemaType +
" schema but found " + (
null == obj ?
"null" : obj.GetType().ToString()) );
281 private void error(
Schema schema, Object value)
356 if( value ==
null || !( value is IDictionary ) )
throw TypeMismatch( value,
"map",
"IDictionary" );
361 return ( (IDictionary) value ).Count;
366 foreach (DictionaryEntry entry
in ((IDictionary)map))
369 encoder.WriteString(entry.Key.ToString());
370 valueWriter(entry.Value, encoder);
void WriteMapValues(object map, WriteItem valueWriter, Encoder encoder)
Returns the contents of the given map object.
abstract void WriteRecordFields(object record, RecordFieldWriter[] writers, Encoder encoder)
Schema Schema
Field type's schema
Class for fields defined in a record
void Write(T datum, Encoder encoder)
Class for enum type schemas
abstract void EnsureRecordObject(RecordSchema recordSchema, object value)
void WriteArrayValues(object array, WriteItem valueWriter, Encoder encoder)
Returns the element at the given index from the given array object.
Schema ValueSchema
Schema for map values type
Base class for all schema types
IList< Schema > Schemas
List of schemas in the union
long GetArrayLength(object value)
Returns the length of an array.
Type
Enum for schema types
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
WriteItem ResolveArray(ArraySchema schema)
Serialized an array.
void EnsureMapObject(object value)
Checks if the given object is a map.
abstract WriteItem ResolveEnum(EnumSchema es)
Serializes an enumeration.
Class for array type schemas
void WriteNull(object value, Encoder encoder)
Serializes a "null"
abstract void WriteFixed(FixedSchema es, object value, Encoder encoder)
Serialized a fixed object.
void EnsureArrayObject(object value)
Checks if the given object is an array.
abstract void WriteField(object record, string fieldName, int fieldPos, WriteItem writer, Encoder encoder)
Extracts the field value from the given object.
int ResolveUnion(UnionSchema us, Schema[] branchSchemas, object obj)
Finds the branch within the given UnionSchema that matches the given object.
long GetMapSize(object value)
Returns the size of the map object.
abstract bool UnionBranchMatches(Schema sc, object obj)
A general purpose writer of data from avro streams.
long GetMapSize(object value)
Returns the size of the map object.
void Write< S >(object value, Schema.Type tag, Writer< S > writer)
A generic method to serialize primitive Avro types.
Schema ItemSchema
Schema for the array 'type' attribute
PreresolvingDatumWriter(Schema schema, ArrayAccess arrayAccess, MapAccess mapAccess)
Type Tag
Schema type property
List< Field > Fields
List of fields in the record
static AvroException TypeMismatch(object obj, string schemaType, string type)
void WriteMap(WriteItem itemWriter, object value, Encoder encoder)
Serialized a map.
void WriteEnum(object value)
delegate void WriteItem(Object value, Encoder encoder)
void WriteMapValues(object map, WriteItem valueWriter, Encoder encoder)
Returns the contents of the given map object.
void EnsureMapObject(object value)
Checks if the given object is a map.