A simple test for dpz2.Json and Newtonsoft.Json parsing library under .Net Core

About dpz2.Json

dpz2.Json is self-development big fat Json parsing library software.

Applied to the simpler usage scenarios

Before dpz2.Json born, we have been using Newtonsoft.Json parsing library, Newtonsoft.Json most convenient place is the adoption of binding JavaBean-like way of operating, but the actual operation, we might just want more time a parser, so that we can quickly identify data, this time, a simple JavaBean way and become more elbow system, data is read using C # dynamic type can indeed be quite easy to operate.

Focus on direct manipulation

Another important reason for self-study prompted us to resolve a library is generated Json statement, if the generated data classes, we can really achieve rapid generation, but as a Json generates each data type, will greatly develop more programs cost.

A simple example of typical data Json:

{
  aaa:{
        bbb:[111,222],
        cc:"abc"
    },
    ddd:"qaz"
}

If Newtonsoft.Json without generating data classes, relying on the dynamic type C # accomplished:

dynamic obj = new Newtonsoft.Json.Linq.JObject();
dynamic aaa = obj.aaa = new Newtonsoft.Json.Linq.JObject();
Newtonsoft.Json.Linq.JArray bbb = aaa.bbb = new Newtonsoft.Json.Linq.JArray();
bbb.Add(111);
bbb.Add(222);
aaa.cc = "abc";
obj.ddd = "qaz";
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));

It can be seen, if at all types of Newtonsoft.Json unclear, will be very troublesome.

If you use dpz2.Json, can effectively avoid this problem:

using (var json = new dpz2.Json.JsonObject()) {
        var aaa = json.Object("aaa");
        var bbb = aaa.Array("bbb");
        bbb.Number(0).Value = 111;
        bbb.Number(1).Value = 222;
        aaa.String("cc").Value = "abc";
        json.String("ddd").Value = "qaz";
        Console.WriteLine(json.ToJsonString());
}

If you do not use intermediate variables can be used directly multistage operation mode (level in this way is very clear, but the efficiency of slightly slower):

using (var json = new dpz2.Json.JsonObject()) {
        json.Object("aaa").Array("bbb").Number(0).Value = 111;
        json.Object("aaa").Array("bbb").Number(1).Value = 222;
        json.Object("aaa").String("cc").Value = "abc";
        json.String("ddd").Value = "qaz";
        Console.WriteLine(json.ToJsonString());
}

In order to facilitate operation of the data, we provide the function mode and the index mode two meanings different operation modes: Mode function add / adaptive mode access, i.e., access does not exist as an object, the object is automatically added; and the index compared with access mode mode, such as access to the object does not exist, it will return null null object.
Two operating modes need to be more freely arranged in combination, the above embodiment may be modified to code for:

using (var json = new dpz2.Json.JsonObject()) {
        json.Object("aaa").Array("bbb").Number(0).Value = 111;
        json["aaa"]["bbb"].Number(1).Value = 222;
        json["aaa"].String("cc").Value = "abc";
        json.String("ddd").Value = "qaz";
        Console.WriteLine(json.ToJsonString());
}

We offer two ways to get data (two methods are equivalent, the efficiency is similar, according to the actual needs of use):

double b1 = json.Object("aaa").Array("bbb").Number(0).Value;
double b1 = json["aaa"]["bbb"][0].GetNumber();

Resolution efficiency dpz2.Json VS Newtonsoft.Json contrast to the simple environment

The test is used to develop my notebook, small trendy 7000, configured to i5-8250U / 8G memory

The test to resolve a relatively long (about 1800 + character length) the actual API interaction data to resolve a plus sequence into operation, to compare the efficiency of analysis of the two libraries.

The following is a test code (compiled using the Release configuration):

using System;

namespace demo {
    class Program {
        static void Main(string[] args) {

            string str = "{\"Header\":{\"Ver\":\"\",\"Type\":\"\",\"SessionID\":\"\",\"Time\":\"1565942801\",\"Status\":\"\",\"Error\":\"\"},\"Data\":{\"Apis\":[{\"Url\":\"/Api/Help/Get\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.HelpController\",\"Description\":\"API\u8f85\u52a9\u63a5\u53e3\uff0c\u8fd4\u56de\u670d\u52a1\u5668\u4e0a\u6240\u6709\u7684API\u5217\u8868\"},{\"Url\":\"/Api/Info/Update\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.InfoController\",\"Description\":\"\u83b7\u53d6\u66f4\u65b0\u65e5\u5fd7\"},{\"Url\":\"/Api/Info/Environment\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.InfoController\",\"Description\":\"\u83b7\u53d6\u8fd0\u884c\u73af\u5883\u4fe1\u606f\"},{\"Url\":\"/Api/Info/Debug\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.InfoController\",\"Description\":\"\u83b7\u53d6\u8fd0\u884c\u73af\u5883\u4fe1\u606f\"},{\"Url\":\"/Api/Info/Get\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.InfoController\",\"Description\":\"\u83b7\u53d6\u6240\u6709\u4e0e\u672c\u7a0b\u5e8f\u76f8\u5173\u7684 \u4fe1\u606f\"},{\"Url\":\"/Api/User/GetInfo\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.UserController\",\"Description\":\"\u4ee5Get\u65b9\u5f0f\u83b7 \u53d6\u767b\u9646\u7528\u6237\u4fe1\u606f\"},{\"Url\":\"/Api/User/PostInfo\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.UserController\",\"Description\":\" \u4ee5Post\u65b9\u5f0f\u83b7\u53d6\u767b\u9646\u7528\u6237\u4fe1\u606f\"},{\"Url\":\"/Api/User/Get\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.UserController\",\"Description\":\"\u9ed8\u8ba4\u4ea4\u4e92\uff0c\u8fd4\u56de\u7a7a\u5185\u5bb9\"},{\"Url\":\"/Api/Ycc/GetConfig\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.YccController\",\"Description\":\"\u83b7\u53d6\u914d\u7f6e\u4fe1\u606f\"},{\"Url\":\"/Api/Ycc/CreateNew\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.YccController\",\"Description\":\"\u521b\u5efa\u4e00\u4e2a\u4ea4\u4e92\u6807\u8bc6\"},{\"Url\":\"/Api/Ycc/Get\",\"LoginNeed\":false,\"Controller\":\"Kernel.Api.YccController\",\"Description\":\"\u9ed8\u8ba4\u4ea4\u4e92\uff0c\u8fd4\u56de\u7a7a\u5185\u5bb9\"}]}}";

            int tick0 = Environment.TickCount;

            dpz2.Json.Parser.EnforceUnicode = false;
            using (var json = dpz2.Json.Parser.ParseJson(str)) {
                Console.WriteLine(json.ToJsonString());
            }
            int tick1 = Environment.TickCount;

            dynamic dy = Newtonsoft.Json.JsonConvert.DeserializeObject(str);
            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(dy));
            int tick2 = Environment.TickCount;

            Console.WriteLine($"[dpz2.Json] {tick1 - tick0}ms");
            Console.WriteLine($"[Newtonsoft.Json] {tick2 - tick1}ms");
        }
    }
}

First run:

[dpz2.Json] 31ms
[Newtonsoft.Json] 438ms

Second run:

[dpz2.Json] 47ms
[Newtonsoft.Json] 422ms

Third run:

[dpz2.Json] 32ms
[Newtonsoft.Json] 422ms

Fourth run:

[dpz2.Json] 31ms
[Newtonsoft.Json] 438ms

The fifth run:

[dpz2.Json] 46ms
[Newtonsoft.Json] 469ms

The resolution efficiency dpz2.Json is satisfactory, but Newtonsoft.Json time-consuming looks like a little big, help us to see whether there Newtonsoft.Json optimization parameters can be adjusted, so that the test can be more valuable.

dpz2.Jso is an open source library, you can download the source repository in the following codes:

Github:https://github.com/dpzsoft/dotnet-core-dpz2-json
Code Cloud (Gitee):https://gitee.com/dpzsoft/dotnet-core-dpz2-json

Nuget package also simultaneously launched:

Access Address: https://www.nuget.org/packages/dotnet-core-dpz2-json/

Guess you like

Origin blog.51cto.com/5103803/2430242