Utf8JsonReader 和 JsonNode 解析 JSON 性能测试

2022年10月20日 1453点热度 0人点赞 0条评论
内容目录

数据:
file
格式:

  {
    "a_tttttttttttt": 1001,
    "b_tttttttttttt": "邱平",
    "c_tttttttttttt": "Nancy Lee",
    "d_tttttttttttt": "buqdu",
    "e_tttttttttttt": 81.26,
    "f_tttttttttttt": 60,
    "g_tttttttttttt": "1990-04-18 10:52:59",
    "h_tttttttttttt": "35812178",
    "i_tttttttttttt": "18935337799",
    "j_tttttttttttt": "w.nsliozye@mbwrxiyf.ug",
    "k_tttttttttttt": "浙江省 金华市 兰溪市"
  }

基础:

[SimpleJob(RuntimeMoniker.Net60)]
[MarkdownExporter, AsciiDocExporter, HtmlExporter, CsvExporter, RPlotExporter]
public class ParseJson
{
    private ReadOnlySequence<byte> sequence;

    [Params("100.json", "1000.json", "10000.json")]
    public string FileName;

    [GlobalSetup]
    public async Task Setup()
    {
        var text = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"json/{FileName}"));
        var bytes = Encoding.UTF8.GetBytes(text);
        sequence = new ReadOnlySequence<byte>(bytes);
    }
..... .....
}

Utf8JsonReader :


    [Benchmark]
    public void Utf8JsonReader()
    {
        var reader = new Utf8JsonReader(sequence, new JsonReaderOptions());
        UB(ref reader);
    }

    private static void UB(ref Utf8JsonReader reader)
    {
        while (reader.Read())
        {
            if (reader.TokenType is JsonTokenType.StartArray)
            {
                UA(ref reader);
            }
            else if (reader.TokenType is JsonTokenType.EndObject) break;
            else if (reader.TokenType is JsonTokenType.PropertyName)
            {
                reader.Read();
                if (reader.TokenType is JsonTokenType.StartArray)
                {
                    // 进入数组处理
                    UA(ref reader);
                }
                else if (reader.TokenType is JsonTokenType.StartObject)
                {
                    UB(ref reader);
                }
                else
                {
                }
            }
        }
    }

    private static void UA(ref Utf8JsonReader reader)
    {
        while (reader.Read())
        {
            if (reader.TokenType is JsonTokenType.EndArray) break;
            switch (reader.TokenType)
            {
                case JsonTokenType.StartObject:
                    UB(ref reader);
                    break;
                // [...,[],...]
                case JsonTokenType.StartArray:
                    UA(ref reader);
                    break;
            }
        }
    }

JsonNode:

    [Benchmark]
    public void JsonNode()
    {
        var reader = new Utf8JsonReader(sequence, new JsonReaderOptions());
        var nodes = System.Text.Json.Nodes.JsonNode.Parse(ref reader, null);
        if (nodes is JsonObject o)
        {
            JB(o);
        }
        else if (nodes is JsonArray a)
        {
            JA(a);
        }
    }

    private static void JB(JsonObject obj)
    {
        foreach (var item in obj)
        {
            var v = item.Value;
            if (v is JsonObject o)
            {
                JB(o);
            }
            else if (v is JsonArray a)
            {
                JA(a);
            }
            else if (v is JsonValue value)
            {
                var el = value.GetValue<JsonElement>();
                JE(el);
            }
        }
    }

    private static void JA(JsonArray obj)
    {
        foreach (var v in obj)
        {
            if (v is JsonObject o)
            {
                JB(o);
            }
            else if (v is JsonArray a)
            {
                JA(a);
            }
            else if (v is JsonValue value)
            {
                var el = value.GetValue<JsonElement>();
                JE(el);
            }
        }
    }

    private static void JE(JsonElement obj)
    {
    }

测试结果:
file

痴者工良

高级程序员劝退师

文章评论