本文整理汇总了C#中IJsonSerializer.DeserializeValue方法的典型用法代码示例。如果您正苦于以下问题:C# IJsonSerializer.DeserializeValue方法的具体用法?C# IJsonSerializer.DeserializeValue怎么用?C# IJsonSerializer.DeserializeValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IJsonSerializer
的用法示例。
在下文中一共展示了IJsonSerializer.DeserializeValue方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeserializeObject
/// <summary>
/// Deserialize a JSON object.
/// </summary>
/// <param name="serializer">The serializer to utilize when deserializing nested objects.</param>
/// <param name="type">The type of the object to deserialize to.</param>
/// <param name="jsonObject">The JSON object to deserialize from.</param>
/// <returns>The CLR object that represents the JSON object.</returns>
static object DeserializeObject(IJsonSerializer serializer, Type type, JsonObject jsonObject)
{
var entity = Activator.CreateInstance(type);
foreach (var member in jsonObject.Members)
{
var property = type.GetRuntimeProperty(serializer.FieldNamingStrategy.ResolveName(member.Name));
property?.SetValue(entity, serializer.DeserializeValue(property.PropertyType, member.Value));
}
return entity;
}
示例2: DeserializeValue
/// <summary>
/// Deserialize a JSON value to a defined CLR type.
/// </summary>
/// <param name="serializer">The serializer to utilize when deserializing nested objects.</param>
/// <param name="type">The CLR type to deserialize the JSON value to.</param>
/// <param name="jsonValue">The JSON value to deserialize.</param>
/// <returns>The object that represents the CLR version of the given JSON value.</returns>
public object DeserializeValue(IJsonSerializer serializer, Type type, JsonValue jsonValue)
{
type = Nullable.GetUnderlyingType(type) ?? type;
return serializer.DeserializeValue(type, jsonValue);
}
示例3: DeserializeArray
/// <summary>
/// Deserialize a JSON array into an array.
/// </summary>
/// <param name="serializer">The serializer to utilize when deserializing nested objects.</param>
/// <param name="type">The type of the array.</param>
/// <param name="jsonArray">The JSON array to deserialize from.</param>
/// <returns>The collection that represents the JSON array.</returns>
static Array DeserializeArray(IJsonSerializer serializer, Type type, JsonArray jsonArray)
{
var array = Array.CreateInstance(type.GetElementType(), jsonArray.Count);
for (var i = 0; i < jsonArray.Count; i++)
{
var value = serializer.DeserializeValue(type.GetElementType(), jsonArray[i]);
array.SetValue(value, i);
}
return array;
}
示例4: DeserializeCollection
/// <summary>
/// Deserialize a JSON array into a collection.
/// </summary>
/// <param name="serializer">The serializer to utilize when deserializing nested objects.</param>
/// <param name="type">The type of the collection to create.</param>
/// <param name="jsonArray">The JSON array to deserialize from.</param>
/// <returns>The collection that represents the JSON array.</returns>
static ICollection DeserializeCollection(IJsonSerializer serializer, Type type, JsonArray jsonArray)
{
Type collectionType;
if (TypeHelper.TryGetCollectionType(type, out collectionType) == false)
{
throw new JsonException("Can not deserialize a JSON array to a type that doesnt support ICollection<T>.");
}
var method = collectionType
.GetTypeInfo()
.DeclaredMethods
.FirstOrDefault(m => m.DeclaringType == collectionType && m.Name == "Add");
var elementType = collectionType.GenericTypeArguments[0];
var collection = (ICollection)Activator.CreateInstance(type);
foreach (var jsonValue in jsonArray)
{
var value = serializer.DeserializeValue(elementType, jsonValue);
method.Invoke(collection, new[] { value });
}
return collection;
}
示例5: DeserializeFields
/// <summary>
/// Deserialize the fields for the given instance.
/// </summary>
/// <param name="serializer">The serializer to utilize when deserializing nested objects.</param>
/// <param name="jsonObject">The JSON object to deserialize from.</param>
/// <param name="fields">The list of fields to deserialize.</param>
/// <param name="instance">The instance to deserialize into.</param>
void DeserializeFields(IJsonSerializer serializer, JsonObject jsonObject, IReadOnlyList<IField> fields, object instance)
{
foreach (var member in jsonObject.Members)
{
var field = fields.SingleOrDefault(f => String.Equals(f.Name, member.Name, StringComparison.OrdinalIgnoreCase));
field?.SetValue(instance, serializer.DeserializeValue(field.Accessor.ValueType, member.Value));
}
}