Ejemplo n.º 1
0
        private object DeserializeValue(JsonMember member, JsonNode value)
        {
            if (value.Type == JsonType.Null)
            {
                return(null);
            }

            var type = member.Type;

            if (member.IsStringLike && value.Type != JsonType.String)
            {
                // Take the long path...
                return(DeserializeObject(JsonModelCache.Get(type), (JsonObject)value));
            }
            else if (member.Converter != null)
            {
                return(member.Converter.FromJson(value));
            }
            else if (type.IsArray)
            {
                return(DeserializeArray(type, (JsonArray)value));
            }
            else if (member.IsList)
            {
                return(DeserializeList(type, (JsonArray)value));
            }
            else
            {
                var contract = JsonModelCache.Get(type);

                return(DeserializeObject(contract, (JsonObject)value));
            }
        }
Ejemplo n.º 2
0
        internal T Deseralize <T>(JsonObject json)
            where T : new()
        {
            var contract = JsonModelCache.Get(typeof(T));

            return((T)DeserializeObject(contract, json));
        }
Ejemplo n.º 3
0
        internal IEnumerable <KeyValuePair <string, JsonNode> > ReadProperties(object instance)
        {
            var contract = JsonModelCache.Get(instance.GetType());

            foreach (var member in contract.Members)
            {
                string name = member.Name;

                if (options.PropertyNameTransformer != null)
                {
                    name = options.PropertyNameTransformer.Invoke(name);
                }

                // Skip the field if it's not included
                if ((depth == 1 && !options.IsIncluded(name)))
                {
                    continue;
                }

                var value = member.GetValue(instance);

                if (!member.EmitDefaultValue && (value == null || (member.IsList && ((IList)value).Count == 0) || value.Equals(member.DefaultValue)))
                {
                    continue;
                }
                else if (options.IgnoreNullValues && value == null) // Ignore null values
                {
                    continue;
                }

                // Transform the value if there is one
                if (options.Transformations != null)
                {
                    var transform = options.GetTransformation(name);

                    if (transform != null)
                    {
                        value = transform.Transformer(value);
                    }
                }

                yield return(new KeyValuePair <string, JsonNode>(name, ReadValue(member.TypeDetails, value)));
            }
        }
Ejemplo n.º 4
0
        private object DeserializeValue(Type type, JsonNode value)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (value.Type == JsonType.Null)
            {
                return(null);
            }

            var typeDetails = TypeDetails.Get(type);

            if (typeDetails.JsonConverter != null)
            {
                return(typeDetails.JsonConverter.FromJson(value));
            }
            else if (typeDetails.IsEnum)
            {
                return(Enum.Parse(type, value.ToString(), ignoreCase: true));
            }
            else if (type.IsArray)
            {
                return(DeserializeArray(type, (JsonArray)value));
            }
            else if (typeDetails.IsList)
            {
                return(DeserializeList(type, (JsonArray)value));
            }
            else
            {
                var contract = JsonModelCache.Get(type);

                return(DeserializeObject(contract, (JsonObject)value));
            }
        }