js讀寫json文件,C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文檔

 2023-12-06 阅读 18 评论 0

摘要:  Swifter.Json 是由本人編寫的高性能且多功能的 Json 解析庫。下圖是 Swifter.Json 與 .Net 平臺上的其他 Json 庫性能對比:      在 Swifter.Json 近期更新的 API 中增加了直接構建 JSON 和直接解析 JSON 的方法。下面演示這兩個方法如何使用:   1&#

  Swifter.Json 是由本人編寫的高性能且多功能的 Json 解析庫。下圖是 Swifter.Json 與 .Net 平臺上的其他 Json 庫性能對比:

  

  在 Swifter.Json 近期更新的 API 中增加了直接構建 JSON 和直接解析 JSON 的方法。下面演示這兩個方法如何使用:

  1:使用 JsonWriter 直接生成 Json 文檔:

using Swifter.Json;
using Swifter.Tools;
using System;public class Demo
{public static void Main(){var jsonWriter = JsonFormatter.CreateJsonWriter();jsonWriter.WriteBeginObject();jsonWriter.WritePropertyName("Id");jsonWriter.WriteInt32(123);jsonWriter.WritePropertyName("Name");jsonWriter.WriteString("Dogwei");jsonWriter.WriteEndObject();Console.WriteLine(jsonWriter.HGCache.ToStringEx());/*** Output : {"Id":123,"Name":"Dogwei"}*/}
}

js讀寫json文件、  注意:使用 JsonWriter 時應將?jsonWriter 保存起來,重復使用,這樣才能將性能最大化。

  2:使用 JsonReader 直接遍歷 Json 文檔:

using Swifter.Json;
using System;
using System.IO;public class Demo
{public static void Main(){using var textReader = new StringReader("{\"Id\":123,\"Name\":\"Dogwei\"}");var jsonReader = JsonFormatter.CreateJsonReader(textReader);if (jsonReader.TryReadBeginObject()){while (!jsonReader.TryReadEndObject()){var name = jsonReader.ReadPropertyName();switch (name){case "Id":Console.WriteLine($"{name}: {jsonReader.ReadInt32()}");break;case "Name":Console.WriteLine($"{name}: {jsonReader.ReadString()}");break;default:Console.WriteLine($"{name}: {jsonReader.DirectRead()}");break;}}}/** * Output : * Id: 123* Name: Dogwei*/}
}

  3:更簡單的遍歷 Json 文檔:

using Swifter.Json;
using System;
using System.IO;public class Demo
{public static void Main(){using var textReader = new StringReader("[{\"Id\":1,\"Name\":\"Dogwei\"},{\"Id\":2,\"Name\":\"ChenXinwei\"},{\"Id\":3,\"Name\":\"Swifter.Json\"}]");var jsonReader = JsonFormatter.CreateJsonReader(textReader);foreach (var item in jsonReader.ReadArray()){foreach (var pair in item.ReadObject()){var name = pair.Key;var value = pair.Value.DirectRead();Console.WriteLine($"{name} : {value}");}}/** * Output : * Id : 1* Name : Dogwei* Id : 2* Name : ChenXinwei* Id : 3* Name : Swifter.Json*/}
}

  注意:JsonReader 是原始提供的是原始解析 Json 的方法,它性能極快,也正因此,它每個讀取方法都會偏移游標,不讀取就不偏移,解析 Json 時所有的 '值' 都必須讀且只讀一次!如上例:如果?pair.Value.DirectRead() 調用了兩次,或者一次都沒調用,那么就會解析出錯!

  下例做一下簡單的性能對比:

using Newtonsoft.Json;
using Swifter.Json;
using System;
using System.Diagnostics;
using System.IO;public class Demo
{public static void Main(){var swifterWriter = JsonFormatter.CreateJsonWriter();var newtonsoftStringWriter = new StringWriter();var newtonsoftWriter = new JsonTextWriter(newtonsoftStringWriter);while (true){var stopwatch = Stopwatch.StartNew();for (int i = 0; i < 1000000; i++){newtonsoftWriter.WriteStartObject();newtonsoftWriter.WritePropertyName("Id");newtonsoftWriter.WriteValue(123);newtonsoftWriter.WritePropertyName("Name");newtonsoftWriter.WriteValue("Dogwei");newtonsoftWriter.WriteEndObject();newtonsoftStringWriter.GetStringBuilder().Length = 0;}Console.WriteLine($"Newtonsoft.Json : {stopwatch.ElapsedMilliseconds}");stopwatch = Stopwatch.StartNew();for (int i = 0; i < 1000000; i++){swifterWriter.WriteBeginObject();swifterWriter.WritePropertyName("Id");swifterWriter.WriteInt32(123);swifterWriter.WritePropertyName("Name");swifterWriter.WriteString("Dogwei");swifterWriter.WriteEndObject();swifterWriter.Clear();}Console.WriteLine($"Swifter.Json : {stopwatch.ElapsedMilliseconds}");Console.ReadKey();}/*** Output:* Newtonsoft.Json : 197* Swifter.Json : 64*/}
}
using Newtonsoft.Json;
using Swifter.Json;
using System;
using System.Diagnostics;
using System.IO;public class Demo
{public static void Main(){while (true){var stopwatch = Stopwatch.StartNew();for (int i = 0; i < 1000000; i++){var jsonReader = new JsonTextReader(new StringReader("{\"Id\":123,\"Name\":\"Dogwei\"}"));while (jsonReader.Read()){if (jsonReader.TokenType == JsonToken.PropertyName){var name = (string)jsonReader.Value;switch (name){case "Id":jsonReader.ReadAsInt32();break;case "Name":jsonReader.ReadAsString();break;default:jsonReader.Skip();break;}}}}Console.WriteLine($"Newtonsoft.Json : {stopwatch.ElapsedMilliseconds}");stopwatch = Stopwatch.StartNew();for (int i = 0; i < 1000000; i++){var jsonReader = JsonFormatter.CreateJsonReader(new StringReader("{\"Id\":123,\"Name\":\"Dogwei\"}"));if (jsonReader.TryReadBeginObject()){while (!jsonReader.TryReadEndObject()){var name = jsonReader.ReadPropertyName();switch (name){case "Id":jsonReader.ReadInt32();break;case "Name":jsonReader.ReadString();break;default:jsonReader.SkipValue();break;}}}}Console.WriteLine($"Swifter.Json : {stopwatch.ElapsedMilliseconds}");Console.ReadKey();}/*** Output:* Newtonsoft.Json : 759* Swifter.Json : 161*/}
}

  特別強調:這兩種方式都是提供給有特別需求的用戶,普通用戶不建議使用,因為使用門檻較高,不利于維護!個人建議是定義模型,然后不管是序列化和反序列化都使用模型!這樣在保證性能的情況下,使用也變得簡單,易于維護。

jsonreader。?

  最后附上 Swifter.Json 的開源地址:https://github.com/Dogwei/Swifter.Json 希望大家支持一下。

轉載于:https://www.cnblogs.com/Dogwei/p/11238941.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/190367.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息