Ejemplo n.º 1
0
 /// <summary>
 /// Adds a schema name to the map if it doesn't exist yet
 /// </summary>
 /// <param name="name">schema name</param>
 /// <param name="schema">schema object</param>
 /// <returns>true if schema was added to the list, false if schema is already in the list</returns>
 public bool Add(SchemaName name, NamedSchema schema)
 {
     if (Names.ContainsKey(name))
     {
         return(false);
     }
     Names.Add(name, schema);
     return(true);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Tries to get the value for the given name fields
        /// </summary>
        /// <param name="name">name of the schema</param>
        /// <param name="space">namespace of the schema</param>
        /// <param name="encspace">enclosing namespace of the schema</param>
        /// <param name="schema">schema object found</param>
        /// <returns>true if name is found in the map, false otherwise</returns>
        public bool TryGetValue(string name, string space, string encspace, out NamedSchema schema)
        {
            var schemaName = new SchemaName(name, space, encspace);

            return(Names.TryGetValue(schemaName, out schema));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds a named schema to the list
 /// </summary>
 /// <param name="schema">schema object</param>
 /// <returns>true if schema was added to the list, false if schema is already in the list</returns>
 public bool Add(NamedSchema schema)
 {
     return(Add(schema.SchemaName, schema));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Static method 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("jtok", "jtok 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 = JsonHelper.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 BaijiTypeException("Invalid JSON for schema: " + jtok);
        }