Avro C#

Public Member Functions | Protected Member Functions | Properties | List of all members
Avro.Generic.DefaultWriter Class Reference

A General purpose writer for serializing objects into a Stream using Avro. This class implements a default way of serializing objects. But one can derive a class from this and override different methods to achieve results that are different from the default implementation. More...

Inheritance diagram for Avro.Generic.DefaultWriter:

## Public Member Functions
| DefaultWriter (Schema schema)
| Constructs a generic writer for the given schema. More...
void | Write< T > (T value, Encoder encoder)
| Examines the Schema and dispatches the actual work to one of the other methods of this class. This allows the derived classes to override specific methods and get custom results. More...
virtual void | Write (Schema schema, object value, Encoder encoder)
| Examines the schema and dispatches the actual work to one of the other methods of this class. This allows the derived classes to override specific methods and get custom results. More...

## Protected Member Functions
virtual void | WriteNull (object value, Encoder encoder)
| Serializes a "null" More...
virtual void | Write< T > (object value, Schema.Type tag, Writer< T > writer)
| A generic method to serialize primitive Avro types. More...
virtual void | WriteRecord (RecordSchema schema, object value, Encoder encoder)
| Serialized a record using the given RecordSchema. It uses GetField method to extract the field value from the given object. More...
virtual void | EnsureRecordObject (RecordSchema s, object value)
| Ensures that the given value is a record and that it corresponds to the given schema. Throws an exception if either of those assertions are false. More...
virtual object | GetField (object value, string fieldName, int fieldPos)
| Extracts the field value from the given object. In this default implementation, value should be of type GenericRecord. More...
virtual void | WriteEnum (EnumSchema es, object value, Encoder encoder)
| Serializes an enumeration. The default implementation expects the value to be string whose value is the name of the enumeration. More...
virtual void | WriteArray (ArraySchema schema, object value, Encoder encoder)
| Serialized an array. The default implementation calls EnsureArrayObject() to ascertain that the given value is an array. It then calls GetArrayLength() and GetArrayElement() to access the members of the array and then serialize them. More...
virtual void | EnsureArrayObject (object value)
| Checks if the given object is an array. If it is a valid array, this function returns normally. Otherwise, it throws an exception. The default implementation checks if the value is an array. More...
virtual long | GetArrayLength (object value)
| Returns the length of an array. The default implementation requires the object to be an array of objects and returns its length. The default implementation guarantees that EnsureArrayObject() has been called on the value before this function is called. More...
virtual object | GetArrayElement (object value, long index)
| Returns the element at the given index from the given array object. The default implementation requires that the value is an object array and returns the element in that array. The default implementation guarantees that EnsureArrayObject() has been called on the value before this function is called. More...
virtual void | WriteMap (MapSchema schema, object value, Encoder encoder)
| Serialized a map. The default implementation first ensure that the value is indeed a map and then uses GetMapSize() and GetMapElements() to access the contents of the map. More...
virtual void | EnsureMapObject (object value)
| Checks if the given object is a map. If it is a valid map, this function returns normally. Otherwise, it throws an exception. The default implementation checks if the value is an IDictionary<string, object>. More...
virtual long | GetMapSize (object value)
| Returns the size of the map object. The default implementation guarantees that EnsureMapObject has been successfully called with the given value. The default implementation requires the value to be an IDictionary<string, object> and returns the number of elements in it. More...
virtual IEnumerable< KeyValuePair< string, object > > | GetMapValues (object value)
| Returns the contents of the given map object. The default implementation guarantees that EnsureMapObject has been called with the given value. The default implementation of this method requires that the value is an IDictionary<string, object> and returns its contents. More...
virtual void | WriteUnion (UnionSchema us, object value, Encoder encoder)
| Resolves the given value against the given UnionSchema and serializes the object against the resolved schema member. The default implementation of this method uses ResolveUnion to find the member schema within the UnionSchema. More...
virtual int | ResolveUnion (UnionSchema us, object obj)
| Finds the branch within the given UnionSchema that matches the given object. The default implementation calls Matches() method in the order of branches within the UnionSchema. If nothing matches, throws an exception. More...
virtual void | WriteLogical (LogicalSchema ls, object value, Encoder encoder)
| Serializes a logical value object by using the underlying logical type to convert the value to its base value. More...
virtual void | WriteFixed (FixedSchema es, object value, Encoder encoder)
| Serialized a fixed object. The default implementation requires that the value is a GenericFixed object with an identical schema as es. More...
AvroException | TypeMismatch (object obj, string schemaType, string type)
| Creates a new AvroException and uses the provided parameters to build an exception message indicating there was a type mismatch. More...
virtual bool | Matches (Schema sc, object obj)
| Tests whether the given schema an object are compatible. More...

## Properties
Schema | Schema`[get]`
| Schema that this object uses to write datum. More...

## Detailed Description

A General purpose writer for serializing objects into a Stream using Avro. This class implements a default way of serializing objects. But one can derive a class from this and override different methods to achieve results that are different from the default implementation.

## Constructor & Destructor Documentation

## ◆ DefaultWriter()

Avro.Generic.DefaultWriter.DefaultWriter | ( | Schema | schema | ) |
| inline

Constructs a generic writer for the given schema.

Parameters

  schema | The schema for the object to be serialized

## Member Function Documentation

## ◆ EnsureArrayObject()

virtual void Avro.Generic.DefaultWriter.EnsureArrayObject | ( | object | value | ) |
| inline protected virtual

Checks if the given object is an array. If it is a valid array, this function returns normally. Otherwise, it throws an exception. The default implementation checks if the value is an array.

Parameters

  value |

## ◆ EnsureMapObject()

virtual void Avro.Generic.DefaultWriter.EnsureMapObject | ( | object | value | ) |
| inline protected virtual

Checks if the given object is a map. If it is a valid map, this function returns normally. Otherwise, it throws an exception. The default implementation checks if the value is an IDictionary<string, object>.

Parameters

  value |

## ◆ EnsureRecordObject()

virtual void Avro.Generic.DefaultWriter.EnsureRecordObject | ( | RecordSchema | s,
|  | object | value
| ) |  |
| inline protected virtual

Ensures that the given value is a record and that it corresponds to the given schema. Throws an exception if either of those assertions are false.

Parameters

  s | Schema associated with the record
  value | Ensure this object is a record

## ◆ GetArrayElement()

virtual object Avro.Generic.DefaultWriter.GetArrayElement | ( | object | value,
|  | long | index
| ) |  |
| inline protected virtual

Returns the element at the given index from the given array object. The default implementation requires that the value is an object array and returns the element in that array. The default implementation guarantees that EnsureArrayObject() has been called on the value before this function is called.

Parameters

  value | The array object
  index | The index to look for

Returns
  The array element at the index

## ◆ GetArrayLength()

virtual long Avro.Generic.DefaultWriter.GetArrayLength | ( | object | value | ) |
| inline protected virtual

Returns the length of an array. The default implementation requires the object to be an array of objects and returns its length. The default implementation guarantees that EnsureArrayObject() has been called on the value before this function is called.

Parameters

  value | The object whose array length is required

Returns
  The array length of the given object

## ◆ GetField()

virtual object Avro.Generic.DefaultWriter.GetField | ( | object | value,
|  | string | fieldName,
|  | int | fieldPos
| ) |  |
| inline protected virtual

Extracts the field value from the given object. In this default implementation, value should be of type GenericRecord.

Parameters

  value | The record value from which the field needs to be extracted
  fieldName | The name of the field in the record
  fieldPos | The position of field in the record

Returns

## ◆ GetMapSize()

virtual long Avro.Generic.DefaultWriter.GetMapSize | ( | object | value | ) |
| inline protected virtual

Returns the size of the map object. The default implementation guarantees that EnsureMapObject has been successfully called with the given value. The default implementation requires the value to be an IDictionary<string, object> and returns the number of elements in it.

Parameters

  value | The map object whose size is desired

Returns
  The size of the given map object

## ◆ GetMapValues()

virtual IEnumerable<KeyValuePair<string, object> > Avro.Generic.DefaultWriter.GetMapValues | ( | object | value | ) |
| inline protected virtual

Returns the contents of the given map object. The default implementation guarantees that EnsureMapObject has been called with the given value. The default implementation of this method requires that the value is an IDictionary<string, object> and returns its contents.

Parameters

  value | The map object whose size is desired

Returns
  The contents of the given map object

## ◆ Matches()

virtual bool Avro.Generic.DefaultWriter.Matches | ( | Schema | sc,
|  | object | obj
| ) |  |
| inline protected virtual

Tests whether the given schema an object are compatible.

FIXME: This method of determining the Union branch has problems. If the data is IDictionary<string, object> if there are two branches one with record schema and the other with map, it choose the first one. Similarly if the data is byte[] and there are fixed and bytes schemas as branches, it choose the first one that matches. Also it does not recognize the arrays of primitive types.

Parameters

  sc | Schema to compare
  obj | Object to compare

Returns
  True if the two parameters are compatible, false otherwise.

Reimplemented in Avro.Specific.SpecificDefaultWriter, and Avro.Reflect.ReflectDefaultWriter.

## ◆ ResolveUnion()

virtual int Avro.Generic.DefaultWriter.ResolveUnion | ( | UnionSchema | us,
|  | object | obj
| ) |  |
| inline protected virtual

Finds the branch within the given UnionSchema that matches the given object. The default implementation calls Matches() method in the order of branches within the UnionSchema. If nothing matches, throws an exception.

Parameters

  us | The UnionSchema to resolve against
  obj | The object that should be used in matching

Returns

## ◆ TypeMismatch()

AvroException Avro.Generic.DefaultWriter.TypeMismatch | ( | object | obj,
|  | string | schemaType,
|  | string | type
| ) |  |
| inline protected

Creates a new AvroException and uses the provided parameters to build an exception message indicating there was a type mismatch.

Parameters

  obj | Object whose type does not the expected type
  schemaType | Schema that we tried to write against
  type | Type that we expected

Returns
  A new AvroException indicating a type mismatch.

## ◆ Write()

virtual void Avro.Generic.DefaultWriter.Write | ( | Schema | schema,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline virtual

Examines the schema and dispatches the actual work to one of the other methods of this class. This allows the derived classes to override specific methods and get custom results.

Parameters

  schema | The schema to use for serializing
  value | The value to be serialized
  encoder | The encoder to use during serialization

## ◆ Write< T >() [1/2]

virtual void Avro.Generic.DefaultWriter.Write< T > | ( | object | value,
|  | Schema.Type | tag,
|  | Writer< T > | writer
| ) |  |
| inline protected virtual

A generic method to serialize primitive Avro types.

Template Parameters

  T | Type of the C# type to be serialized

Parameters

  value | The value to be serialized
  tag | The schema type tag
  writer | The writer which should be used to write the given type.

## ◆ Write< T >() [2/2]

void Avro.Generic.DefaultWriter.Write< T > | ( | T | value,
|  | Encoder | encoder
| ) |  |
| inline

Examines the Schema and dispatches the actual work to one of the other methods of this class. This allows the derived classes to override specific methods and get custom results.

Parameters

  value | The value to be serialized
  encoder | The encoder to use during serialization

## ◆ WriteArray()

virtual void Avro.Generic.DefaultWriter.WriteArray | ( | ArraySchema | schema,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Serialized an array. The default implementation calls EnsureArrayObject() to ascertain that the given value is an array. It then calls GetArrayLength() and GetArrayElement() to access the members of the array and then serialize them.

Parameters

  schema | The ArraySchema for serialization
  value | The value being serialized
  encoder | The encoder for serialization

Reimplemented in Avro.Specific.SpecificDefaultWriter, and Avro.Reflect.ReflectDefaultWriter.

## ◆ WriteEnum()

virtual void Avro.Generic.DefaultWriter.WriteEnum | ( | EnumSchema | es,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Serializes an enumeration. The default implementation expects the value to be string whose value is the name of the enumeration.

Parameters

  es | The EnumSchema for serialization
  value | Value to be written
  encoder | Encoder for serialization

Reimplemented in Avro.Specific.SpecificDefaultWriter.

## ◆ WriteFixed()

virtual void Avro.Generic.DefaultWriter.WriteFixed | ( | FixedSchema | es,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Serialized a fixed object. The default implementation requires that the value is a GenericFixed object with an identical schema as es.

Parameters

  es | The schema for serialization
  value | The value to be serialized
  encoder | The encoder for serialization

Reimplemented in Avro.Reflect.ReflectDefaultWriter, and Avro.Specific.SpecificDefaultWriter.

## ◆ WriteLogical()

virtual void Avro.Generic.DefaultWriter.WriteLogical | ( | LogicalSchema | ls,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Serializes a logical value object by using the underlying logical type to convert the value to its base value.

Parameters

  ls | The schema for serialization
  value | The value to be serialized
  encoder | The encoder for serialization

## ◆ WriteMap()

virtual void Avro.Generic.DefaultWriter.WriteMap | ( | MapSchema | schema,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Serialized a map. The default implementation first ensure that the value is indeed a map and then uses GetMapSize() and GetMapElements() to access the contents of the map.

Parameters

  schema | The MapSchema for serialization
  value | The value to be serialized
  encoder | The encoder for serialization

Reimplemented in Avro.Reflect.ReflectDefaultWriter, and Avro.Specific.SpecificDefaultWriter.

## ◆ WriteNull()

virtual void Avro.Generic.DefaultWriter.WriteNull | ( | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Serializes a "null"

Parameters

  value | The object to be serialized using null schema
  encoder | The encoder to use while serialization

## ◆ WriteRecord()

virtual void Avro.Generic.DefaultWriter.WriteRecord | ( | RecordSchema | schema,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Serialized a record using the given RecordSchema. It uses GetField method to extract the field value from the given object.

Parameters

  schema | The RecordSchema to use for serialization
  value | The value to be serialized
  encoder | The Encoder for serialization

Reimplemented in Avro.Reflect.ReflectDefaultWriter, and Avro.Specific.SpecificDefaultWriter.

## ◆ WriteUnion()

virtual void Avro.Generic.DefaultWriter.WriteUnion | ( | UnionSchema | us,
|  | object | value,
|  | Encoder | encoder
| ) |  |
| inline protected virtual

Resolves the given value against the given UnionSchema and serializes the object against the resolved schema member. The default implementation of this method uses ResolveUnion to find the member schema within the UnionSchema.

Parameters

  us | The UnionSchema to resolve against
  value | The value to be serialized
  encoder | The encoder for serialization

Reimplemented in Avro.Specific.SpecificDefaultWriter.

## Property Documentation

## ◆ Schema

Schema Avro.Generic.DefaultWriter.Schema
| get

Schema that this object uses to write datum.

The documentation for this class was generated from the following file:

- src/apache/main/Generic/GenericWriter.cs