本文整理汇总了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);
}
}
}
}
}
示例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;
}
}