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

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

            var o = new BJsonParser(json, _params.UseUTCDateTime, _params.v1_4TypedArray).Decode();

            if (type?.IsEnum == true)
            {
                return(CreateEnum(type, o));
            }
#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 typedarray)
            {
                return(ParseTypedArray(new Dictionary <string, object>(), o));
            }
            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 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);
        }
Пример #2
0
        public object ToObject(byte[] json, Type type)
        {
            _params.FixValues();
            Type t = null;

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

            var o = new BJsonParser(json, _params.UseUTCDateTime).Decode();

#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());
                }
            }

            return(o);
        }