/// <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));
        }
        public void Gives()
        {
            var target        = new JsonApiSerializer <PersonResource>();
            var initialPerson = Get.Person();

            var personJson = target.Serialize(initialPerson, DefaultUrl);
            var dsPerson   = (Person)target.Deserialize(personJson, typeof(Person));

            Assert.Equal(initialPerson.FirstName, dsPerson.FirstName);
        }
        public void CanDeserializeNullBelongsRelationship()
        {
            // arrange
            var serializer = new JsonApiSerializer(HypermediaSampleClient.CreateResolver());

            // act
            var resource = (PostResource)serializer.Deserialize(JsonContent.GetObject(nameof(CanDeserializeNullBelongsRelationship)));

            // assert
            Assert.Equal(2, resource.Id);
            Assert.Null(resource.OwnerUser);
        }
        public void CanDeserialize()
        {
            // arrange
            var serializer = new JsonApiSerializer(HypermediaSampleClient.CreateResolver());

            // act
            var resource = (PostResource)serializer.Deserialize(JsonContent.GetObject(nameof(CanDeserialize)));

            // assert
            Assert.Equal(2, resource.Id);
            Assert.Equal("Did the Greeks build temples for all of the children of Cronus?", resource.Title);
            Assert.Equal("kuwaly", resource.OwnerUser.DisplayName);
        }
        public void CanDeserializeNullHasManyRelationship()
        {
            // arrange
            var serializer = new JsonApiSerializer(new JsonApiSerializerOptions {
                ContractResolver = HypermediaSampleClient.CreateResolver()
            });

            // act
            var resource = (PostResource)serializer.Deserialize(JsonContent.GetObject(nameof(CanDeserializeNullHasManyRelationship)));

            // assert
            Assert.Equal(2, resource.Id);
            Assert.Null(resource.Comments);
        }
Exemple #6
0
        /// <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, FieldNamingStrategy);

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

            return(serializer.Deserialize(jsonObject));
        }
 /// <summary>
 /// Gets a single entity.
 /// </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 TEntity Get <TEntity>(JsonApiSerializer serializer, IJsonApiEntityCache cache)
 {
     return((TEntity)serializer.Deserialize(_jsonObject, cache));
 }