コード例 #1
0
ファイル: JSON.cs プロジェクト: xiaopohou/Magicodes.Sms
        /// <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;
            }

            // FEATURE : enable extensions when you can deserialize anon types
            if (param.EnableAnonymousTypes)
            {
                param.UseExtensions = false; param.UsingGlobalTypes = false;
            }
            return(new JSONSerializer(param).ConvertToJSON(obj));
        }
コード例 #2
0
ファイル: JSON.cs プロジェクト: xiaopohou/Magicodes.Sms
        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 !SILVERLIGHT
            if (type != null && type == typeof(DataSet))
            {
                return(CreateDataset(o as Dictionary <string, object>, null));
            }
            else 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));
                }
            }
            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);
        }