Пример #1
0
        public object ToObject(string json, Type type)
        {
            //_params = Parameters;
            _params.FixValues();
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = Reflection.Instance.GetGenericTypeDefinition(type);
            }
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                _params.UsingGlobalTypes = false;
            }
            _usingglobals = _params.UsingGlobalTypes;

            object o = new JsonParser(json).Decode();

            if (o == null)
            {
                return(null);
            }

            if (o is IDictionary)
            {
                if (type != null && t == typeof(Dictionary <,>)) // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                else // deserialize an object
                {
                    return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
                }
            }
            else if (o is List <object> )
            {
                if (type != null && t == typeof(Dictionary <,>)) // kv format
                {
                    return(RootDictionary(o, type));
                }
                else if (type != null && t == typeof(List <>)) // deserialize to generic list
                {
                    return(RootList(o, type));
                }
                else if (type == typeof(Hashtable))
                {
                    return(RootHashTable((List <object>)o));
                }
                else
                {
                    return((o as List <object>).ToArray());
                }
            }
            else if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }
Пример #2
0
        private object ToObject(string json, Type type = null)
        {
            //_params = Parameters;
            _params.FixValues();
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = Reflection.Instance.GetGenericTypeDefinition(type);
            }
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                _params.UsingGlobalTypes = false;
            }
            _usingglobals = _params.UsingGlobalTypes;

            var o = new JsonParser(json).Decode();

            if (o == null)
            {
                return(null);
            }
            if (o is IDictionary)
            {
                if (type != null && t == typeof(Dictionary <,>)) // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
            }

            var list = o as List <object>;

            if (list != null)
            {
                if (type != null && t == typeof(Dictionary <,>)) // kv format
                {
                    return(RootDictionary(list, type));
                }
                if (type != null && t == typeof(List <>))                // deserialize to generic list
                {
                    //Debug.Log("rootList");
                    return(RootList(list, type));
                }
                if (type != null && type.IsArray)
                {
                    return(RootArray(list, type));
                }
                return(type == typeof(Hashtable) ? RootHashTable(list) : list.ToArray());
            }

            if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }
Пример #3
0
        public object ToObject(string json, Type type)
        {
            this._params.FixValues();
            Type genericTypeDefinition = null;

            if ((type != null) && type.IsGenericType)
            {
                genericTypeDefinition = Reflection.Instance.GetGenericTypeDefinition(type);
            }
            if ((genericTypeDefinition == typeof(Dictionary <,>)) || (genericTypeDefinition == typeof(List <>)))
            {
                this._params.UsingGlobalTypes = false;
            }
            this._usingglobals = this._params.UsingGlobalTypes;
            object parse = new JsonParser(json, this._params.IgnoreCaseOnDeserialize).Decode();

            if (parse == null)
            {
                return(null);
            }
            if (parse is IDictionary)
            {
                if ((type != null) && (genericTypeDefinition == typeof(Dictionary <,>)))
                {
                    return(this.RootDictionary(parse, type));
                }
                return(this.ParseDictionary(parse as Dictionary <string, object>, null, type, null));
            }
            if (parse is List <object> )
            {
                if ((type != null) && (genericTypeDefinition == typeof(Dictionary <,>)))
                {
                    return(this.RootDictionary(parse, type));
                }
                if ((type != null) && (genericTypeDefinition == typeof(List <>)))
                {
                    return(this.RootList(parse, type));
                }
                if (type == typeof(Hashtable))
                {
                    return(this.RootHashTable((List <object>)parse));
                }
                return((parse as List <object>).ToArray());
            }
            if ((type != null) && (parse.GetType() != type))
            {
                return(this.ChangeType(parse, type));
            }
            return(parse);
        }
Пример #4
0
        public object ToObject(string json, Type type)
        {
            _params = Parameters;
            _params.FixValues();
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = type.GetGenericTypeDefinition();
            }
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                _params.UsingGlobalTypes = false;
            }
            _usingglobals = _params.UsingGlobalTypes;

            object o = new JsonParser(json, Parameters.IgnoreCaseOnDeserialize).Decode();

            if (o == null)
            {
                return(null);
            }
#if !SILVERLIGHT && ADONET
            if (type != null && type == typeof(DataSet))
            {
                return(CreateDataset(o as Dictionary <string, object>, null));
            }

            if (type != null && type == typeof(DataTable))
            {
                return(CreateDataTable(o as Dictionary <string, object>, null));
            }
#endif
            if (o is IDictionary)
            {
                if (type != null && t == typeof(Dictionary <,>)) // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                else // deserialize an object
                {
                    return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
                }
            }

            if (o is List <object> )
            {
                if (type != null && t == typeof(Dictionary <,>)) // kv format
                {
                    return(RootDictionary(o, type));
                }

                if (type != null && t == typeof(List <>)) // deserialize to generic list
                {
                    return(RootList(o, type));
                }

                if (type == typeof(Hashtable))
                {
                    return(RootHashTable((List <object>)o));
                }
                else
                {
                    return((o as List <object>).ToArray());
                }
            }

            if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }
Пример #5
0
        public object ToObject(string json, Type type)
        {
            //_params.FixValues();
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = Reflection.Instance.GetGenericTypeDefinition(type);
            }
            _usingglobals = _params.UsingGlobalTypes;
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                _usingglobals = false;
            }

            object o = new JsonParser(json, _params.AllowNonQuotedKeys).Decode();

            if (o == null)
            {
                return(null);
            }
#if !SILVERLIGHT
            if (type != null)
            {
                if (type == typeof(DataSet))
                {
                    return(CreateDataset(o as Dictionary <string, object>, null));
                }
                else if (type == typeof(DataTable))
                {
                    return(CreateDataTable(o as Dictionary <string, object>, null));
                }
            }
#endif
            if (o is IDictionary)
            {
                if (type != null && t == typeof(Dictionary <,>)) // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                else // deserialize an object
                {
                    return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
                }
            }
            else if (o is List <object> )
            {
                if (type != null)
                {
                    if (t == typeof(Dictionary <,>)) // kv format
                    {
                        return(RootDictionary(o, type));
                    }
                    else if (t == typeof(List <>)) // deserialize to generic list
                    {
                        return(RootList(o, type));
                    }
                    else if (type.IsArray)
                    {
                        return(RootArray(o, type));
                    }
                    else if (type == typeof(Hashtable))
                    {
                        return(RootHashTable((List <object>)o));
                    }
                }
                else //if (type == null)
                {
                    List <object> l = (List <object>)o;
                    if (l.Count > 0 && l[0].GetType() == typeof(Dictionary <string, object>))
                    {
                        Dictionary <string, object> globals = new Dictionary <string, object>();
                        List <object> op = new List <object>();
                        // try to get $types
                        foreach (var i in l)
                        {
                            op.Add(ParseDictionary((Dictionary <string, object>)i, globals, null, null));
                        }
                        return(op);
                    }
                    return(l.ToArray());
                }
            }
            else if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }
Пример #6
0
        public object ToObject(string json, Type type)
        {
            object o = new JsonParser(json).Decode();

            if (o == null)
            {
                return(null);
            }

            ReflectionCache c = null;

            if (type != null)
            {
                c = _manager.GetReflectionCache(type);
                var cv = c.Converter;
                if (cv != null)
                {
                    var ji = new JsonItem(String.Empty, o, false);
                    ConvertObject(cv, ji, type);
                    if (ReferenceEquals(ji._Value, o) == false)
                    {
                        return(ji._Value);
                    }
                    if (c.CommonType == ComplexType.Dictionary ||
                        c.CommonType == ComplexType.List)
                    {
                        _usingglobals = false;
                    }
                }
                else if (c.DeserializeMethod != null)
                {
                    return(c.DeserializeMethod(this, o, c));
                }
            }
            else
            {
                _usingglobals = _params.UsingGlobalTypes;
            }

            var d = o as JsonDict;

            if (d != null)
            {
                if (type != null)
                {
#if !SILVERLIGHT
                    if (c.JsonDataType == JsonDataType.DataSet)
                    {
                        return(CreateDataSet(d));
                    }

                    if (c.JsonDataType == JsonDataType.DataTable)
                    {
                        return(CreateDataTable(d));
                    }
#endif
                    if (c.CommonType == ComplexType.Dictionary)                     // deserialize a dictionary
                    {
                        return(RootDictionary(o, c));
                    }
                }
                return(CreateObject(d, c, null));
            }
            var a = o as JsonArray;
            if (a != null)
            {
                if (type != null)
                {
                    if (c.CommonType == ComplexType.Dictionary)                     // k/v format
                    {
                        return(RootDictionary(o, c));
                    }

                    if (c.CommonType == ComplexType.List)                     // deserialize to generic list
                    {
                        return(RootList(o, c));
                    }

                    if (c.JsonDataType == JsonDataType.Hashtable)
                    {
                        return(RootHashTable(a));
                    }

                    if (c.CommonType == ComplexType.Array)
                    {
                        return(CreateArray(a, c));
                    }
                }
                return(a.ToArray());
            }

            if (type != null && o.GetType().Equals(type) == false)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }