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;
using System.Collections.Specialized;

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;
        /// <summary>
        /// Output Enum values instead of names (default = False)
        /// </summary>
        public bool UseValuesOfEnums = false;
        /// <summary>
        /// Ignore attributes to check for (default : XmlIgnoreAttribute)
        /// </summary>
        public List<Type> IgnoreAttributes = new List<Type> { typeof(System.Xml.Serialization.XmlIgnoreAttribute) };
        /// <summary>
        /// If you have parametric and no default constructor for you classes (default = False)
        /// 
        /// IMPORTANT NOTE : If True then all initial values within the class will be ignored and will be not set
        /// </summary>
        public bool ParametricConstructorOverride = false;

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

    public static class JSON
    {
        public static JSONParameters Parameters = new JSONParameters();

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

            return Beautify(s);
        }

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

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

        public static object Parse(string json)
        {
            return new JsonParser(json, JSON.Parameters.IgnoreCaseOnDeserialize).Decode();
        }
#if net4
        public static dynamic ToDynamic(string json)
        {
            return new DynamicJson(json);
        }
#endif
        public static T ToObject<T>(string json)
        {
            return new deserializer(Parameters).ToObject<T>(json);
        }

        public static T ToObject<T>(string json, JSONParameters param)
        {
            return new deserializer(param).ToObject<T>(json);
        }

        public static object ToObject(string json)
        {
            return new deserializer(Parameters).ToObject(json, null);
        }

        public static object ToObject(string json, JSONParameters param)
        {
            return new deserializer(param).ToObject(json, null);
        }

        public static object ToObject(string json, Type type)
        {
            return new deserializer(Parameters).ToObject(json, type);
        }

        public static object FillObject(object input, string json)
        {
            Dictionary<string, object> ht = new JsonParser(json, Parameters.IgnoreCaseOnDeserialize).Decode() as Dictionary<string, object>;
            if (ht == null) return null;
            return new deserializer(Parameters).ParseDictionary(ht, null, input.GetType(), input);
        }

        public static object DeepCopy(object obj)
        {
            return new deserializer(Parameters).ToObject(ToJSON(obj));
        }

        public static T DeepCopy<T>(T obj)
        {
            return new deserializer(Parameters).ToObject<T>(ToJSON(obj));
        }

        public static string Beautify(string input)
        {
            return Formatter.PrettyPrint(input);
        }

        public static void RegisterCustomType(Type type, Serialize serializer, Deserialize deserializer)
        {
            Reflection.Instance.RegisterCustomType(type, serializer, deserializer);
        }

        internal static long CreateLong(out long num, string s, int index, int count)
        {
            num = 0;
            bool neg = false;
            for (int x = 0; x < count; x++, index++)
            {
                char cc = s[index];

                if (cc == '-')
                    neg = true;
                else if (cc == '+')
                    neg = false;
                else
                {
                    num *= 10;
                    num += (int)(cc - '0');
                }
            }
            if (neg) num = -num;

            return num;
        }
    }

    internal class deserializer
    {
        public deserializer(JSONParameters param)
        {
            _params = param;
        }

        pr