当前位置: 首页>>代码示例>>C#>>正文


C# JObject.OptionalProperty方法代码示例

本文整理汇总了C#中Newtonsoft.Json.Linq.JObject.OptionalProperty方法的典型用法代码示例。如果您正苦于以下问题:C# JObject.OptionalProperty方法的具体用法?C# JObject.OptionalProperty怎么用?C# JObject.OptionalProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Newtonsoft.Json.Linq.JObject的用法示例。


在下文中一共展示了JObject.OptionalProperty方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetNamespace

 private string GetNamespace(JObject type, NamedSchema parentSchema, string name)
 {
     var nspace = type.OptionalProperty<string>(Token.Namespace);
     if (string.IsNullOrEmpty(nspace) && !name.Contains(".") && parentSchema != null)
     {
         nspace = parentSchema.Namespace;
     }
     return nspace;
 }
开发者ID:sathishkkl,项目名称:hadoopsdk,代码行数:9,代码来源:JsonSchemaBuilder.cs

示例2: ParseRecordType

        /// <summary>
        /// Parses the record type.
        /// </summary>
        /// <param name="record">The record.</param>
        /// <param name="parent">The parent schema.</param>
        /// <param name="namedSchemas">The named schemas.</param>
        /// <returns>
        /// Schema internal representation.
        /// </returns>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when <paramref name="record"/> can not be parsed properly.</exception>
        private TypeSchema ParseRecordType(JObject record, NamedSchema parent, Dictionary<string, NamedSchema> namedSchemas)
        {
            var name = record.RequiredProperty<string>(Token.Name);
            var nspace = this.GetNamespace(record, parent, name);
            var recordName = new SchemaName(name, nspace);

            var doc = record.OptionalProperty<string>(Token.Doc);
            var aliases = this.GetAliases(record, recordName.Namespace);
            var attributes = new NamedEntityAttributes(recordName, aliases, doc);

            Dictionary<string, string> customAttributes = record.GetAttributesNotIn(StandardProperties.Record);
            var result = new RecordSchema(attributes, typeof(AvroRecord), customAttributes);
            namedSchemas.Add(result.FullName, result);

            List<RecordField> fields = record.OptionalArrayProperty(
                Token.Fields,
                (field, index) =>
                {
                    if (field.Type != JTokenType.Object)
                    {
                        throw new SerializationException(
                            string.Format(CultureInfo.InvariantCulture, "Property 'fields' has invalid value '{0}'.", field));
                    }
                    return this.ParseRecordField(field as JObject, result, namedSchemas, index);
                });

            fields.ForEach(result.AddField);
            return result;
        }
开发者ID:sathishkkl,项目名称:hadoopsdk,代码行数:39,代码来源:JsonSchemaBuilder.cs

示例3: ParseRecordField

        /// <summary>
        /// Parses the record field.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="parent">The parent schema.</param>
        /// <param name="namedSchemas">The named schemas.</param>
        /// <param name="position">The position.</param>
        /// <returns>
        /// Schema internal representation.
        /// </returns>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when <paramref name="field"/> is not valid or when sort order is not valid.</exception>
        private RecordField ParseRecordField(JObject field, NamedSchema parent, Dictionary<string, NamedSchema> namedSchemas, int position)
        {
            var name = field.RequiredProperty<string>(Token.Name);
            var doc = field.OptionalProperty<string>(Token.Doc);
            var order = field.OptionalProperty<string>(Token.Order);
            var aliases = this.GetAliases(field, parent.FullName);
            var fieldType = field[Token.Type];
            if (fieldType == null)
            {
                throw new SerializationException(
                    string.Format(CultureInfo.InvariantCulture, "Record field schema '{0}' has no type.", field));
            }

            TypeSchema type = this.Parse(fieldType, parent, namedSchemas);
            object defaultValue = null;
            bool hasDefaultValue = field[Token.Default] != null;
            if (hasDefaultValue)
            {
                var objectParser = new JsonObjectParser();
                defaultValue = objectParser.Parse(type, field[Token.Default].ToString());
            }

            var orderValue = SortOrder.Ascending;
            if (!string.IsNullOrEmpty(order))
            {
                if (!SortValue.ContainsKey(order.ToUpperInvariant()))
                {
                    throw new SerializationException(
                        string.Format(CultureInfo.InvariantCulture, "Invalid sort order of the field '{0}'.", order));
                }
                orderValue = SortValue[order.ToUpperInvariant()];
            }

            var fieldName = new SchemaName(name);
            var attributes = new NamedEntityAttributes(fieldName, aliases, doc);

            return new RecordField(attributes, type, orderValue, hasDefaultValue, defaultValue, null, position);
        }
开发者ID:sathishkkl,项目名称:hadoopsdk,代码行数:49,代码来源:JsonSchemaBuilder.cs

示例4: ParseEnumType

        /// <summary>
        /// Parses a JSON object representing an Avro enumeration to a <see cref="Microsoft.Hadoop.Avro.Schema.EnumSchema"/>.
        /// </summary>
        /// <param name="enumeration">The JSON token that represents the enumeration.</param>
        /// <param name="parent">The parent schema.</param>
        /// <param name="namedSchemas">The named schemas.</param>
        /// <returns>
        /// Instance of <see cref="TypeSchema" /> containing IR of the enumeration.
        /// </returns>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when <paramref name="enumeration"/> contains invalid symbols.</exception>
        private TypeSchema ParseEnumType(JObject enumeration, NamedSchema parent, Dictionary<string, NamedSchema> namedSchemas)
        {
            var name = enumeration.RequiredProperty<string>(Token.Name);
            var nspace = this.GetNamespace(enumeration, parent, name);
            var enumName = new SchemaName(name, nspace);

            var doc = enumeration.OptionalProperty<string>(Token.Doc);
            var aliases = this.GetAliases(enumeration, enumName.Namespace);
            var attributes = new NamedEntityAttributes(enumName, aliases, doc);

            List<string> symbols = enumeration.OptionalArrayProperty(
                Token.Symbols,
                (symbol, index) =>
                {
                    if (symbol.Type != JTokenType.String)
                    {
                        throw new SerializationException(
                            string.Format(CultureInfo.InvariantCulture, "Expected an enum symbol of type string however the type of the symbol is '{0}'.", symbol.Type));
                    }
                    return (string)symbol;
                });

            Dictionary<string, string> customAttributes = enumeration.GetAttributesNotIn(StandardProperties.Enumeration);
            var result = new EnumSchema(attributes, typeof(AvroEnum), customAttributes);
            namedSchemas.Add(result.FullName, result);
            symbols.ForEach(result.AddSymbol);
            return result;
        }
开发者ID:sathishkkl,项目名称:hadoopsdk,代码行数:38,代码来源:JsonSchemaBuilder.cs


注:本文中的Newtonsoft.Json.Linq.JObject.OptionalProperty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。