public static void DisableExtensions()
        {
            var p = new fastJSON.JSONParameters {
                UseExtensions = false, SerializeNullValues = false
            };
            var s = fastJSON.JSON.Instance.ToJSON(new Retclass {
                date = DateTime.Now, Name = "aaaaaaa"
            }, p);

            Console.WriteLine(fastJSON.JSON.Instance.Beautify(s));
            var o = fastJSON.JSON.Instance.ToObject <Retclass>(s);

            Assert.AreEqual("aaaaaaa", o.Name);
        }
Exemple #2
0
    public static bool saveGame(string name)
    {
        FileStream   stream = File.Open(Directory.GetCurrentDirectory() + Engine.MAINDIRECTORY + FOLDER + name + EXTENSION, FileMode.Create);
        StreamWriter writer = new StreamWriter(stream);

        fastJSON.JSONParameters pars = new fastJSON.JSONParameters();
        pars.SerializerMaxDepth = 20;
        Program.engine.save();
        string s = fastJSON.JSON.ToJSON(Program.engine, pars);

        writer.Write(fastJSON.JSON.Beautify(s));
        writer.Close();

        stream.Close();
        return(true);
    }
 internal JsonSerializer(JSONParameters param, SerializationManager manager)
 {
     _manager = manager;
     _params = param;
     _useEscapedUnicode = _params.UseEscapedUnicode;
     _maxDepth = _params.SerializerMaxDepth;
     _naming = _params.NamingStrategy;
     if (_params.EnableAnonymousTypes) {
         _useExtensions = _useGlobalTypes = false;
         _showReadOnlyFields = _showReadOnlyProperties = true;
     }
     else {
         _useExtensions = _params.UseExtensions;
         _useGlobalTypes = _params.UsingGlobalTypes && _useExtensions;
         _showReadOnlyProperties = _params.ShowReadOnlyProperties;
         _showReadOnlyFields = _params.ShowReadOnlyFields;
     }
 }
Exemple #4
0
 /// <summary>
 /// Create a typed generic object from the json with parameter override on this call
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="json"></param>
 /// <param name="param"></param>
 /// <returns></returns>
 public static T ToObject <T>(string json, JSONParameters param)
 {
     return(new deserializer(param).ToObject <T>(json));
 }
Exemple #5
0
        /// <summary>
        /// Create a formatted json string (beautified) from an object
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string ToNiceJSON(object obj, JSONParameters param)
        {
            string s = ToJSON(obj, param);

            return(Beautify(s));
        }
Exemple #6
0
 public deserializer(JSONParameters param)
 {
     param.FixValues();
     _params = param.MakeCopy();
 }
Exemple #7
0
 /// <summary>
 /// Create an object of type from the json with parameter override on this call
 /// </summary>
 /// <param name="json"></param>
 /// <param name="type"></param>
 /// <param name="par"></param>
 /// <returns></returns>
 public static object ToObject(string json, Type type, JSONParameters par)
 {
     return(new deserializer(par).ToObject(json, type));
 }
Exemple #8
0
 /// <summary>
 /// Creates an object from the JSON with parameter override on this call.
 /// </summary>
 /// <param name="json">The JSON string to be deserialized.</param>
 /// <param name="type">The type of the expected object after deserialization.</param>
 /// <param name="param">The <see cref="JSONParameters"/> to control deserialization.</param>
 /// <param name="manager">The <see cref="SerializationManager"/> to control advanced JSON deserialization.</param>
 /// <returns>The deserialized object.</returns>
 public static object ToObject(string json, Type type, JSONParameters param, SerializationManager manager)
 {
     return(new JsonDeserializer(param, manager).ToObject(json, type));
 }
Exemple #9
0
 internal JSONSerializer(JSONParameters param)
 {
     _params = param;
     _useEscapedUnicode = _params.UseEscapedUnicode;
 }
Exemple #10
0
 /// <summary>
 /// Creates a JSON representation for an object with parameter override on this call
 /// </summary>
 /// <param name="obj">The object to be serialized.</param>
 /// <param name="param">The <see cref="JSONParameters"/> to control serialization.</param>
 /// <returns>The serialized JSON string.</returns>
 public static string ToJSON(object obj, JSONParameters param)
 {
     return(ToJSON(obj, param, Manager));
 }
Exemple #11
0
 public object Parse(string json)
 {
     _params = Parameters;
     Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
     return(new JsonParser(json, _params.IgnoreCaseOnDeserialize).Decode());
 }
Exemple #12
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);
        }
Exemple #13
0
        //internal static GenericSetter CreateSetField(Type type, FieldInfo fieldInfo)
        //{
        //    Type[] arguments = new Type[2];
        //    arguments[0] = arguments[1] = typeof(object);

        //    DynamicMethod dynamicSet = new DynamicMethod("_", typeof(object), arguments, type);

        //    ILGenerator il = dynamicSet.GetILGenerator();

        //    if (!type.IsClass) // structs
        //    {
        //        var lv = il.DeclareLocal(type);
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Unbox_Any, type);
        //        il.Emit(OpCodes.Stloc_0);
        //        il.Emit(OpCodes.Ldloca_S, lv);
        //        il.Emit(OpCodes.Ldarg_1);
        //        if (fieldInfo.FieldType.IsClass)
        //            il.Emit(OpCodes.Castclass, fieldInfo.FieldType);
        //        else
        //            il.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType);
        //        il.Emit(OpCodes.Stfld, fieldInfo);
        //        il.Emit(OpCodes.Ldloc_0);
        //        il.Emit(OpCodes.Box, type);
        //        il.Emit(OpCodes.Ret);
        //    }
        //    else
        //    {
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Ldarg_1);
        //        if (fieldInfo.FieldType.IsValueType)
        //            il.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType);
        //        il.Emit(OpCodes.Stfld, fieldInfo);
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Ret);
        //    }
        //    return (GenericSetter)dynamicSet.CreateDelegate(typeof(GenericSetter));
        //}

        //internal static GenericSetter CreateSetMethod(Type type, PropertyInfo propertyInfo)
        //{
        //    MethodInfo setMethod = propertyInfo.GetSetMethod();
        //    if (setMethod == null)
        //        return null;

        //    Type[] arguments = new Type[2];
        //    arguments[0] = arguments[1] = typeof(object);

        //    DynamicMethod setter = new DynamicMethod("_", typeof(object), arguments);
        //    ILGenerator il = setter.GetILGenerator();

        //    if (!type.IsClass) // structs
        //    {
        //        var lv = il.DeclareLocal(type);
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Unbox_Any, type);
        //        il.Emit(OpCodes.Stloc_0);
        //        il.Emit(OpCodes.Ldloca_S, lv);
        //        il.Emit(OpCodes.Ldarg_1);
        //        if (propertyInfo.PropertyType.IsClass)
        //            il.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
        //        else
        //            il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
        //        il.EmitCall(OpCodes.Call, setMethod, null);
        //        il.Emit(OpCodes.Ldloc_0);
        //        il.Emit(OpCodes.Box, type);
        //    }
        //    else
        //    {
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
        //        il.Emit(OpCodes.Ldarg_1);
        //        if (propertyInfo.PropertyType.IsClass)
        //            il.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
        //        else
        //            il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
        //        il.EmitCall(OpCodes.Callvirt, setMethod, null);
        //        il.Emit(OpCodes.Ldarg_0);
        //    }

        //    il.Emit(OpCodes.Ret);

        //    return (GenericSetter)setter.CreateDelegate(typeof(GenericSetter));
        //}

        //internal static GenericGetter CreateGetField(Type type, FieldInfo fieldInfo)
        //{
        //    DynamicMethod dynamicGet = new DynamicMethod("_", typeof(object), new Type[] { typeof(object) }, type);

        //    ILGenerator il = dynamicGet.GetILGenerator();

        //    if (!type.IsClass) // structs
        //    {
        //        var lv = il.DeclareLocal(type);
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Unbox_Any, type);
        //        il.Emit(OpCodes.Stloc_0);
        //        il.Emit(OpCodes.Ldloca_S, lv);
        //        il.Emit(OpCodes.Ldfld, fieldInfo);
        //        if (fieldInfo.FieldType.IsValueType)
        //            il.Emit(OpCodes.Box, fieldInfo.FieldType);
        //    }
        //    else
        //    {
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Ldfld, fieldInfo);
        //        if (fieldInfo.FieldType.IsValueType)
        //            il.Emit(OpCodes.Box, fieldInfo.FieldType);
        //    }

        //    il.Emit(OpCodes.Ret);

        //    return (GenericGetter)dynamicGet.CreateDelegate(typeof(GenericGetter));
        //}

        //internal static GenericGetter CreateGetMethod(Type type, PropertyInfo propertyInfo)
        //{
        //    MethodInfo getMethod = propertyInfo.GetGetMethod();
        //    if (getMethod == null)
        //        return null;

        //    DynamicMethod getter = new DynamicMethod("_", typeof(object), new Type[] { typeof(object) }, type);

        //    ILGenerator il = getter.GetILGenerator();

        //    if (!type.IsClass) // structs
        //    {
        //        var lv = il.DeclareLocal(type);
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Unbox_Any, type);
        //        il.Emit(OpCodes.Stloc_0);
        //        il.Emit(OpCodes.Ldloca_S, lv);
        //        il.EmitCall(OpCodes.Call, getMethod, null);
        //        if (propertyInfo.PropertyType.IsValueType)
        //            il.Emit(OpCodes.Box, propertyInfo.PropertyType);
        //    }
        //    else
        //    {
        //        il.Emit(OpCodes.Ldarg_0);
        //        il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
        //        il.EmitCall(OpCodes.Callvirt, getMethod, null);
        //        if (propertyInfo.PropertyType.IsValueType)
        //            il.Emit(OpCodes.Box, propertyInfo.PropertyType);
        //    }

        //    il.Emit(OpCodes.Ret);

        //    return (GenericGetter)getter.CreateDelegate(typeof(GenericGetter));
        //}

        internal Getters[] GetGetters(Type type, JSONParameters param)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }

            //PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <Getters> getters = new List <Getters>();

            //foreach (PropertyInfo p in props)
            //{
            //    if (!p.CanWrite && param.ShowReadOnlyProperties == false) continue;
            //    if (param.IgnoreAttributes != null)
            //    {
            //        bool found = false;
            //        foreach (var ignoreAttr in param.IgnoreAttributes)
            //        {
            //            if (p.IsDefined(ignoreAttr, false))
            //            {
            //                found = true;
            //                break;
            //            }
            //        }
            //        if (found)
            //            continue;
            //    }
            //    GenericGetter g = CreateGetMethod(type, p);
            //    if (g != null)
            //        getters.Add(new Getters { Getter = g, Name = p.Name });
            //}

            FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (var f in fi)
            {
                if (param.IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in param.IgnoreAttributes)
                    {
                        if (f.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }

                GenericGetter g = f.GetValue;// CreateGetField(type, f);
                if (g != null)
                {
                    getters.Add(new Getters {
                        Getter = g, Name = f.Name
                    });
                }
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return(val);
        }
 internal JSONSerializer(JSONParameters param)
 {
     this._params            = param;
     this._useEscapedUnicode = this._params.UseEscapedUnicode;
 }
Exemple #15
0
 internal void SetParameters(JSONParameters p)
 {
     parameters = p;
 }
Exemple #16
0
 /// <summary>
 /// Create an object from the json with parameter override on this call
 /// </summary>
 /// <param name="json"></param>
 /// <param name="param"></param>
 /// <returns></returns>
 public static object ToObject(string json, JSONParameters param)
 {
     return(new deserializer(param).ToObject(json, null));
 }
 internal JSONSerializer(JSONParameters param)
 {
     _params = param;
     _useEscapedUnicode = _params.UseEscapedUnicode;
     _MAX_DEPTH = _params.SerializerMaxDepth;
 }
Exemple #18
0
 public deserializer(JSONParameters param)
 {
     _params = param;
 }
Exemple #19
0
 /// <summary>
 /// Deep-copies an object i.e. clones to a new object.
 /// </summary>
 /// <typeparam name="T">The type of the object to be copied.</typeparam>
 /// <param name="obj">The object to be deep copied.</param>
 /// <param name="param">The <see cref="JSONParameters"/> to control deserialization.</param>
 /// <param name="manager">The <see cref="SerializationManager"/> to control advanced JSON deserialization.</param>
 /// <returns>The copy of <paramref name="obj"/>.</returns>
 public static T DeepCopy <T>(T obj, JSONParameters param, SerializationManager manager)
 {
     return(new JsonDeserializer(param, manager).ToObject <T>(ToJSON(obj)));
 }
 internal JSONSerializer(JSONParameters param)
 {
     _params = param;
 }
Exemple #21
0
 internal JSONSerializer(JSONParameters param)
 {
     _params            = param;
     _useEscapedUnicode = _params.UseEscapedUnicode;
 }
Exemple #22
0
 internal JSONSerializer(JSONParameters param)
 {
     _params            = param;
     _useEscapedUnicode = _params.UseEscapedUnicode;
     _MAX_DEPTH         = _params.SerializerMaxDepth;
 }
Exemple #23
0
        /// <summary>
        /// Create a formatted json string (beautified) from an object
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string ToNiceJSON(object obj, JSONParameters param)
        {
            string s = ToJSON(obj, param);

            return(Beautify(s, param.FormatterIndentSpaces));
        }
Exemple #24
0
        internal Getters[] GetGetters(Type type, JSONParameters param)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
                return val;

            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List<Getters> getters = new List<Getters>();
            foreach (PropertyInfo p in props)
            {
                if (!p.CanWrite && param.ShowReadOnlyProperties == false) continue;
                if (param.IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in param.IgnoreAttributes)
                    {
                        if (p.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                        continue;
                }
                GenericGetter g = CreateGetMethod(type, p);
                if (g != null)
                    getters.Add(new Getters { Getter = g, Name = p.Name });
            }

            FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (var f in fi)
            {
                if (param.IgnoreAttributes != null)
                {
                    bool found = false;
                    foreach (var ignoreAttr in param.IgnoreAttributes)
                    {
                        if (f.IsDefined(ignoreAttr, false))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                        continue;
                }

                GenericGetter g = CreateGetField(type, f);
                if (g != null)
                    getters.Add(new Getters { Getter = g, Name = f.Name });
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return val;
        }
Exemple #25
0
 /// <summary>
 /// Creates a typed generic object from the JSON with parameter and serialization manager override on this call.
 /// </summary>
 /// <typeparam name="T">The type of the expected object after deserialization.</typeparam>
 /// <param name="json">The JSON string to be deserialized.</param>
 /// <param name="param">The <see cref="JSONParameters"/> to control deserialization.</param>
 /// <param name="manager">The <see cref="SerializationManager"/> to control advanced JSON deserialization.</param>
 /// <returns>The deserialized object of type <typeparamref name="T"/>.</returns>
 public static T ToObject <T>(string json, JSONParameters param, SerializationManager manager)
 {
     return(new JsonDeserializer(param, manager).ToObject <T>(json));
 }