Exemplo n.º 1
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
            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);
        }
Exemplo n.º 2
0
        public string ToNiceJSON(object obj, JSONParameters param)
        {
            string s = ToJSON(obj, param);

            return(Beautify(s));
        }