Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

fastJSON

, 14 Sep 2014
Smallest, fastest polymorphic JSON serializer (with Silverlight4 and MonoDroid support)
consoletest-noexe.zip
Consoletest.zip
fastjson-noexe.zip
fastJSON.snk
fastJSON.zip
fastJSON.snk
fastJSON_2.0.28.1.zip
Consoletest
fastJSON
fastJSON.snk
Properties
README.md
UnitTests
UnitTests.nunit
Properties
fastjson_v1.1-noexe.zip
fastJSON_v1.1.zip
Properties
fastjson_v1.2-noexe.zip
fastJSON_v1.2.zip
fastjson_v1.3-noexe.zip
fastJSON_v1.3.zip
fastjson_v1.4-noexe.zip
fastJSON_v1.4.zip
fastjson_v1.5-noexe.zip
fastJSON_v1.5.zip
fastjson_v1.6-noexe.zip
fastJSON_v1.6.zip
fastjson_v1.7-noexe.zip
fastjson_v1.7.5-noexe.zip
fastJSON_v1.7.5.zip
fastjson_v1.7.6-noexe.zip
fastJSON_v1.7.6.zip
fastjson_v1.7.7-noexe.zip
fastjson_v1.7.7-silverlight4-noexe.zip
fastJSON_v1.7.7-SilverLight4.zip
fastJSON_v1.7.7.zip
fastJSON_v1.7.zip
fastjson_v1.8-noexe.zip
fastJSON_v1.8.zip
fastjson_v1.9-noexe.zip
fastjson_v1.9.1-noexe.zip
fastJSON_v1.9.1.zip
fastjson_v1.9.2-noexe.zip
fastjson_v1.9.2.zip
fastjson_v1.9.3-noexe.zip
fastJSON_v1.9.3.zip
fastjson_v1.9.4-noexe.zip
fastJSON_v1.9.4.zip
fastjson_v1.9.6-noexe.zip
fastJSON_v1.9.6.zip
fastjson_v1.9.7-noexe.zip
fastJSON_v1.9.7.zip
fastjson_v1.9.8-noexe.zip
fastJSON_v1.9.8.zip
fastjson_v1.9.9-noexe.zip
fastJSON_v1.9.9.zip
fastJSON_v1.9.zip
fastjson_v2.0.0-noexe.zip
UnitTests.nunit
fastJSON_v2.0.0.zip
UnitTests.nunit
fastjson_v2.0.1-noexe.zip
UnitTests.nunit
fastJSON_v2.0.1.zip
UnitTests.nunit
fastjson_v2.0.10-noexe.zip
UnitTests.nunit
fastJSON_v2.0.10.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.11-noexe.zip
UnitTests.nunit
fastJSON_v2.0.11.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.12-noexe.zip
UnitTests.nunit
fastJSON_v2.0.12.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.13-noexe.zip
UnitTests.nunit
fastJSON_v2.0.13.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.14-noexe.zip
UnitTests.nunit
fastJSON_v2.0.14.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.15-noexe.zip
UnitTests.nunit
fastJSON_v2.0.15.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.16-noexe.zip
UnitTests.nunit
fastJSON_v2.0.16.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.17-noexe.zip
UnitTests.nunit
fastJSON_v2.0.17.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.18-noexe.zip
UnitTests.nunit
fastJSON_v2.0.18.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.19-noexe.zip
UnitTests.nunit
fastJSON_v2.0.19.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.2-noexe.zip
UnitTests.nunit
fastJSON_v2.0.2.zip
UnitTests.nunit
fastjson_v2.0.20-noexe.zip
UnitTests.nunit
fastJSON_v2.0.20.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.21-noexe.zip
UnitTests.nunit
fastJSON_v2.0.21.zip
UnitTests.nunit
fastJSON-SL.v11.suo
fastJSON.nuspec
fastjson_v2.0.22-noexe.zip
fastJSON.nuspec
README.md
UnitTests.nunit
fastJSON_v2.0.22.zip
fastJSON.nuspec
README.md
UnitTests.nunit
fastJSON.snk
fastjson_v2.0.23-noexe.zip
README.md
UnitTests.nunit
fastJSON_v2.0.23.zip
README.md
UnitTests.nunit
fastJSON.snk
fastjson_v2.0.24-noexe.zip
README.md
UnitTests.nunit
fastJSON_v2.0.24.zip
README.md
UnitTests.nunit
fastJSON.snk
fastjson_v2.0.25-noexe.zip
README.md
UnitTests.nunit
fastJSON_v2.0.25.zip
README.md
UnitTests.nunit
fastJSON.snk
fastjson_v2.0.26-noexe.zip
README.md
UnitTests.nunit
fastJSON_v2.0.26.zip
README.md
UnitTests.nunit
fastJSON.snk
fastjson_v2.0.27-noexe.zip
README.md
UnitTests.nunit
fastJSON_v2.0.27.zip
README.md
UnitTests.nunit
fastJSON.snk
fastJSON_v2.0.28.zip
fastJSON.snk
README.md
UnitTests.nunit
fastjson_v2.0.3-noexe.zip
UnitTests.nunit
fastJSON_v2.0.3.zip
UnitTests.nunit
fastjson_v2.0.4-noexe.zip
UnitTests.nunit
fastjson_v2.0.4.zip
UnitTests.nunit
fastjson_v2.0.5-noexe.zip
UnitTests.nunit
fastJSON_v2.0.5.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.6-noexe.zip
UnitTests.nunit
fastJSON_v2.0.6.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.7-noexe.zip
UnitTests.nunit
fastJSON_v2.0.7.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.8-noexe.zip
UnitTests.nunit
fastJSON_v2.0.8.zip
UnitTests.nunit
fastJSON.nuspec
fastjson_v2.0.9-noexe.zip
UnitTests.nunit
fastJSON_v2.0.9.zip
UnitTests.nunit
fastJSON.nuspec
fastJSON_v2.1.0.zip
fastJSON.snk
README.md
UnitTests.nunit
fastJSON_v2.1.1.zip
fastJSON.snk
README.md
UnitTests.nunit
fastJSON_v2.1.2.zip
fastJSON.snk
README.md
UnitTests.nunit
fastJSON_v2.1.3.zip
fastJSON.snk
README.md
UnitTests.nunit
using System;
using System.Collections;
using System.Collections.Generic;
#if !SILVERLIGHT
using System.Data;
#endif
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;

namespace fastJSON
{
    public delegate string Serialize(object data);
    public delegate object Deserialize(string data);

    public sealed class JSONParameters
    {
        /// <summary>
        /// Use the optimized fast Dataset Schema format (default = True)
        /// </summary>
        public bool UseOptimizedDatasetSchema = true;
        /// <summary>
        /// Use the fast GUID format (default = True)
        /// </summary>
        public bool UseFastGuid = true;
        /// <summary>
        /// Serialize null values to the output (default = True)
        /// </summary>
        public bool SerializeNullValues = true;
        /// <summary>
        /// Use the UTC date format (default = True)
        /// </summary>
        public bool UseUTCDateTime = true;
        /// <summary>
        /// Show the readonly properties of types in the output (default = False)
        /// </summary>
        public bool ShowReadOnlyProperties = false;
        /// <summary>
        /// Use the $types extension to optimise the output json (default = True)
        /// </summary>
        public bool UsingGlobalTypes = true;
        /// <summary>
        /// Ignore case when processing json and deserializing 
        /// </summary>
        public bool IgnoreCaseOnDeserialize = false;
        /// <summary>
        /// Anonymous types have read only properties 
        /// </summary>
        public bool EnableAnonymousTypes = false;
        /// <summary>
        /// Enable fastJSON extensions $types, $type, $map (default = True)
        /// </summary>
        public bool UseExtensions = true;
        /// <summary>
        /// Use escaped unicode i.e. \uXXXX format for non ASCII characters (default = True)
        /// </summary>
        public bool UseEscapedUnicode = true;
        /// <summary>
        /// Output string key dictionaries as "k"/"v" format (default = False) 
        /// </summary>
        public bool KVStyleStringDictionary = false;

        public void FixValues()
        {
            if (UseExtensions == false) // disable conflicting params
                UsingGlobalTypes = false;
            if (EnableAnonymousTypes)
                ShowReadOnlyProperties = true;
        }
    }

    public sealed class JSON
    {
        [ThreadStatic]
        private static JSON _instance;

        public static JSON Instance
        {
            get { return _instance ?? (_instance = new JSON()); }
        }

        private JSON()
        {
        }
        /// <summary>
        /// You can set these paramters globally for all calls
        /// </summary>
        public JSONParameters Parameters = new JSONParameters();

        private JSONParameters _params;
        internal SafeDictionary<Type, Serialize> _customSerializer = new SafeDictionary<Type, Serialize>();
        internal SafeDictionary<Type, Deserialize> _customDeserializer = new SafeDictionary<Type, Deserialize>();
        SafeDictionary<string, SafeDictionary<string, myPropInfo>> _propertycache = new SafeDictionary<string, SafeDictionary<string, myPropInfo>>();
        bool _usingglobals = false;

        public string ToNiceJSON(object obj, JSONParameters param)
        {
            string s = ToJSON(obj, param);

            return Beautify(s);
        }

        public string ToJSON(object obj)
        {
            return ToJSON(obj, Parameters);
        }

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

        public object Parse(string json)
        {
            //_params = Parameters;
            return new JsonParser(json, Parameters.IgnoreCaseOnDeserialize).Decode();
        }
#if net4
        public dynamic ToDynamic(string json)
        {
            return new DynamicJson(json);
        }
#endif
        public T ToObject<T>(string json)
        {
            Type t = typeof(T);
            var o = ToObject(json, t);

            if (t.IsArray)
            {
                if ((o as ICollection).Count == 0) // edge case for "[]" -> T[]
                {
                    Type tt = t.GetElementType();
                    object oo = Array.CreateInstance(tt, 0);
                    return (T)oo;
                }
                else
                    return (T)o;
            }
            else
                return (T)o;
        }

        public object ToObject(string json)
        {
            return ToObject(json, null);
        }

        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(Hashtab