예제 #1
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);
        }
예제 #2
0
        /// <summary>
        /// Create a json representation for an object with parameter override on this call
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string ToJson(object obj, JsonParameters param)
        {
            param.FixValues();
            Type t = null;

            if (obj == null)
            {
                return("null");
            }

            if (obj.GetType().IsGenericType)
            {
                t = Reflection.Instance.GetGenericTypeDefinition(obj.GetType());
            }
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                param.UsingGlobalTypes = false;
            }

            //Debug.Log(obj.GetType().Name);
            return(new JsonSerializer(param).ConvertToJson(obj));
        }