Esempio n. 1
0
        /// <summary>
        /// Attempt to patch the given entity.
        /// </summary>
        /// <param name="entity">The entity to apply the patch to.</param>
        /// <param name="contractResolver">The contract resolver to use.</param>
        /// <returns>true if the entity could be patched, false if not.</returns>
        public bool TryPatch(T entity, IContractResolver contractResolver)
        {
            try
            {
                var jsonObject = _jsonValue["data"] as JsonObject;

                if (TryResolveContact(contractResolver, jsonObject, out var contract) == false)
                {
                    return(false);
                }

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

                serializer.DeserializeEntity(contract, jsonObject, entity);

                return(true);
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch { }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Attempt to patch the given entity.
        /// </summary>
        /// <param name="entity">The entity to apply the patch to.</param>
        /// <param name="contractResolver">The contract resolver to use.</param>
        /// <returns>true if the entity could be patched, false if not.</returns>
        public bool TryPatch(T entity, IContractResolver contractResolver)
        {
            try
            {
                var jsonObject = _jsonValue["data"] as JsonObject;

                var typeAttribute = jsonObject?["type"];
                if (typeAttribute == null)
                {
                    return(false);
                }

                IContract contract;
                if (contractResolver.TryResolve(((JsonString)typeAttribute).Value, out contract) == false)
                {
                    return(false);
                }

                var serializer = new JsonApiSerializer(contractResolver, _fieldNamingStratgey);
                serializer.DeserializeEntity(contract, jsonObject, entity);

                return(true);
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch { }

            return(false);
        }
        /// <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));
        }
Esempio n. 4
0
 /// <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.DeserializeEntity(_jsonObject, cache));
 }
        /// <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);
        }