GetOptionalString() public static method

public static GetOptionalString ( JToken j, string field ) : string
j JToken
field string
return string
Beispiel #1
0
        /// <summary>
        /// Static function to return new instance of EnumSchema
        /// </summary>
        /// <param name="jtok">JSON object for enum schema</param>
        /// <param name="names">list of named schema already parsed in</param>
        /// <param name="encspace">enclosing namespace for the enum schema</param>
        /// <returns>new instance of enum schema</returns>
        internal static EnumSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            SchemaName name    = NamedSchema.GetName(jtok, encspace);
            var        aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);

            JArray jsymbols = jtok["symbols"] as JArray;

            if (null == jsymbols)
            {
                throw new SchemaParseException("Enum has no symbols: " + name);
            }

            List <string>             symbols   = new List <string>();
            IDictionary <string, int> symbolMap = new Dictionary <string, int>();
            int i = 0;

            foreach (JValue jsymbol in jsymbols)
            {
                string s = (string)jsymbol.Value;
                if (symbolMap.ContainsKey(s))
                {
                    throw new SchemaParseException("Duplicate symbol: " + s);
                }

                symbolMap[s] = i++;
                symbols.Add(s);
            }
            return(new EnumSchema(name, aliases, symbols, symbolMap, props, names,
                                  JsonHelper.GetOptionalString(jtok, "doc")));
        }
Beispiel #2
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));
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new field for the record
        /// </summary>
        /// <param name="jfield">JSON object for the field</param>
        /// <param name="pos">position number of the field</param>
        /// <param name="names">list of named schemas already read</param>
        /// <param name="encspace">enclosing namespace of the records schema</param>
        /// <returns>new Field object</returns>
        private static Field createField(JToken jfield, int pos, SchemaNames names, string encspace)
        {
            var name = JsonHelper.GetRequiredString(jfield, "name");
            var doc  = JsonHelper.GetOptionalString(jfield, "doc");

            var jorder = JsonHelper.GetOptionalString(jfield, "order");

            Field.SortOrder sortorder = Field.SortOrder.ignore;
            if (null != jorder)
            {
                sortorder = (Field.SortOrder)Enum.Parse(typeof(Field.SortOrder), jorder);
            }

            var aliases      = Field.GetAliases(jfield);
            var props        = Schema.GetProperties(jfield);
            var defaultValue = jfield["default"];

            JToken jtype = jfield["type"];

            if (null == jtype)
            {
                throw new SchemaParseException($"'type' was not found for field: name at '{jfield.Path}'");
            }
            var schema = Schema.ParseJson(jtype, names, encspace);

            return(new Field(schema, name, aliases, pos, doc, defaultValue, sortorder, props));
        }
        /// <summary>
        /// Parses the messages section of a protocol definition
        /// </summary>
        /// <param name="jmessage">messages JSON object</param>
        /// <param name="names">list of parsed names</param>
        /// <param name="encspace">enclosing namespace</param>
        /// <returns></returns>
        internal static Message Parse(JProperty jmessage, SchemaNames names, string encspace)
        {
            string name   = jmessage.Name;
            string doc    = JsonHelper.GetOptionalString(jmessage.Value, "doc");
            bool?  oneway = JsonHelper.GetOptionalBoolean(jmessage.Value, "one-way");

            PropertyMap  props  = Schema.GetProperties(jmessage.Value);
            RecordSchema schema = RecordSchema.NewInstance(Schema.Type.Record, jmessage.Value as JObject, props, names, encspace);

            JToken jresponse = jmessage.Value["response"];
            var    response  = Schema.ParseJson(jresponse, names, encspace);

            JToken      jerrors      = jmessage.Value["errors"];
            UnionSchema uerrorSchema = null;

            if (null != jerrors)
            {
                Schema errorSchema = Schema.ParseJson(jerrors, names, encspace);
                if (!(errorSchema is UnionSchema))
                {
                    throw new AvroException("");
                }

                uerrorSchema = errorSchema as UnionSchema;
            }

            return(new Message(name, doc, schema, response, uerrorSchema, oneway));
        }
Beispiel #5
0
        /// <summary>
        /// Parses the name and namespace from the given JSON schema object then creates
        /// SchemaName object including the given enclosing namespace
        /// </summary>
        /// <param name="jtok">JSON object to read</param>
        /// <param name="encspace">enclosing namespace</param>
        /// <returns>new SchemaName object</returns>
        protected static SchemaName GetName(JToken jtok, string encspace)
        {
            String n  = JsonHelper.GetOptionalString(jtok, "name");     // Changed this to optional string for anonymous records in messages
            String ns = JsonHelper.GetOptionalString(jtok, "namespace");

            return(new SchemaName(n, ns, encspace));
        }
Beispiel #6
0
        /// <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="props">dictionary that provides access to custom properties</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");
            string doc  = JsonHelper.GetOptionalString(jo, "doc");

            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, doc, out result))
                {
                    return(result);
                }
                return(null);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Static function to return new instance of the fixed schema class
        /// </summary>
        /// <param name="jtok">JSON object for the fixed schema</param>
        /// <param name="names">list of named schema already parsed in</param>
        /// <param name="encspace">enclosing namespace of the fixed schema</param>
        /// <returns></returns>
        internal static FixedSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            SchemaName name    = NamedSchema.GetName(jtok, encspace);
            var        aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);

            return(new FixedSchema(name, aliases, JsonHelper.GetRequiredInteger(jtok, "size"), props, names,
                                   JsonHelper.GetOptionalString(jtok, "doc")));
        }
Beispiel #8
0
        /// <summary>
        /// Static function to return new instance of the record schema
        /// </summary>
        /// <param name="type">type of record schema, either record or error</param>
        /// <param name="jtok">JSON object for the record schema</param>
        /// <param name="props">dictionary that provides access to custom properties</param>
        /// <param name="names">list of named schema already read</param>
        /// <param name="encspace">enclosing namespace of the records schema</param>
        /// <returns>new RecordSchema object</returns>
        internal static RecordSchema NewInstance(Type type, JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            bool   request = false;
            JToken jfields = jtok["fields"];    // normal record

            if (null == jfields)
            {
                jfields = jtok["request"];      // anonymous record from messages
                if (null != jfields)
                {
                    request = true;
                }
            }
            if (null == jfields)
            {
                throw new SchemaParseException("'fields' cannot be null for record");
            }
            if (jfields.Type != JTokenType.Array)
            {
                throw new SchemaParseException("'fields' not an array for record");
            }

            var name          = GetName(jtok, encspace);
            var aliases       = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);
            var fields        = new List <Field>();
            var fieldMap      = new Dictionary <string, Field>();
            var fieldAliasMap = new Dictionary <string, Field>();
            var result        = new RecordSchema(type, name, aliases, props, fields, request, fieldMap, fieldAliasMap, names,
                                                 JsonHelper.GetOptionalString(jtok, "doc"));

            int fieldPos = 0;

            foreach (JObject jfield in jfields)
            {
                string fieldName = JsonHelper.GetRequiredString(jfield, "name");
                Field  field     = createField(jfield, fieldPos++, names, name.Namespace); // add record namespace for field look up
                fields.Add(field);
                addToFieldMap(fieldMap, fieldName, field);
                addToFieldMap(fieldAliasMap, fieldName, field);

                if (null != field.aliases)    // add aliases to field lookup map so reader function will find it when writer field name appears only as an alias on the reader field
                {
                    foreach (string alias in field.aliases)
                    {
                        addToFieldMap(fieldAliasMap, alias, field);
                    }
                }
            }
            return(result);
        }
Beispiel #9
0
        /// <summary>
        /// Static function to return new instance of the fixed schema class
        /// </summary>
        /// <param name="jtok">JSON object for the fixed schema</param>
        /// <param name="props">dictionary that provides access to custom properties</param>
        /// <param name="names">list of named schema already parsed in</param>
        /// <param name="encspace">enclosing namespace of the fixed schema</param>
        /// <returns></returns>
        internal static FixedSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace)
        {
            SchemaName name    = NamedSchema.GetName(jtok, encspace);
            var        aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);

            try
            {
                return(new FixedSchema(name, aliases, JsonHelper.GetRequiredInteger(jtok, "size"), props, names,
                                       JsonHelper.GetOptionalString(jtok, "doc")));
            }
            catch (Exception e)
            {
                throw new SchemaParseException($"{e.Message} at '{jtok.Path}'", e);
            }
        }