Super Fast Json Writer/Serializer

A minimalistic and super fast compile base JSON writer/serializer, for .NET

View project onGitHub

Welcome!

FastJsonWriter is a compile base tools for writer or create a JSON document from object(s).

This project helps you to have a super fast manual like JSON writer!

How is it work!

FastJsonWriter generate C# code for your specific object that you want to serialize to JSON. and then compile it to assembly using CodeProvider

Now you have a direct JSON writer for your object. I think this is the fastest way to generate JSON code ever.
FastJsonWriter cache all compiled assembly and reuse assembly while the project is running.

How to use!

Download the source and add FastJsonWriter.cs to your project. and then use it very easily.

var json = JsonWriter.WriteJson(YourObject);

For better performance, for large scale is more beneficial to use GetWriter to Cache Json Writer!

var writer = JsonWriter.GetWriter<YourClassName>();
writer.Writer(TextWriter, YourObject);
//Or
var json = writer.Write(YourObject);

Benchmark

After the research I've done. I found that the fastest Json Writer is StackServer.
So I got all the tests compared to StackServer

Write 1,000,000 item

var b = new b();
b.Username = "OmidID";
b.BornYear = 1985;
b.ID = 10225445554;
b.Another = new c() { Title = "Test Title" };
b.MyList = new a();
b.MyList.Add("Test 1");
b.MyList.Add("Test 2");
b.MyList.Add("Test 3");
b.MyList.Add("Test 4");
 
var writer = JsonWriter.GetWriter<b>();
 
var w = new Stopwatch();
using (var wr = new System.IO.StringWriter()) {
    w.Reset();
    w.Start();
    for (var i = 0; i < 1000000; i++) {
        JsonWriter.WriteJson(wr, b);
    }
    w.Stop();
    //Write using JsonWriter
    Console.WriteLine(w.ElapsedMilliseconds.ToString());
 
    w.Reset();
    w.Start();
    for (var i = 0; i < 1000000; i++) {
        writer.Writer(wr, b);
    }
    w.Stop();
    //Write using JsonWriterCache
    Console.WriteLine(w.ElapsedMilliseconds.ToString());
}
 
 
w.Reset();
w.Start();
for (var i = 0; i < 1000000; i++) {
    var outstr = ServiceStack.DynamicJson.Serialize(b);
}
w.Stop();
//Write using ServiceStack
Console.WriteLine(w.ElapsedMilliseconds.ToString());

Write a item with 1,000,000 array object

var f = new arr();
f.coll = new sub_arr[1];
f.coll[0= new sub_arr() { Name = "a" };
 
var writer_f = JsonWriter.GetWriter<arr>();
f.coll = new sub_arr[1000000];
 
for (var i = 0; i < f.coll.Length; i++) {
    f.coll[i] = new sub_arr { Name = i.ToString() };
}
 
 
w.Reset();
w.Start();
var test_f = JsonWriter.WriteJson(f);
w.Stop();
//Write using JsonWriter
Console.WriteLine(w.ElapsedMilliseconds.ToString());
 
w.Reset();
w.Start();
test_f = writer_f.Write(f);
w.Stop();
//Write using JsonWriterCache
Console.WriteLine(w.ElapsedMilliseconds.ToString());
 
w.Reset();
w.Start();
test_f = ServiceStack.DynamicJson.Serialize(f);
w.Stop();
//Write using ServiceStack
Console.WriteLine(w.ElapsedMilliseconds.ToString());

Looking for Json Parser?

This project only work on JSON writer. and if you want to have a JSON Parser you can use this project (this is my favorite project)

FastJsonParser

Beta version!

I'm so sorry for the beta version, currently I'm working on to complete this project.
Currently FastJsonWriter dose not support few datatype like Datetime. and also is not supported for the custom property name.

If you want special feature or if you got some error please write me Issue in the link below:
Issues

Contribute!

Just add your code and push it, after I checked your code, I will merge it to the main source.