public AvroSerializerFactory() { if (typeof(T) == typeof(byte[])) { _schema = PrimitiveSchema.NewInstance("bytes"); } else if (typeof(T) == typeof(int)) { _schema = PrimitiveSchema.NewInstance("int"); } else if (typeof(T) == typeof(long)) { _schema = PrimitiveSchema.NewInstance("long"); } else if (typeof(T).IsPrimitive || typeof(T) == typeof(string)) { _schema = PrimitiveSchema.NewInstance(typeof(T).Name.ToLowerInvariant()); } else if (typeof(T).IsClass) { var schemaField = typeof(T).GetField("_SCHEMA", BindingFlags.Public | BindingFlags.Static); if (schemaField != null) { _schema = (Schema)typeof(T).GetField("_SCHEMA", BindingFlags.Public | BindingFlags.Static).GetValue(null); } } if (_schema == null) { throw new ApplicationException($"Can't decide AVRO schema for type {typeof(T).FullName}"); } _schemaJson = _schema.ToString(); }
/// <summary> /// Matches a primitive schema to its type name. /// </summary> protected virtual string GetSchemaToken(PrimitiveSchema schema) { switch (schema) { case BooleanSchema _: return(JsonSchemaToken.Boolean); case BytesSchema _: return(JsonSchemaToken.Bytes); case DoubleSchema _: return(JsonSchemaToken.Double); case FloatSchema _: return(JsonSchemaToken.Float); case IntSchema _: return(JsonSchemaToken.Int); case LongSchema _: return(JsonSchemaToken.Long); case NullSchema _: return(JsonSchemaToken.Null); case StringSchema _: return(JsonSchemaToken.String); default: throw new UnsupportedSchemaException(schema, $"Unknown primitive schema {schema.GetType().Name}."); } }
public static object DecodeNull(this PrimitiveSchema schema, JToken value) { if (value.Type == JTokenType.Null) { return(null); } throw new ArgumentException("invalid value type: " + value.GetType().FullName); }
public static string DecodeString(this PrimitiveSchema schema, JToken value) { var jvalue = value as JValue; if (jvalue != null) { var underlying = jvalue.Value; if (underlying is string) { return((string)underlying); } } throw new ArgumentException("invalid value type: " + value.GetType().FullName); }
private Schema.Schema BuildSchema(CodeType type, IDictionary <SchemaName, Schema.Schema> parsedSchemas) { var schemaName = new SchemaName(type.Name, type.CodeNamespace, null); Schema.Schema schema; if (parsedSchemas.TryGetValue(schemaName, out schema)) { return(schema); } var definition = type.Definition; if (definition is Struct) { var record = new RecordSchema(schemaName, null, null, null, null, false); parsedSchemas[schemaName] = schema = record; int pos = 0; foreach (var idlField in ((Struct)definition).Fields) { var actualFieldSchema = BuildSchema(type.IdlNamespace, idlField.Type, parsedSchemas); var fieldSchema = new UnionSchema(new[] { actualFieldSchema, PrimitiveSchema.NewInstance("null") }, null); var field = new Field(fieldSchema, _typeMangler.MangleFieldName(idlField.Name), null, pos++, null, null, Field.SortOrder.Ignore, null); record.AddField(field); } } else if (definition is IntegerEnum) { var fields = ((IntegerEnum)definition).Fields; var symbols = new List <KeyValuePair <string, int?> >(fields.Count); foreach (var symbol in fields) { symbols.Add(new KeyValuePair <string, int?>(_typeMangler.MangleConstantName(symbol.Name), (int?)symbol.ExplicitValue)); } parsedSchemas[schemaName] = schema = new EnumSchema(schemaName, null, null, symbols.ToArray(), null); } if (schema == null) { throw new NotSupportedException("Unsupport code type for schema generation: " + type); } return(schema); }
protected virtual object ResolveMap(MapSchema writerSchema, Schema.Schema readerSchema, IReader d, Type type) { var containingTypes = type.GetGenericArguments(); dynamic result = Activator.CreateInstance(type); Schema.Schema stringSchema = PrimitiveSchema.NewInstance("string"); MapSchema rs = (MapSchema)readerSchema; for (int n = (int)d.ReadMapStart(); n != 0; n = (int)d.ReadMapNext()) { for (int j = 0; j < n; j++) { dynamic key = Resolve(stringSchema, stringSchema, d, containingTypes[0]); dynamic value = Resolve(writerSchema.ValueSchema, rs.ValueSchema, d, containingTypes[1]); result.Add(key, value); } } return(result); }
public static T DecodePrimitive <T>(this PrimitiveSchema schema, JToken value) { var jvalue = value as JValue; if (jvalue != null) { var underlying = jvalue.Value; if (underlying is T) { return((T)underlying); } var castConverter = CastHelper.GetCastConverter <T>(); if (castConverter != null) { return(castConverter(underlying)); } } throw new ArgumentException("invalid value type: " + value.GetType().FullName); }
public static byte[] DecodeBytes( this PrimitiveSchema schema, JToken value) { var jvalue = value as JValue; if (jvalue != null) { var underlying = jvalue.Value; if (underlying is byte[] byteArrayUnderlying) { return(byteArrayUnderlying); } // Strings are unicode, not bytes but Newtonsoft returns the encoded JSON // as a string anyway. if (underlying is string stringUnderlying) { return(stringUnderlying.GetUnicodeBytes()); } } throw new ArgumentException("invalid value type: " + value.GetType().FullName); }
/// <summary> /// Indicates that this attribute will be used to store simple, unstructured data. /// </summary> /// <param name="schema">The type of unstructured data to store</param> public void SetSchema(PrimitiveSchema schema) { this.Schema = schema; }
public SchemaOverrideAttribute(PrimitiveSchema primitive) { this.primitive = primitive; }
private JToken EncodePrimitive(PrimitiveSchema schema) { JToken jtoken = null; switch (schema.Tag) { case Schema.Type.Null: jtoken = new JValue("null"); break; case Schema.Type.Boolean: jtoken = new JValue("boolean"); break; case Schema.Type.Int: jtoken = new JValue("int"); break; case Schema.Type.Long: jtoken = new JValue("long"); break; case Schema.Type.Float: jtoken = new JValue("float"); break; case Schema.Type.Double: jtoken = new JValue("double"); break; case Schema.Type.String: jtoken = new JValue("string"); break; case Schema.Type.Bytes: jtoken = new JValue("bytes"); break; default: throw new ArgumentException("unknown schema tag: " + schema.Tag, nameof(schema)); } var propertyMap = schema.GetPropertyMap(); if ((propertyMap == null) || (propertyMap.Count == 0)) { return(jtoken); } var jobject = new JObject(); jobject.Add(new JProperty("type", jtoken)); foreach (var property in propertyMap) { var propertyKey = property.Key; var propertyValue = property.Value; if ((propertyValue.Length >= 2) && (propertyValue[0] == '"') && (propertyValue[propertyValue.Length - 1] == '"')) { propertyValue = propertyValue.Substring(1, propertyValue.Length - 2); } jobject.Add(new JProperty(property.Key, propertyValue)); } return(jobject); }