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


C# JsonObjectContract.MemberReader方法代码示例

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


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

示例1: PopulateObject

        private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, string id)
        {
            OnDeserializing(reader, contract, newObject);

            // only need to keep a track of properies presence if they are required or a value should be defaulted if missing
            Dictionary<JsonProperty, PropertyPresence> propertiesPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate))
                ? contract.Properties.ToDictionary(m => m, m => PropertyPresence.None)
                : null;

            if (id != null)
                AddReference(reader, id, newObject);

            int initialDepth = reader.Depth;

            if (contract.MemberReader != null)
            {
                contract.MemberReader(reader, newObject, GetInternalSerializer());
            }
            else
            {
                bool finished = false;
                do
                {
                    switch (reader.TokenType)
                    {
                        case JsonToken.PropertyName:
                            {
                                string memberName = reader.Value.ToString();

                                if (CheckPropertyName(reader, memberName))
                                    continue;

                                try
                                {
                                    // attempt exact case match first
                                    // then try match ignoring case
                                    JsonProperty property = contract.Properties.GetClosestMatchProperty(memberName);

                                    if (property == null)
                                    {
                                        if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
                                            TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Could not find member '{0}' on {1}".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType)), null);

                                        if (Serializer._missingMemberHandling == MissingMemberHandling.Error)
                                            throw JsonSerializationException.Create(reader, "Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType.Name));

                                        if (!reader.Read())
                                            break;

                                        SetExtensionData(contract, member, reader, memberName, newObject);
                                        continue;
                                    }

                                    if (property.PropertyContract == null)
                                        property.PropertyContract = GetContractSafe(property.PropertyType);

                                    JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, member);

                                    if (!ReadForType(reader, property.PropertyContract, propertyConverter != null))
                                        throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));

                                    SetPropertyPresence(reader, property, propertiesPresence);

                                    // set extension data if property is ignored or readonly
                                    if (!SetPropertyValue(property, propertyConverter, contract, member, reader, newObject))
                                        SetExtensionData(contract, member, reader, memberName, newObject);
                                }
                                catch (Exception ex)
                                {
                                    if (IsErrorHandled(newObject, contract, memberName, reader as IJsonLineInfo, reader.Path, ex))
                                        HandleError(reader, true, initialDepth);
                                    else
                                        throw;
                                }
                                break;
                            }
                        case JsonToken.EndObject:
                            finished = true;
                            break;
                        case JsonToken.Comment:
                            // ignore
                            break;
                        default:
                            throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + reader.TokenType);
                    }
                } while (!finished && reader.Read());

                if (!finished)
                    ThrowUnexpectedEndException(reader, contract, newObject, "Unexpected end when deserializing object.");
            }

            EndObject(newObject, reader, contract, initialDepth, propertiesPresence);

            OnDeserialized(reader, contract, newObject);
            return newObject;
        }
开发者ID:OndrejPetrzilka,项目名称:Newtonsoft.Json,代码行数:96,代码来源:JsonSerializerInternalReader.cs


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