|
| 1 | +[简体中文](./README_CN.md) [English](./README.md) |
| 2 | + |
| 3 | +# 简介 |
| 4 | + |
| 5 | +`STJson`是一款基于`MIT`开源协议的`Json`解析库。该库纯原生实现不依赖任何库,所以非常轻量便捷,且功能强大。 |
| 6 | + |
| 7 | +完整教程: |
| 8 | + |
| 9 | +HOME: [https://DebugST.github.io/STJson](https://DebugST.github.io/STJson) |
| 10 | + |
| 11 | +CN: [https://DebugST.github.io/STJson/tutorial_cn.html](https://DebugST.github.io/STJson/tutorial_cn.html) |
| 12 | + |
| 13 | +EN: [https://DebugST.github.io/STJson/tutorial_en.html](https://DebugST.github.io/STJson/tutorial_en.html) |
| 14 | + |
| 15 | +`STJson`不同与其他`Json`库拥有类似与`JObject`和`JArray`的对象。在`STJson`中仅一个`STJson`对象,它既可以是`Object`,也可以是`Array`。`STJson`拥有两个索引器:`STJson[int]`和`STJson[string]`。可通过`STJson.ValueType`确定当前`Json`对象的类型。 |
| 16 | + |
| 17 | +```cs |
| 18 | +var json_1 = new STJson(); |
| 19 | +Console.WriteLine("[json_1] - " + json_1.IsNullObject + " - " + json_1.ValueType); |
| 20 | + |
| 21 | +var json_2 = STJson.New(); |
| 22 | +json_2.SetItem("key", "value"); |
| 23 | +Console.WriteLine("[json_2] - " + json_2.IsNullObject + " - " + json_2.ValueType); |
| 24 | + |
| 25 | +var json_3 = new STJson(); |
| 26 | +json_3.Append(1, 2, 3); |
| 27 | +Console.WriteLine("[json_3] - " + json_3.IsNullObject + " - " + json_3.ValueType); |
| 28 | + |
| 29 | +var json_4 = new STJson(); |
| 30 | +json_4.SetValue(DateTime.Now); |
| 31 | +Console.WriteLine("[json_4] - " + json_4.IsNullObject + " - " + json_4.ValueType); |
| 32 | + |
| 33 | +var json_5 = STJson.CreateArray(); // made by static function |
| 34 | +Console.WriteLine("[json_5] - " + json_5.IsNullObject + " - " + json_5.ValueType); |
| 35 | + |
| 36 | +var json_6 = STJson.CreateObject(); // made by static function |
| 37 | +Console.WriteLine("[json_6] - " + json_6.IsNullObject + " - " + json_6.ValueType); |
| 38 | + |
| 39 | +var json_7 = STJson.FromObject(12); // made by static function |
| 40 | +Console.WriteLine("[json_3] - " + json_7.IsNullObject + " - " + json_7.ValueType); |
| 41 | +/******************************************************************************* |
| 42 | + * [output] * |
| 43 | + *******************************************************************************/ |
| 44 | +[json_1] - True - Undefined |
| 45 | +[json_2] - False - Object |
| 46 | +[json_3] - False - Array |
| 47 | +[json_4] - False - Datetime |
| 48 | +[json_5] - False - Array |
| 49 | +[json_6] - False - Object |
| 50 | +[json_7] - False - Long |
| 51 | +``` |
| 52 | + |
| 53 | +# STJson |
| 54 | + |
| 55 | +`STJson`是一个中间数据类型,它是`string`与`object`之间的桥梁,使用非常便捷,比如: |
| 56 | + |
| 57 | +```cs |
| 58 | +var st_json = new STJson() |
| 59 | + .SetItem("number", 0) // 函数返回自身 所以可以连续操作 |
| 60 | + .SetItem("boolean", true) |
| 61 | + .SetItem("string", "this is string") |
| 62 | + .SetItem("datetime", DateTime.Now) |
| 63 | + .SetItem("array_1", STJson.CreateArray(123, true, "string")) |
| 64 | + .SetItem("array_2", STJson.FromObject(new object[] { 123, true, "string" })) |
| 65 | + .SetItem("object", new { key = "this is a object" }) |
| 66 | + .SetItem("null", obj: null); |
| 67 | +st_json.SetKey("key").SetValue("this is a test"); |
| 68 | +Console.WriteLine(st_json.ToString(4)); // 4 -> indentation space count |
| 69 | +/******************************************************************************* |
| 70 | + * [output] * |
| 71 | + *******************************************************************************/ |
| 72 | +{ |
| 73 | + "number": 0, |
| 74 | + "boolean": true, |
| 75 | + "string": "this is string", |
| 76 | + "datetime": "2023-04-22T21:12:30.6109410+08:00", |
| 77 | + "array_1": [ |
| 78 | + 123, true, "string" |
| 79 | + ], |
| 80 | + "array_2": [ |
| 81 | + 123, true, "string" |
| 82 | + ], |
| 83 | + "object": { |
| 84 | + "key": "this is a object" |
| 85 | + }, |
| 86 | + "null": null, |
| 87 | + "key": "this is a test" |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +# 序列化 |
| 92 | + |
| 93 | +通过上面的例子或许你已经知道怎么将一个对象转换为`string`,通过`STJson.FromObject(object).ToString(+n)`即可,但是有没有可能,其实不用这么麻烦的?比如:`STJson.Serialize(+n)`就可以了??? |
| 94 | + |
| 95 | +事实上`STJson.Serialize(+n)`的效率会更好,因为它是直接将对象转换为字符串,而不是转换成`STJson`再转换成字符串。 |
| 96 | + |
| 97 | +```cs |
| 98 | +Console.WriteLine(STJson.Serialize(new { key = "this is test" })); |
| 99 | +/******************************************************************************* |
| 100 | + * [output] * |
| 101 | + *******************************************************************************/ |
| 102 | +{"key":"this is test"} |
| 103 | +``` |
| 104 | + |
| 105 | +当然你可以有个更友好的输出格式: |
| 106 | + |
| 107 | +```cs |
| 108 | +Console.WriteLine(STJson.Serialize(new { key = "this is test" }, 4)); |
| 109 | +/******************************************************************************* |
| 110 | + * [output] * |
| 111 | + *******************************************************************************/ |
| 112 | +{ |
| 113 | + "key": "this is test" |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +事实上格式化输出是通过调用静态函数`STJson.Format(+n)`完成的。如果你觉得不喜欢作者内置的格式化风格,完全可以自己写一个格式化的函数。或者去修改源码文件`STJson.Statics.cs:Format(string,int)`。 |
| 118 | + |
| 119 | +# 反序列化 |
| 120 | + |
| 121 | +事实上代码并不会直接将`string`转换为`object`。因为在那之前必须先对字符串进行解析,确保它是一个正确格式的`Json`。但是做完这个过程的时候已经得到一个`STJson`对象了。最后将`STJson`再转换为`object`。 |
| 122 | + |
| 123 | +所以你会在源代码`STLib.Json.Converter`中看到如下文件: |
| 124 | + |
| 125 | +`ObjectToSTJson.cs` `ObjectToString.cs` `STJsonToObject.cs` `StringToSTJson.cs` |
| 126 | + |
| 127 | +里面并没有`StringToObject.cs`文件,而`STJson.Deserialize(+n)`的源码如下: |
| 128 | + |
| 129 | +```cs |
| 130 | +public static T Deserialize<T>(string strJson, +n) { |
| 131 | + var json = StringToSTJson.Get(strJson, +n); |
| 132 | + return STJsonToObject.Get<T>(json, +n); |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +如何将字符串转换为对象,相信作者不用说明读者也应该知道如何处理,但是这里值得说明的是,`STJson`可以附加到对象中,实现局部更新。 |
| 137 | + |
| 138 | +```cs |
| 139 | +public class TestClass { |
| 140 | + public int X; |
| 141 | + public int Y; |
| 142 | +} |
| 143 | + |
| 144 | +TestClass tc = new TestClass() { |
| 145 | + X = 10, |
| 146 | + Y = 20 |
| 147 | +}; |
| 148 | +STJson json_test = new STJson().SetItem("Y", 100); |
| 149 | +STJson.Deserialize(json_test, tc); |
| 150 | +Console.WriteLine(STJson.Serialize(tc)); |
| 151 | +/******************************************************************************* |
| 152 | + * [output] * |
| 153 | + *******************************************************************************/ |
| 154 | + {"X":10,"Y":100} |
| 155 | +``` |
| 156 | + |
| 157 | +# STJsonPath |
| 158 | + |
| 159 | +测试数据`test.json`: |
| 160 | + |
| 161 | +```cs |
| 162 | +[{ |
| 163 | + "name": "Tom", "age": 16, "gender": 0, |
| 164 | + "hobby": [ |
| 165 | + "cooking", "sing" |
| 166 | + ] |
| 167 | +},{ |
| 168 | + "name": "Tony", "age": 16, "gender": 0, |
| 169 | + "hobby": [ |
| 170 | + "game", "dance" |
| 171 | + ] |
| 172 | +},{ |
| 173 | + "name": "Andy", "age": 20, "gender": 1, |
| 174 | + "hobby": [ |
| 175 | + "draw", "sing" |
| 176 | + ] |
| 177 | +},{ |
| 178 | + "name": "Kun", "age": 26, "gender": 1, |
| 179 | + "hobby": [ |
| 180 | + "sing", "dance", "rap", "basketball" |
| 181 | + ] |
| 182 | +}] |
| 183 | +// 将其加载到程序中: |
| 184 | +var json_src = STJson.Deserialize(System.IO.File.ReadAllText("./test.json")); |
| 185 | +``` |
| 186 | +通过以下方式可以构建一个`STJsonPath`: |
| 187 | +```cs |
| 188 | +// var jp = new STJsonPath("$[0]name"); |
| 189 | +// var jp = new STJsonPath("$[0].name"); |
| 190 | +var jp = new STJsonPath("[0]'name'"); // 以上方式均可以使用 $不是必须的 |
| 191 | +Console.WriteLine(jp.Select(json_src)); |
| 192 | +/******************************************************************************* |
| 193 | + * [output] * |
| 194 | + *******************************************************************************/ |
| 195 | +["Tom"] |
| 196 | +``` |
| 197 | +当然在`STJson`中的扩展函数中已经集成`STJsonPath`,可以通过下面的方式直接使用: |
| 198 | +```cs |
| 199 | +// var jp = new STJsonPath("[0].name"); |
| 200 | +// Console.WriteLine(json_src.Select(jp)); |
| 201 | +Console.WriteLine(json_src.Select("[0].name")); // 内部动态构建 STJsonPath |
| 202 | +/******************************************************************************* |
| 203 | + * [output] * |
| 204 | + *******************************************************************************/ |
| 205 | +["Tom"] |
| 206 | +``` |
| 207 | + |
| 208 | +普通模式(默认方式): |
| 209 | +```cs |
| 210 | +Console.WriteLine(json_src.Select("..name", STJsonPathSelectMode.ItemOnly).ToString(4)); |
| 211 | +/******************************************************************************* |
| 212 | + * [output] * |
| 213 | + *******************************************************************************/ |
| 214 | +[ |
| 215 | + "Tom", "Tony", "Andy", "Kun" |
| 216 | +] |
| 217 | +``` |
| 218 | +路径模式: |
| 219 | +```cs |
| 220 | +Console.WriteLine(json_src.Select("..name", STJsonPathSelectMode.ItemWithPath).ToString(4)); |
| 221 | +/******************************************************************************* |
| 222 | + * [output] * |
| 223 | + *******************************************************************************/ |
| 224 | +[ |
| 225 | + { |
| 226 | + "path": [ |
| 227 | + 0, "name" |
| 228 | + ], |
| 229 | + "item": "Tom" |
| 230 | + }, { |
| 231 | + "path": [ |
| 232 | + 1, "name" |
| 233 | + ], |
| 234 | + "item": "Tony" |
| 235 | + }, { |
| 236 | + "path": [ |
| 237 | + 2, "name" |
| 238 | + ], |
| 239 | + "item": "Andy" |
| 240 | + }, { |
| 241 | + "path": [ |
| 242 | + 3, "name" |
| 243 | + ], |
| 244 | + "item": "Kun" |
| 245 | + } |
| 246 | +] |
| 247 | +``` |
| 248 | +保持结构: |
| 249 | +```cs |
| 250 | +Console.WriteLine(json_src.Select("..name", STJsonPathSelectMode.KeepStructure).ToString(4)); |
| 251 | +/******************************************************************************* |
| 252 | + * [output] * |
| 253 | + *******************************************************************************/ |
| 254 | +[ |
| 255 | + { |
| 256 | + "name": "Tom" |
| 257 | + }, { |
| 258 | + "name": "Tony" |
| 259 | + }, { |
| 260 | + "name": "Andy" |
| 261 | + }, { |
| 262 | + "name": "Kun" |
| 263 | + } |
| 264 | +] |
| 265 | +``` |
| 266 | + |
| 267 | +当然上面仅仅是介绍了一个基本用法,还有很多丰富的功能并没有介绍到。如果你时间充足可以直接阅读教程: |
| 268 | + |
| 269 | +CN: [https://DebugST.github.io/STJson/tutorial_cn.html](https://DebugST.github.io/STJson/tutorial_cn.html) |
| 270 | + |
| 271 | +EN: [https://DebugST.github.io/STJson/tutorial_en.html](https://DebugST.github.io/STJson/tutorial_en.html) |
| 272 | + |
| 273 | +# 联系作者 |
| 274 | + |
| 275 | +* TG: DebugST |
| 276 | +* QQ: 2212233137 |
| 277 | + |
0 commit comments