示例#1
0
        /// <summary>
        /// Deserialize a JSON array into a collection.
        /// </summary>
        /// <param name="deserializer">The deserializer 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(IJsonDeserializer deserializer, Type type, JsonArray jsonArray)
        {
            if (TypeHelper.TryGetCollectionType(type, out var 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 = deserializer.DeserializeValue(elementType, jsonValue);

                method.Invoke(collection, new[] { value });
            }

            return(collection);
        }
示例#2
0
        /// <summary>
        /// Deserialize a JSON array into an array.
        /// </summary>
        /// <param name="deserializer">The deserializer 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(IJsonDeserializer deserializer, Type type, JsonArray jsonArray)
        {
            var array = Array.CreateInstance(type.GetElementType(), jsonArray.Count);

            for (var i = 0; i < jsonArray.Count; i++)
            {
                var value = deserializer.DeserializeValue(type.GetElementType(), jsonArray[i]);

                array.SetValue(value, i);
            }

            return(array);
        }
        /// <summary>
        /// Deserialize the fields for the given instance.
        /// </summary>
        /// <param name="deserializer">The deserializer 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(IJsonDeserializer deserializer, JsonObject jsonObject, IReadOnlyList <IField> fields, object instance)
        {
            foreach (var member in jsonObject.Members)
            {
                var field = fields.SingleOrDefault(f => String.Equals(f.Name, _fieldNamingStrategy.ResolveName(member.Name), StringComparison.OrdinalIgnoreCase));

                if (field != null && ShouldDeserializeField(field))
                {
                    field.SetValue(instance, deserializer.DeserializeValue(field.Accessor.ValueType, member.Value));
                    continue;
                }

                if (instance is IJsonExtension jsonExtension)
                {
                    jsonExtension.Data = jsonExtension.Data ?? new List <JsonMember>();
                    jsonExtension.Data.Add(member);
                }
            }
        }
        /// <summary>
        /// Deserialize a JSON object.
        /// </summary>
        /// <param name="deserializer">The deserializer 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>
        object DeserializeObject(IJsonDeserializer deserializer, Type type, JsonObject jsonObject)
        {
            var entity = Activator.CreateInstance(type);

            foreach (var member in jsonObject.Members)
            {
                var property = type.GetRuntimeProperty(_fieldNamingStrategy.ResolveName(member.Name));

                if (property != null)
                {
                    property.SetValue(entity, deserializer.DeserializeValue(property.PropertyType, member.Value));
                    continue;
                }

                if (entity is IJsonExtension jsonExtension)
                {
                    jsonExtension.Data = jsonExtension.Data ?? new List <JsonMember>();
                    jsonExtension.Data.Add(member);
                }
            }

            return(entity);
        }
        /// <summary>
        /// Deserialize a JSON value to a defined CLR type.
        /// </summary>
        /// <param name="deserializer">The deserializer 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(IJsonDeserializer deserializer, Type type, JsonValue jsonValue)
        {
            type = Nullable.GetUnderlyingType(type) ?? type;

            return(deserializer.DeserializeValue(type, jsonValue));
        }