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


C# JsonSerializer.DeserializeValue方法代码示例

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


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

示例1: ReadJsonProperty

        public void ReadJsonProperty(JsonReader reader, JsonSerializer serializer, ModifiableEntity entity, PropertyConverter pc, PropertyRoute parentRoute, bool markedAsModified)
        {
            if (pc.CustomReadJsonProperty != null)
            {
                pc.CustomReadJsonProperty(new ReadJsonPropertyContext
                {
                    JsonReader = reader,
                    JsonSerializer = serializer,
                    Entity = entity,
                    ParentPropertyRoute = parentRoute,
                    PropertyConverter = pc,
                });
            }
            else
            {

                object oldValue = pc.GetValue(entity);

                var pi = pc.PropertyValidator.PropertyInfo;

                var pr = parentRoute.Add(pi);

                using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr))
                {
                    object newValue = serializer.DeserializeValue(reader, pi.PropertyType, oldValue);

                    if (!IsEquals(newValue, oldValue))
                    {
                        if (!markedAsModified && parentRoute.RootType.IsEntity())
                        {
                            try
                            {
                                //Call attention of developer
                                throw new InvalidOperationException($"'modified' is not set but '{pi.Name}' is modified");
                            }
                            catch (Exception)
                            {
                            }

                        }
                        else
                        {
                            AssertCanWrite(pr);
                            if (newValue == null && pc.IsNotNull()) //JSON.Net already complaining
                                return;

                            pc.SetValue?.Invoke(entity, newValue);
                        }
                    }
                }
            }
        }
开发者ID:signumsoftware,项目名称:framework,代码行数:52,代码来源:EntityJsonConverter.cs

示例2: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
                return null;

            using (EntityCache ec = new EntityCache())
            {
                reader.Assert(JsonToken.StartObject);

                bool markedAsModified;
                ModifiableEntity mod = GetEntity(reader, objectType, existingValue, serializer, out markedAsModified);
                
                var pr = JsonSerializerExtensions.CurrentPropertyRoute;
                if (pr == null || mod is IRootEntity)
                    pr = PropertyRoute.Root(mod.GetType());
                else if (pr.Type.ElementType() == objectType)
                    pr = pr.Add("Item"); //Because we have a custom MListJsonConverter but not for other simpler collections

                var dic = PropertyConverter.GetPropertyConverters(mod.GetType());

                while (reader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)reader.Value == "mixins")
                    {
                        var entity = (Entity)mod;
                        reader.Read();
                        reader.Assert(JsonToken.StartObject);

                        reader.Read();
                        while (reader.TokenType == JsonToken.PropertyName)
                        {
                            var mixin = entity[(string)reader.Value];

                            reader.Read();

                            using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr.Add(mixin.GetType())))
                                serializer.DeserializeValue(reader, mixin.GetType(), mixin);

                            reader.Read();
                        }

                        reader.Assert(JsonToken.EndObject);
                        reader.Read();
                    }
                    else
                    {
                        PropertyConverter pc = dic.GetOrThrow((string)reader.Value);

                        reader.Read();
                        ReadJsonProperty(reader, serializer, mod, pc, pr, markedAsModified);

                        reader.Read();
                    }
                }

                reader.Assert(JsonToken.EndObject);

                AfterDeserilization.Invoke(mod);

                return mod;
            }
        }
开发者ID:signumsoftware,项目名称:framework,代码行数:62,代码来源:EntityJsonConverter.cs


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