Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / Markdown

fastJSON - Smallest, Fastest Polymorphic JSON Serializer

Rate me:
Please Sign up or sign in to vote.
4.91/5 (412 votes)
23 Jan 2021CPOL37 min read 5.6M   63.7K   984  
In this article I demonstrate why fastJSON is the smallest, fastest polymorphic JSON serializer (with Silverlight4, MonoDroid and .NET core support)
Here we look at: the what and why of JSON, features of this implementation, some of the JSON alternatives that I have personally used, using the code, and performance tests.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace consoletest
{    

    class Program
    {
        static int count = 1000;
        static int tcount = 5;
        static DataSet ds = new DataSet();
        static bool exotic = false;
        static bool dsser = false;


        public static void Main(string[] args)
        {
            Console.WriteLine(".net version = " + Environment.Version);
            Console.WriteLine("press key : (E)xotic ");
            if (Console.ReadKey().Key == ConsoleKey.E)
                exotic = true;

            ds = CreateDataset();
            Console.WriteLine("-dataset");
            dsser = false;
            //bin_serialize();
            fastjson_serialize();
            //bin_deserialize();
            fastjson_deserialize();

            dsser = true;
            Console.WriteLine();
            Console.WriteLine("+dataset");
            //bin_serialize();
            fastjson_serialize();
            //bin_deserialize();
            fastjson_deserialize();

            Console.WriteLine();
            #region [ other tests]

            //			litjson_serialize();
            //			jsonnet_serialize();
            //			jsonnet4_serialize();
            //stack_serialize();

            //systemweb_deserialize();
            //bin_deserialize();
            //fastjson_deserialize();

            //			litjson_deserialize();
            //			jsonnet_deserialize();
            //			jsonnet4_deserialize();
            //			stack_deserialize();
            #endregion
        }

        private static string pser(object data)
        {
            System.Drawing.Point p = (System.Drawing.Point)data;
            return p.X.ToString() + "," + p.Y.ToString();
        }

        private static object pdes(string data)
        {
            string[] ss = data.Split(',');

            return new System.Drawing.Point(
                int.Parse(ss[0]),
                int.Parse(ss[1])
                );
        }

        private static string tsser(object data)
        {
            return ((TimeSpan)data).Ticks.ToString();
        }

        private static object tsdes(string data)
        {
            return new TimeSpan(long.Parse(data));
        }

        public static colclass CreateObject()
        {
            var c = new colclass();

            c.booleanValue = true;
            c.ordinaryDecimal = 3;

            if (exotic)
            {
                c.nullableGuid = Guid.NewGuid();
                c.hash = new Hashtable();
                c.bytes = new byte[1024];
                c.stringDictionary = new Dictionary<string, baseclass>();
                c.objectDictionary = new Dictionary<baseclass, baseclass>();
                c.intDictionary = new Dictionary<int, baseclass>();
                c.nullableDouble = 100.003;

                if (dsser)
                    c.dataset = ds;
                c.nullableDecimal = 3.14M;

                c.hash.Add(new class1("0", "hello", Guid.NewGuid()), new class2("1", "code", "desc"));
                c.hash.Add(new class2("0", "hello", "pppp"), new class1("1", "code", Guid.NewGuid()));

                c.stringDictionary.Add("name1", new class2("1", "code", "desc"));
                c.stringDictionary.Add("name2", new class1("1", "code", Guid.NewGuid()));

                c.intDictionary.Add(1, new class2("1", "code", "desc"));
                c.intDictionary.Add(2, new class1("1", "code", Guid.NewGuid()));

                c.objectDictionary.Add(new class1("0", "hello", Guid.NewGuid()), new class2("1", "code", "desc"));
                c.objectDictionary.Add(new class2("0", "hello", "pppp"), new class1("1", "code", Guid.NewGuid()));

                c.arrayType = new baseclass[2];
                c.arrayType[0] = new class1();
                c.arrayType[1] = new class2();
            }


            c.items.Add(new class1("1", "1", Guid.NewGuid()));
            c.items.Add(new class2("2", "2", "desc1"));
            c.items.Add(new class1("3", "3", Guid.NewGuid()));
            c.items.Add(new class2("4", "4", "desc2"));

            c.laststring = "" + DateTime.Now;

            return c;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            for (int j = 1; j < 3; j++)
            {
                DataTable dt = new DataTable();
                dt.TableName = "Table" + j;
                dt.Columns.Add("col1", typeof(int));
                dt.Columns.Add("col2", typeof(string));
                dt.Columns.Add("col3", typeof(Guid));
                dt.Columns.Add("col4", typeof(string));
                dt.Columns.Add("col5", typeof(bool));
                dt.Columns.Add("col6", typeof(string));
                dt.Columns.Add("col7", typeof(string));
                ds.Tables.Add(dt);
                Random rrr = new Random();
                for (int i = 0; i < 100; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = rrr.Next(int.MaxValue);
                    dr[1] = "" + rrr.Next(int.MaxValue);
                    dr[2] = Guid.NewGuid();
                    dr[3] = "" + rrr.Next(int.MaxValue);
                    dr[4] = true;
                    dr[5] = "" + rrr.Next(int.MaxValue);
                    dr[6] = "" + rrr.Next(int.MaxValue);

                    dt.Rows.Add(dr);
                }
            }
            return ds;
        }

        private static void fastjson_deserialize()
        {
            Console.WriteLine();
            Console.Write("fastjson deserialize");
            colclass c = CreateObject();
			var stopwatch = new Stopwatch();
            for (int pp = 0; pp < tcount; pp++)
            {
                colclass deserializedStore;
                string jsonText = null;

				stopwatch.Restart();
                jsonText = fastJSON.JSON.Instance.ToJSON(c);
                //Console.WriteLine(" size = " + jsonText.Length);
                for (int i = 0; i < count; i++)
                {
                    deserializedStore = (colclass)fastJSON.JSON.Instance.ToObject(jsonText);
                }
				stopwatch.Stop();
				Console.Write("\t" + stopwatch.ElapsedMilliseconds);
            }
        }

        private static void fastjson_serialize()
        {
            Console.WriteLine();
            Console.Write("fastjson serialize");
            colclass c = CreateObject();
			var stopwatch = new Stopwatch();
            for (int pp = 0; pp < tcount; pp++)
            {
                string jsonText = null;
				stopwatch.Restart();
                for (int i = 0; i < count; i++)
                {
                    jsonText = fastJSON.JSON.Instance.ToJSON(c);
                }
				stopwatch.Stop();
				Console.Write("\t" + stopwatch.ElapsedMilliseconds);
            }
        }

        private static void bin_deserialize()
        {
            Console.WriteLine();
            Console.Write("bin deserialize");
            colclass c = CreateObject();
			var stopwatch = new Stopwatch();
            for (int pp = 0; pp < tcount; pp++)
            {
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
				colclass deserializedStore = null;
				stopwatch.Restart();
                bf.Serialize(ms, c);
                //Console.WriteLine(" size = " +ms.Length);
                for (int i = 0; i < count; i++)
                {
					stopwatch.Stop(); // we stop then resume the stopwatch here so we don't factor in Seek()'s execution
                    ms.Seek(0L, SeekOrigin.Begin);
					stopwatch.Start();
                    deserializedStore = (colclass)bf.Deserialize(ms);
                }
				stopwatch.Stop();
				Console.Write("\t" + stopwatch.ElapsedMilliseconds);
            }
        }

        private static void bin_serialize()
        {
            Console.Write("\r\nbin serialize");
            colclass c = CreateObject();
			var stopwatch = new Stopwatch();
            for (int pp = 0; pp < tcount; pp++)
            {
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
				stopwatch.Restart();
                for (int i = 0; i < count; i++)
                {
					stopwatch.Stop(); // we stop then resume the stop watch here so we don't factor in the MemoryStream()'s execution
                    ms = new MemoryStream();
					stopwatch.Start();
                    bf.Serialize(ms, c);
                }
				stopwatch.Stop();
				Console.Write("\t" + stopwatch.ElapsedMilliseconds);
            }
        }

        #region [   other tests  ]
        /*
		private static void systemweb_serialize()
		{
			Console.WriteLine();
			Console.Write("msjson serialize");
			colclass c = CreateObject();
			var sws = new System.Web.Script.Serialization.JavaScriptSerializer();
			for (int pp = 0; pp < tcount; pp++)
			{
				DateTime st = DateTime.Now;
				colclass deserializedStore = null;
				string jsonText = null;

				//jsonText =sws.Serialize(c);
				//Console.WriteLine(" size = " + jsonText.Length);
				for (int i = 0; i < count; i++)
				{
					jsonText =sws.Serialize(c);
					//deserializedStore = (colclass)sws.DeserializeObject(jsonText);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

//		private static void stack_serialize()
//		{
//			Console.WriteLine();
//			Console.Write("stack serialize");
//			colclass c = CreateObject();
//			for (int pp = 0; pp < 5; pp++)
//			{
//				DateTime st = DateTime.Now;
//				string jsonText = null;
//
//				for (int i = 0; i < count; i++)
//				{
//					jsonText = ServiceStack.Text.JsonSerializer.SerializeToString(c);
//				}
//				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
//			}
//		}		

		private static void systemweb_deserialize()
//		{
//			Console.WriteLine();
//			Console.Write("fastjson deserialize");
//			colclass c = CreateObject();
//			var sws = new System.Web.Script.Serialization.JavaScriptSerializer();
//			for (int pp = 0; pp < tcount; pp++)
//			{
//				DateTime st = DateTime.Now;
//				colclass deserializedStore = null;
//				string jsonText = null;
//
//				jsonText =sws.Serialize(c);
//				//Console.WriteLine(" size = " + jsonText.Length);
//				for (int i = 0; i < count; i++)
//				{
//					deserializedStore = (colclass)sws.DeserializeObject(jsonText);
//				}
//				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
//			}
//		}

		private static void jsonnet4_deserialize()
		{
			Console.WriteLine();
			Console.Write("json.net4 deserialize");
			for (int pp = 0; pp < 5; pp++)
			{
				DateTime st = DateTime.Now;
				colclass c;
				colclass deserializedStore = null;
				string jsonText = null;
				c = Tests.mytests.CreateObject();
				var s = new Newtonsoft.Json.JsonSerializerSettings();
				s.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
				jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(c, Newtonsoft.Json.Formatting.Indented, s);
				for (int i = 0; i < count; i++)
				{
					deserializedStore = (colclass)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText, typeof(colclass), s);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

		private static void jsonnet4_serialize()
		{
			Console.WriteLine();
			Console.Write("json.net4 serialize");
			for (int pp = 0; pp < 5; pp++)
			{
				DateTime st = DateTime.Now;
				colclass c = Tests.mytests.CreateObject();
				Newtonsoft.Json.JsonSerializerSettings s = null;
				string jsonText = null;
				s = new Newtonsoft.Json.JsonSerializerSettings();
				s.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;

				for (int i = 0; i < count; i++)
				{
					jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(c, Newtonsoft.Json.Formatting.Indented, s);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

		private static void stack_deserialize()
		{
			Console.WriteLine();
			Console.Write("stack deserialize");
			for (int pp = 0; pp < 5; pp++)
			{
				DateTime st = DateTime.Now;
				colclass c;
				colclass deserializedStore = null;
				string jsonText = null;
				c = Tests.mytests.CreateObject();
				jsonText = ServiceStack.Text.JsonSerializer.SerializeToString(c);
				for (int i = 0; i < count; i++)
				{
					deserializedStore = ServiceStack.Text.JsonSerializer.DeserializeFromString<colclass>(jsonText);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

		private static void jsonnet_deserialize()
		{
			Console.WriteLine();
			Console.Write("json.net deserialize");
			for (int pp = 0; pp < 5; pp++)
			{
				DateTime st = DateTime.Now;
				colclass c;
				colclass deserializedStore = null;
				string jsonText = null;
				c = Tests.mytests.CreateObject();
				var s = new json.net.JsonSerializerSettings();
				s.TypeNameHandling = json.net.TypeNameHandling.All;
				jsonText = json.net.JsonConvert.SerializeObject(c, json.net.Formatting.Indented, s);
				for (int i = 0; i < count; i++)
				{
					deserializedStore = (colclass)json.net.JsonConvert.DeserializeObject(jsonText, typeof(colclass), s);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

		private static void jsonnet_serialize()
		{
			Console.WriteLine();
			Console.Write("json.net serialize");
			for (int pp = 0; pp < 5; pp++)
			{
				DateTime st = DateTime.Now;
				colclass c = Tests.mytests.CreateObject();
				json.net.JsonSerializerSettings s = null;
				string jsonText = null;
				s = new json.net.JsonSerializerSettings();
				s.TypeNameHandling = json.net.TypeNameHandling.All;

				for (int i = 0; i < count; i++)
				{
					jsonText = json.net.JsonConvert.SerializeObject(c, json.net.Formatting.Indented, s);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

		private static void litjson_deserialize()
		{
			Console.WriteLine();
			Console.Write("litjson deserialize");
			for (int pp = 0; pp < 5; pp++)
			{
				DateTime st = DateTime.Now;
				colclass c;
				colclass deserializedStore = null;
				string jsonText = null;
				c = Tests.mytests.CreateObject();
				jsonText = BizFX.Common.JSON.JsonMapper.ToJson(c);
				for (int i = 0; i < count; i++)
				{
					deserializedStore = (colclass)BizFX.Common.JSON.JsonMapper.ToObject(jsonText);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

		private static void litjson_serialize()
		{
			Console.WriteLine();
			Console.Write("litjson serialize");
			for (int pp = 0; pp < 5; pp++)
			{
				DateTime st = DateTime.Now;
				colclass c;
				string jsonText = null;
				c = Tests.mytests.CreateObject();
				for (int i = 0; i < count; i++)
				{
					jsonText = BizFX.Common.JSON.JsonMapper.ToJson(c);
				}
				Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds );
			}
		}

		
		 */
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect -
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.

* Mehdi is the 5th person to get 6 out of 7 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions