Esempio n. 1
0
 /// <summary>
 /// Compares two union schema objects
 /// </summary>
 /// <param name="obj">union schema object to compare against this schema</param>
 /// <returns>true if objects are equal, false otherwise</returns>
 public override bool Equals(object obj)
 {
     if (obj == this)
     {
         return(true);
     }
     if (obj != null && obj is UnionSchema)
     {
         UnionSchema that = obj as UnionSchema;
         if (that.Count == Count)
         {
             for (int i = 0; i < Count; i++)
             {
                 if (!that[i].Equals(this[i]))
                 {
                     return(false);
                 }
             }
             return(areEqual(that.Props, this.Props));
         }
     }
     return(false);
 }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        private static StringBuilder Build(IDictionary <string, string> env, Schema s, StringBuilder o)
        {
            bool firstTime = true;

            Schema.Type st = s.Tag;
            switch (st)
            {
            case Schema.Type.Union:
                UnionSchema us = s as UnionSchema;
                o.Append('[');
                foreach (Schema b in us.Schemas)
                {
                    if (!firstTime)
                    {
                        o.Append(",");
                    }
                    else
                    {
                        firstTime = false;
                    }
                    Build(env, b, o);
                }
                return(o.Append(']'));

            case Schema.Type.Array:
            case Schema.Type.Map:
                o.Append("{\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append("\"");
                if (st == Schema.Type.Array)
                {
                    ArraySchema arraySchema = s as ArraySchema;
                    Build(env, arraySchema.ItemSchema, o.Append(",\"items\":"));
                }
                else
                {
                    MapSchema mapSchema = s as MapSchema;
                    Build(env, mapSchema.ValueSchema, o.Append(",\"values\":"));
                }
                return(o.Append("}"));

            case Schema.Type.Enumeration:
            case Schema.Type.Fixed:
            case Schema.Type.Record:
                NamedSchema namedSchema = s as NamedSchema;
                var         name        = namedSchema.Fullname;
                if (env.ContainsKey(name))
                {
                    return(o.Append(env[name]));
                }
                var qname = "\"" + name + "\"";
                env.Add(name, qname);
                o.Append("{\"name\":").Append(qname);
                o.Append(",\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append("\"");
                if (st == Schema.Type.Enumeration)
                {
                    EnumSchema enumSchema = s as EnumSchema;
                    o.Append(",\"symbols\":[");
                    foreach (var enumSymbol in enumSchema.Symbols)
                    {
                        if (!firstTime)
                        {
                            o.Append(",");
                        }
                        else
                        {
                            firstTime = false;
                        }
                        o.Append("\"").Append(enumSymbol).Append("\"");
                    }
                    o.Append("]");
                }
                else if (st == Schema.Type.Fixed)
                {
                    FixedSchema fixedSchema = s as FixedSchema;
                    o.Append(",\"size\":").Append(fixedSchema.Size.ToString());
                }
                else      // st == Schema.Type.Record
                {
                    RecordSchema recordSchema = s as RecordSchema;
                    o.Append(",\"fields\":[");
                    foreach (var field in recordSchema.Fields)
                    {
                        if (!firstTime)
                        {
                            o.Append(",");
                        }
                        else
                        {
                            firstTime = false;
                        }
                        o.Append("{\"name\":\"").Append(field.Name).Append("\"");
                        Build(env, field.Schema, o.Append(",\"type\":")).Append("}");
                    }
                    o.Append("]");
                }
                return(o.Append("}"));

            default:        //boolean, bytes, double, float, int, long, null, string
                return(o.Append("\"").Append(s.Name).Append("\""));
            }
        }