Avro.Schema.ParseJson C# (CSharp) Method

ParseJson() static private method

static private ParseJson ( JToken j, Names names ) : Schema
j JToken
names Names
return Schema
        internal static Schema ParseJson(JToken j, Names names)
        {
            if (log.IsDebugEnabled) log.DebugFormat("ParseJson(JToken, Names) - j = {0}, names = {1}", j, names);
            if (null == j) throw new ArgumentNullException("j", "j cannot be null.");
            if (log.IsDebugEnabled) log.DebugFormat("ParseJson(JToken, Names) - j.GetType() == {0}", j.GetType());
            
            if (j.Type == JTokenType.String)
            {
                string value = (string)j;

                PrimitiveSchema ps = PrimitiveSchema.GetInstance(value);
                if (null != ps) return ps;

                NamedSchema schema = null;
                if (names.TryGetValue(value, out schema)) return schema;

                throw new SchemaParseException("Undefined name: " + value);
            }
            if (j is JArray) return UnionSchema.NewInstance(j as JArray, names);
            if (j is JObject)
            {
                JObject jo = j as JObject;
                string type = JsonHelper.GetRequiredString(jo, "type");

                Schema schema = PrimitiveSchema.GetInstance(type);
                if (null != schema) return schema;

                if (type.Equals("array")) return ArraySchema.NewInstance(j, names);
                if (type.Equals("map")) return MapSchema.NewInstance(j, names);
                
                schema = NamedSchema.NewInstance(jo, names);
                if (null != schema) return schema;
            }
            throw new AvroTypeException("Invalid JSON for schema: " + j);
        }

Usage Example

示例#1
0
        /// <summary>
        /// Parses the given JSON object to create a Protocol object
        /// </summary>
        /// <param name="jtok">JSON object</param>
        /// <returns>Protocol object</returns>
        private static Protocol Parse(JToken jtok)
        {
            string name  = JsonHelper.GetRequiredString(jtok, "protocol");
            string space = JsonHelper.GetOptionalString(jtok, "namespace");
            string doc   = JsonHelper.GetOptionalString(jtok, "doc");

            var names = new SchemaNames();

            JToken jtypes = jtok["types"];
            var    types  = new List <Schema>();

            if (jtypes is JArray)
            {
                foreach (JToken jtype in jtypes)
                {
                    var schema = Schema.ParseJson(jtype, names, space);
                    types.Add(schema);
                }
            }

            var    messages  = new Dictionary <string, Message>();
            JToken jmessages = jtok["messages"];

            if (null != jmessages)
            {
                foreach (JProperty jmessage in jmessages)
                {
                    var message = Message.Parse(jmessage, names, space);
                    messages.Add(message.Name, message);
                }
            }

            return(new Protocol(name, space, doc, types, messages));
        }
All Usage Examples Of Avro.Schema::ParseJson