Ejemplo n.º 1
0
        public object Reconstruct(object o, DeserializeAttribute attr = null)
        {
            object result = null;

            if (o == null)
            {
                result = null;
            }
            else
            {
                Type t = o.GetType();
                if (ReflectionUtil.IsBoxed(o) || o is string)
                {
                    result = o;
                }
                else if (o is IDictionary)
                {
                    result = ReconstructDictionary((IDictionary)o, attr?.JsonItems ?? false);
                }
                else if (o is ICollection || ReflectionUtil.ImplementsGenericInterface(o.GetType(), typeof(ICollection <>)))
                {
                    result = ReconstructCollection(o, attr?.JsonItems ?? false);
                }
                else if (o.GetType().IsGenericType&& o.GetType().GetGenericTypeDefinition() == typeof(EnumWrapper <>))
                {
                    return(o.ToString());
                }
                else
                {
                    result = ReconstructObject(o);
                }
            }

            if (attr != null && result != null && attr.Json)
            {
                return(JsonConvert.SerializeObject(result));
            }
            return(result);
        }
Ejemplo n.º 2
0
        private dynamic Parse(JToken token, Type t, DeserializeAttribute attr = null)
        {
            bool deserialize      = attr?.Json ?? false;
            bool deserializeItems = attr?.JsonItems ?? false;

            if (token.Type == JTokenType.Object)
            {
                if (typeof(IDictionary).IsAssignableFrom(t))
                {
                    return(ParseDictionary((JObject)token, t, deserializeItems));
                }
                return(ParseObject((JObject)token, t));
            }
            else if (token.Type == JTokenType.Array)
            {
                return(ParseArray((JArray)token, t, deserializeItems));
            }
            else if (token.Type == JTokenType.Boolean)
            {
                return(token.Value <bool>());
            }
            else if (token.Type == JTokenType.Integer || token.Type == JTokenType.Float)
            {
                if (t == typeof(byte))
                {
                    return(token.Value <byte>());
                }
                else if (t == typeof(short))
                {
                    return(token.Value <short>());
                }
                else if (t == typeof(ushort))
                {
                    return(token.Value <ushort>());
                }
                else if (t == typeof(int))
                {
                    return(token.Value <int>());
                }
                else if (t == typeof(uint))
                {
                    return(token.Value <uint>());
                }
                else if (t == typeof(long))
                {
                    return(token.Value <long>());
                }
                else if (t == typeof(ulong))
                {
                    return(token.Value <ulong>());
                }
                else if (t == typeof(float))
                {
                    return(token.Value <float>());
                }
                else if (t == typeof(long))
                {
                    return(token.Value <long>());
                }
                else if (t == typeof(decimal))
                {
                    return(token.Value <decimal>());
                }
                else
                {
                    throw new Exception("Unsupported type " + t.FullName);
                }
            }
            else if (token.Type == JTokenType.String || t == typeof(string))
            {
                string s = token.Value <string>();
                if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(EnumWrapper <>))
                {
                    return(Activator.CreateInstance(t, s));
                }
                else if (t == typeof(DateTime))
                {
                    return(DateTime.Parse(s));
                }
                if (!deserialize)
                {
                    return(s);
                }
                return(Parse(JToken.Parse(s, jsonLoadSettings), t));
            }
            else if (token.Type == JTokenType.Null)
            {
                return(null);
            }
            else if (token.Type == JTokenType.Date && t == typeof(DateTime))
            {
                return(token.Value <DateTime>());
            }
            else
            {
                throw new Exception("Invalid token type " + token.Type);
            }
        }