# JsonFast **Repository Path**: dsfdsfsdfsdf/jsonfast ## Basic Information - **Project Name**: JsonFast - **Description**: JSON 无第三方依赖 可单独复制JSONFast.cs类文件使用 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 10 - **Created**: 2023-07-23 - **Last Updated**: 2023-07-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # JSONFast 当前版本1.0.8 :bug: 1.0.7以及之前的版本有一个致命bug,如果对象里含有双引号,序列化时没有进行转义,导致无法反序列化,[更新日志](https://gitee.com/majorworld/jsonfast#5%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97) :gem: 添加了动态获取字段的新特性,按空格分隔的字符串,数字代表索引,其余的为字段,[点击查看下列第8条演示](https://gitee.com/majorworld/jsonfast#8%E6%B7%BB%E5%8A%A0%E5%8A%A8%E6%80%81%E8%8E%B7%E5%8F%96%E5%AD%97%E6%AE%B5) #### (1)介绍 JSON序列化工具,无第三方依赖,可单独复制[JSONFast.cs](https://gitee.com/majorworld/jsonfast/raw/master/JsonFast/JsonFast.cs)类文件使用,仅900行代码 [Github入口](https://github.com/majorworld/JsonFast) #### (2)软件架构 最低适用于net 4.0及以后版本 除了数组、集合和基础类型,还包括Color、Point、DataTable、DataSet和Byte[]的序列化和反序列化,兼容Newstonsoft.Json序列化结果 #### (3)安装教程 复制[JSONFast.cs](https://gitee.com/majorworld/jsonfast/raw/master/JsonFast/JsonFast.cs)一个文件到自己项目中,就可以使用两个拓展方法JSONFrom()反序列化和JSONTo()序列化 #### (4)使用说明 ##### 1、反序列化为字典,返回这种类型比Newstonsoft.Json更快 ```cs string str = "{\"state\":true,\"time\":\"2020-10-10 1:2:1\",\"num\":-33,\r\n\t\f \"name\":\"你好\r\n\t\f左\b右,\\\"世界\\\"\",\"age\":9.9,\"yy\":{\"sex\":null}}"; Dictionary t1 = str.JSONFrom(); ``` ##### 2、反序列化为实体类或动态类 ```cs string str = "{\"Name\":\"高老师\",\"Num\":3.1415926,\"Col\":\"2,3,4\"}"; Teacher t1 = str.JSONFrom(); dynamic t2 = str.JSONFrom(); ``` ##### 3、对象或集合序列化字符串 ```cs List list = new List(); string t1 = list.JSONTo(); ``` ##### 4、判断是不是数组 ```cs string str1 = "{\"A\":{\"a\":1,\"b\":2}}"; string str2 = "{\"A\":[1,2]}"; var dy1 = str1.JSONFrom(); if (dy1["A"] is IList) Console.WriteLine("d1里是数组"); var dy2 = str2.JSONFrom(); if (dy2["A"] is IList) Console.WriteLine("d2里是数组"); ``` ##### 5、类似Newstonsoft的JObject和JArray ```cs //JObject Dictionary dict = new Dictionary(); dict.Add("name", "tom"); dict.Add("age", 18); //JArray List> list = new List>(); list.Add(dict); //序列化和反序列化 var stringArr = list.JsonTo(); Console.WriteLine(stringArr); var Arr = stringArr.JsonFrom>>(); ``` ##### 6、如果有字段存在无限递归引用,抛出异常System.StackOverflowException:“Exception of type 'System.StackOverflowException' was thrown.”,可使用[FastIgnore]特性过滤掉问题字段 ```cs class UserItem { public User User { get; set; } public int UserId { get; set; } } class User { public string Name { get; set; } //过滤掉无限递归引用的问题字段 [FastIgnore] public UserItem Bad { get; set; } } ``` ##### 7、支持对unicode字符串的解析 ```cs string str = "{\"Name\":\"\\u4f60\\u597d\"}"; string json = str.JsonFrom().JsonTo(); Console.WriteLine(json);// "{\"Name\":\"你好\"}"; ``` ##### 8、添加动态获取字段 ```cs //第二个参数,传入空格分隔的字符串,直接获取到tgt字段的内容 string result = "{\"type\":\"ZH_CN2EN\",\"errorCode\":0,\"elapsedTime\":0,\"translateResult\":[[{\"src\":\"有人在家吗\",\"tgt\":\"Is anyone home?\"}]]}"; var tgt= JsonFast.PickData(result, "translateResult 0 0 tgt"); //获取到数组类型元素的数量,然后按该数量循环,使用PickData依次获取数组里所有元素 string json = "{\"Student\":[{\"Name\":\"Lucy\"},{\"Name\":\"Jack\"},{\"Name\":\"Tom\"}]}"; int count = JsonFast.ArrayCount(json, "Student"); ``` #### (5)更新日志 | 更新日志 |版本| |------------|--| | 2023-05-31 |1.0.8| | 1、修复了序列化时没有将对象里的双引号进行转义,导致反序列化失败的严重bug|| | 2023-05-15 |1.0.7| | 1、添加动态获取字段,PickData和ArrayCount方法|| | 2023-01-03 |1.0.6| | 1、优化代码结构,防止私有拓展方法全局污染|| | 2022-12-29 |1.0.5| | 1、添加对unicode字符串的解析|| | 2022-12-28 |1.0.4| | 1、修复了转DataTable时,字段存在null报错问题 || | 2、添加了对nullable的支持 || | 3、修改命名空间为System,方便使用 || | 2022-12-26 |1.0.3| | 1、添加了对枚举的反序列化支持 || #### (6)单元测试 如果没有net6和net7环境,可以将JsonFastBenchmark.csproj文件中的 ```xml net461;netcoreapp3.1;net6.0;net7.0 ``` 改成 ```xml net461;netcoreapp3.1 ``` ### (7)使用案例 [https://gitee.com/dotnetchina/TouchSocket](https://gitee.com/dotnetchina/TouchSocket) [链接](https://gitee.com/dotnetchina/TouchSocket/blob/master/src/TouchSocket/Core/Serialization/Json/JsonFast.cs)