/// <summary> /// Static function to return a new instance of the named schema /// </summary> /// <param name="jo">JSON object of the named schema</param> /// <param name="names">list of named schemas already read</param> /// <param name="encspace">enclosing namespace of the named schema</param> /// <returns></returns> internal static NamedSchema NewInstance(JObject jo, PropertyMap props, SchemaNames names, string encspace) { string type = JsonHelper.GetRequiredString(jo, "type"); switch (type) { case "fixed": return(FixedSchema.NewInstance(jo, props, names, encspace)); case "enum": return(EnumSchema.NewInstance(jo, props, names, encspace)); case "record": return(RecordSchema.NewInstance(Type.Record, jo, props, names, encspace)); case "error": return(RecordSchema.NewInstance(Type.Error, jo, props, names, encspace)); default: NamedSchema result; if (names.TryGetValue(type, null, encspace, out result)) { return(result); } return(null); } }
/// <summary> /// Static class to return new instance of schema object /// </summary> /// <param name="jtok">JSON object</param> /// <param name="names">list of named schemas already read</param> /// <param name="encspace">enclosing namespace of the schema</param> /// <returns>new Schema object</returns> internal static Schema ParseJson(JToken jtok, SchemaNames names, string encspace) { if (null == jtok) { throw new ArgumentNullException("j", "j cannot be null."); } if (jtok.Type == JTokenType.String) // primitive schema with no 'type' property or primitive or named type of a record field { string value = (string)jtok; PrimitiveSchema ps = PrimitiveSchema.NewInstance(value); if (null != ps) { return(ps); } NamedSchema schema = null; if (names.TryGetValue(value, null, encspace, out schema)) { return(schema); } throw new SchemaParseException("Undefined name: " + value); } if (jtok is JArray) // union schema with no 'type' property or union type for a record field { return(UnionSchema.NewInstance(jtok as JArray, null, names, encspace)); } if (jtok is JObject) // JSON object with open/close parenthesis, it must have a 'type' property { JObject jo = jtok as JObject; JToken jtype = jo["type"]; if (null == jtype) { throw new SchemaParseException("Property type is required"); } var props = Schema.GetProperties(jtok); if (jtype.Type == JTokenType.String) { string type = (string)jtype; if (type.Equals("array")) { return(ArraySchema.NewInstance(jtok, props, names, encspace)); } if (type.Equals("map")) { return(MapSchema.NewInstance(jtok, props, names, encspace)); } Schema schema = PrimitiveSchema.NewInstance((string)type, props); if (null != schema) { return(schema); } return(NamedSchema.NewInstance(jo, props, names, encspace)); } else if (jtype.Type == JTokenType.Array) { return(UnionSchema.NewInstance(jtype as JArray, props, names, encspace)); } } throw new AvroTypeException("Invalid JSON for schema: " + jtok); }