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>
        /// ** work in progress
        /// </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;

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

    public sealed class JSON
    {
        //public readonly static JSON Instance = new 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;

        public string ToJSON(object obj)
        {
            _params = Parameters;
            _params.FixValues();
            Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
            return ToJSON(obj, Parameters);
        }

        public string ToJSON(object obj, JSONParameters param)
        {
            _params = param;
            _params.FixValues();
            Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
            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; Reflection.Instance.ShowReadOnlyProperties = true; }
            _usingglobals = _params.UsingGlobalTypes;
            return new JSONSerializer(_params).ConvertToJSON(obj);
        }

        public object Parse(string json)
        {
            _params = Parameters;
            Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
            return new JsonParser(json, _params.IgnoreCaseOnDeserialize).Decode();
        }

        public T ToObject<T>(string json)
        {
            return (T)ToObject(json, typeof(T));
        }

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

        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 == 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