/// <summary> /// A generic method to serialize primitive Avro types. /// </summary> /// <typeparam name="S">Type of the C# type to be serialized</typeparam> /// <param name="value">The value to be serialized</param> /// <param name="tag">The schema type tag</param> /// <param name="writer">The writer which should be used to write the given type.</param> protected virtual void Write <S>(object value, Schema.Type tag, Writer <S> writer) { if (!(value is S)) { throw TypeMismatch(value, tag.ToString(), typeof(S).ToString()); } writer((S)value); }
/// <summary> /// A generic method to serialize primitive Avro types. /// </summary> /// <typeparam name="S">Type of the C# type to be serialized</typeparam> /// <param name="value">The value to be serialized</param> /// <param name="tag">The schema type tag</param> /// <param name="writer">The writer which should be used to write the given type.</param> protected void Write <S>(object value, Schema.Type tag, Writer <S> writer) { if (value == null) { value = default(S); } if (!(value is S)) { throw TypeMismatch(value, tag.ToString(), typeof(S).ToString()); } writer((S)value); }
/// <summary> /// Gets the type of the specified type name /// </summary> /// <param name="name">name of the object to get type of</param> /// <param name="schemaType">schema type for the object</param> /// <returns>Type</returns> public Type GetType(string name, Schema.Type schemaType) { Type type = FindType(name, true); if (schemaType == Schema.Type.Map) { type = GenericMapType.MakeGenericType(new[] { typeof(string), type }); } else if (schemaType == Schema.Type.Array) { type = GenericListType.MakeGenericType(new [] { type }); } return(type); }
/// <summary> /// A generic method to serialize primitive Avro types. /// </summary> /// <typeparam name="S">Type of the C# type to be serialized</typeparam> /// <param name="value">The value to be serialized</param> /// <param name="tag">The schema type tag</param> /// <param name="writer">The writer which should be used to write the given type.</param> private static void Write <S>(object value, Schema.Type tag, Writer <S> writer) { if (value == null) { value = default(S); } if (!(value is S)) { throw new AvroTypeMismatchException( $"[{typeof(S)}] required to write against [{tag.ToString()}] schema but found " + value.GetType()); } writer((S)value); }
protected override bool IsReusable(Schema.Type tag) { switch (tag) { case Schema.Type.Double: case Schema.Type.Boolean: case Schema.Type.Int: case Schema.Type.Long: case Schema.Type.Float: case Schema.Type.Bytes: case Schema.Type.String: case Schema.Type.Null: return(false); } return(true); }
/// <summary> /// Creates new instance of the given type /// </summary> /// <param name="name">fully qualified name of the type</param> /// <param name="schemaType">type of schema</param> /// <returns>new object of the given type</returns> public object New(string name, Schema.Type schemaType) { NameCtorKey key = new NameCtorKey(name, schemaType); CtorDelegate ctor; lock (ctors) { if (!ctors.TryGetValue(key, out ctor)) { Type type = GetType(name, schemaType); ctor = GetConstructor(name, schemaType, type); ctors.Add(key, ctor); } } return(ctor()); }
/// <summary> /// Gets the default constructor for the specified type /// </summary> /// <param name="name">name of object for the type</param> /// <param name="schemaType">schema type for the object</param> /// <param name="type">type of the object</param> /// <returns>Default constructor for the type</returns> public CtorDelegate GetConstructor(string name, Schema.Type schemaType, Type type) { //ConstructorInfo ctorInfo = type.GetConstructor(Type.EmptyTypes); //if (ctorInfo == null) // throw new AvroException("Class " + name + " has no default constructor"); //DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + name, typeof(object), null, type, true); //ILGenerator ilGen = dynMethod.GetILGenerator(); //ilGen.Emit(OpCodes.Nop); //ilGen.Emit(OpCodes.Newobj, ctorInfo); //ilGen.Emit(OpCodes.Ret); //return (CtorDelegate)dynMethod.CreateDelegate(ctorType); // https://ayende.com/blog/3167/creating-objects-perf-implications // http://bit.ly/2cbySMk //return () => Activator.CreateInstance(type); return(GetCreator(type)); }
private static ObjectCreator.CtorDelegate GetConstructor(string name, Schema.Type schemaType) { var creator = ObjectCreator.Instance; return(creator.GetConstructor(name, schemaType, creator.GetType(name, schemaType))); }
protected S Read <S>(Schema.Type tag, Schema readerSchema, Reader <S> reader) { return(reader()); }
public NameCtorKey(string value1, Schema.Type value2) : this() { name = value1; type = value2; }
/// <summary> /// Indicates if it's possible to reuse an object of the specified type. Generally /// false for immutable objects like int, long, string, etc but may differ between /// the Specific and Generic implementations. Used to avoid retrieving the existing /// value if it's not reusable. /// </summary> protected virtual bool IsReusable(Schema.Type tag) { return(true); }