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


C# IJsonSerializer.DeserializeValue方法代码示例

本文整理汇总了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;
        }
开发者ID:cosullivan,项目名称:Hypermedia,代码行数:20,代码来源:ComplexConverter.cs

示例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);
        }
开发者ID:cosullivan,项目名称:Hypermedia,代码行数:13,代码来源:NullableConverter.cs

示例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;
        }
开发者ID:cosullivan,项目名称:Hypermedia,代码行数:20,代码来源:EnumerableConverter.cs

示例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;
        }
开发者ID:cosullivan,项目名称:Hypermedia,代码行数:33,代码来源:EnumerableConverter.cs

示例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));
            }
        }
开发者ID:cosullivan,项目名称:Hypermedia,代码行数:16,代码来源:ContractConverter.cs


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