/// <summary>
        /// Deserialize an object.
        /// </summary>
        /// <param name="type">The type of the object to deserialize.</param>
        /// <param name="jsonValue">The JSON value that represents the object to deserialize.</param>
        protected override object DeserializeValue(Type type, JsonValue jsonValue)
        {
            var jsonObject = jsonValue as JsonObject;

            if (jsonObject == null)
            {
                throw new HypermediaWebApiException("The top level JSON value must be an Object.");
            }

            var serializer = new JsonApiSerializer(
                new JsonApiSerializerOptions
            {
                ContractResolver    = ContractResolver,
                FieldNamingStrategy = FieldNamingStrategy
            });

            if (TypeHelper.IsEnumerable(type))
            {
                var collection = TypeHelper.CreateListInstance(type);

                foreach (var item in serializer.DeserializeMany(jsonObject))
                {
                    collection.Add(item);
                }

                return(collection);
            }

            return(serializer.Deserialize(jsonObject));
        }
        /// <summary>
        /// Deserialize an object.
        /// </summary>
        /// <param name="type">The type of the object to deserialize.</param>
        /// <param name="jsonValue">The JSON value that represents the object to deserialize.</param>
        protected override object DeserializeValue(Type type, JsonValue jsonValue)
        {
            var jsonObject = jsonValue as JsonObject;

            if (jsonObject == null)
            {
                throw new HypermediaWebApiException("The top level JSON value must be an Object.");
            }

            var serializer = new JsonApiSerializer(ContractResolver, _fieldNamingStratgey);

            if (TypeHelper.IsEnumerable(type))
            {
                return(serializer.DeserializeMany(jsonObject));
            }

            return(serializer.DeserializeEntity(jsonObject));
        }
Пример #3
0
 /// <summary>
 /// Gets a list of entities.
 /// </summary>
 /// <typeparam name="TEntity">The element type.</typeparam>
 /// <param name="serializer">The JSON API serializer.</param>
 /// <param name="cache">The entity cache to use for resolving existing instances in the object graph.</param>
 /// <returns>The list of JSON API entities.</returns>
 public IEnumerable <TEntity> GetMany <TEntity>(JsonApiSerializer serializer, IJsonApiEntityCache cache)
 {
     return(serializer.DeserializeMany(_jsonObject, cache).OfType <TEntity>().ToList());
 }
        /// <summary>
        /// Deserialize an object.
        /// </summary>
        /// <param name="type">The type of the object to deserialize.</param>
        /// <param name="jsonValue">The JSON value that represents the object to deserialize.</param>
        protected override object DeserializeValue(Type type, JsonValue jsonValue)
        {
            var jsonObject = jsonValue as JsonObject;

            if (jsonObject == null)
            {
                throw new HypermediaWebApiException("The top level JSON value must be an Object.");
            }

            var serializer = new JsonApiSerializer(ContractResolver, _fieldNamingStratgey);

            if (TypeHelper.IsEnumerable(type))
            {
                return serializer.DeserializeMany(jsonObject);
            }

            return serializer.DeserializeEntity(jsonObject);
        }