Beispiel #1
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;
            }

            // FEATURE : enable extensions when you can deserialize anon types
            if (param.EnableAnonymousTypes)
            {
                param.UseExtensions = false; param.UsingGlobalTypes = false;
            }
            return(new JSONSerializer(param).ConvertToJSON(obj));
        }
Beispiel #2
0
        public string ToJSON(object obj, JSONParameters param)
        {
            _params = param;
            _params.FixValues();
            Type t = null;

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

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

            // FEATURE : enable extensions when you can deserialize anon types
            if (_params.EnableAnonymousTypes)
            {
                _params.UseExtensions = false; _params.UsingGlobalTypes = false;
            }
            _usingglobals = _params.UsingGlobalTypes;
            return(new JSONSerializer(_params).ConvertToJSON(obj));
        }
Beispiel #3
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);
        }
Beispiel #4
0
 public string ToJSON(object obj)
 {
     _params = Parameters;
     _params.FixValues();
     Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
     return(ToJSON(obj, Parameters));
 }
Beispiel #5
0
        public object FillObject(object input, string json)
        {
            _params = Parameters;
            _params.FixValues();
            Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
            Dictionary <string, object> ht = new JsonParser(json, Parameters.IgnoreCaseOnDeserialize).Decode() as Dictionary <string, object>;

            if (ht == null)
            {
                return(null);
            }
            return(ParseDictionary(ht, null, input.GetType(), input));
        }
Beispiel #6
0
 public deserializer(JSONParameters param)
 {
     if (param.OverrideObjectHashCodeChecking)
     {
         _circobj = new Dictionary <object, int>(10, ReferenceEqualityComparer.Default);
     }
     else
     {
         _circobj = new Dictionary <object, int>();
     }
     param.FixValues();
     _params = param.MakeCopy();
 }
Beispiel #7
0
        public object ToObject(string json, Type type)
        {
            _params = Parameters;
            _params.FixValues();
            Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
            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 is IDictionary)
            {
                if (type != null && type.IsGenericType && type.GetGenericTypeDefinition() == 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 && type.GetGenericTypeDefinition() == typeof(Dictionary <,>)) // kv format
                {
                    return(RootDictionary(o, type));
                }

                if (type != null && type.GetGenericTypeDefinition() == typeof(List <>)) // deserialize to generic list
                {
                    return(RootList(o, type));
                }
                else
                {
                    return((o as List <object>).ToArray());
                }
            }
            return(o);
        }
Beispiel #8
0
        public static string ToJSON(object obj, JSONParameters param)
        {
            param.FixValues();
            Type genericTypeDefinition = null;

            if (obj == null)
            {
                return("null");
            }
            if (obj.GetType().IsGenericType)
            {
                genericTypeDefinition = Reflection.Instance.GetGenericTypeDefinition(obj.GetType());
            }
            if ((genericTypeDefinition == typeof(Dictionary <,>)) || (genericTypeDefinition == typeof(List <>)))
            {
                param.UsingGlobalTypes = false;
            }
            if (param.EnableAnonymousTypes)
            {
                param.UseExtensions    = false;
                param.UsingGlobalTypes = false;
            }
            return(new JSONSerializer(param).ConvertToJSON(obj));
        }
Beispiel #9
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);
        }
Beispiel #10
0
 public deserializer(JSONParameters param)
 {
     param.FixValues();
     _params = param.MakeCopy();
 }
Beispiel #11
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 !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 != null && 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);
        }
Beispiel #12
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, _params.IgnoreCaseOnDeserialize).Decode();

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

            return(ChangeType1(o, type));


////#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 if(type.IsArray)
//                {
//                    return CreateArray(o as List<object>, type, type.GetElementType(), null);
//                }
//                else
//                    return (o as List<object>).ToArray();
//            }

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

//            return o;
        }