Skip to content

Commit c9aa8f6

Browse files
committed
update readme
1 parent b60ef83 commit c9aa8f6

File tree

1 file changed

+201
-6
lines changed

1 file changed

+201
-6
lines changed

README.md

Lines changed: 201 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ A Json serialization operation calling library. Convert between objects and Json
2020
|char|String|char ch = 0|
2121
|string|String|string str = 0|
2222
|DateTime|String|DateTime dt = DateTime.Now|
23+
|enum|Number|XXX.XXX|
2324
|Array|Array|int[],int[,],int[][]|
24-
|ICollection|Array|List<int>,List<object>|
25-
|IDectionary|Object|Dictionary<int,string>|
25+
|ICollection|Array|List,HashSet|
26+
|IDectionary|Object|Dictionary|
2627
|Other object|Object|Recursively reflect all fields.|
2728

2829
# Simple object
@@ -69,7 +70,7 @@ Console.WriteLine("[Student] - " + stu.Hobby[0]);
6970

7071
Output
7172

72-
```
73+
```json
7374
{
7475
"Name": "Tom",
7576
"Age": 100,
@@ -99,7 +100,7 @@ Console.WriteLine(stu.Hobby[0]);
99100

100101
json = new STJson();
101102
json.SetItem("Name", "Jack");
102-
// attach to a exists project, but stu is struct.
103+
// attach to a exists project.
103104
STJson.Deserialize(json, stu);
104105
Console.WriteLine(stu.Name);
105106
```
@@ -108,7 +109,7 @@ Output
108109

109110
```
110111
Cooking
111-
Andy
112+
Jack
112113
```
113114

114115
# Use Attribute
@@ -122,6 +123,200 @@ Console.WriteLine(str);
122123

123124
Output
124125

125-
```
126+
```json
126127
{"Hobby":["Cooking","Sports"]}
128+
```
129+
130+
# A complex object.
131+
---
132+
```cs
133+
public enum TestObjectEnum { Enum1, Enum2, Enum3 }
134+
135+
[STJson(STJsonSerilizaModel.All)]
136+
public class TestObject
137+
{
138+
private string m_private_string = "private_string_obj";
139+
public static string Name { get { return "TestObject"; } }
140+
public string STR { get; set; }
141+
public int INT { get; set; }
142+
public double DB { get; set; }
143+
public decimal DEC { get; set; }
144+
public TestObjectEnum Enum { get; set; }
145+
public DateTime TIME { get; set; }
146+
public TestObject_1 OBJ { get; set; }
147+
[STJsonProperty]
148+
public TestObject_1[] ARR_OBJ { get; set; }
149+
150+
public void Function() { Console.WriteLine(m_private_string); }
151+
}
152+
153+
public class TestObject_1
154+
{
155+
private string m_private_string = "private_string_obj_1";
156+
public static string Name { get { return "TestObject_1"; } }
157+
public int[] ARR_INT { get; set; }
158+
public List<int> LST_INT { get; set; }
159+
public List<object> LST_OBJ { get; set; }
160+
public Dictionary<string, int> DIC_STR_INT { get; set; }
161+
public Dictionary<string, object> DIC_STR_OBJ { get; set; }
162+
public int[] ARR_INT_READONLY { get; private set; }
163+
public int[][] ARR_ARR_INT { get; set; }
164+
public int[,] ARRARR_INT { get; set; }
165+
public int[,,] ARRARRARR_INT { get; set; }
166+
public List<int> LST_INT_READONLY { get; private set; }
167+
public int ARR_INT_READONLY_LENGTH { get { return ARR_INT_READONLY.Length; } }
168+
169+
public TestObject_1() {
170+
ARRARR_INT = new int[2, 3];
171+
ARRARRARR_INT = new int[2, 3, 4];
172+
ARR_ARR_INT = new int[5][];
173+
for (int i = 0; i < ARR_ARR_INT.Length; i++) {
174+
ARR_ARR_INT[i] = new int[i];
175+
}
176+
ARR_INT_READONLY = new int[] { 101, 202 };
177+
LST_INT_READONLY = new List<int>() { 100, 200 };
178+
}
179+
180+
public void Function() { Console.WriteLine(m_private_string); }
181+
}
182+
183+
public class STJsonTest
184+
{
185+
public static TestObject CreateTestObject() {
186+
return new TestObject() {
187+
STR = "string",
188+
INT = 10,
189+
DB = -0.123,
190+
DEC = 0.123M,
191+
TIME = DateTime.Now,
192+
OBJ = new TestObject_1() {
193+
ARR_INT = new int[] { 1, 2, 3 },
194+
LST_INT = new List<int>() { 11, 12, 13 },
195+
LST_OBJ = new List<object>() { true, false, 123, "string", new { DY_STR = "str" }, -0.0005 },
196+
DIC_STR_INT = new Dictionary<string, int>() {
197+
{"key_1", 10},
198+
{"key_2", 10}
199+
},
200+
DIC_STR_OBJ = new Dictionary<string, object>(){
201+
{"key_1", true},
202+
{"key_2", false},
203+
{"key_3", null},
204+
{"key_4", "string"},
205+
{"key_5", 1234},
206+
{"key_6", new {DY_INT = 100}},
207+
}
208+
},
209+
ARR_OBJ = new TestObject_1[]{
210+
null, new TestObject_1(){ARR_INT = new int[]{60, 70}}, null
211+
}
212+
};
213+
}
214+
}
215+
```
216+
217+
Code
218+
219+
```cs
220+
var obj = STJsonTest.CreateTestObject();
221+
222+
var str_a = STJson.Serialize(obj);
223+
var aaa = STJson.Deserialize<TestObject>(str_a);
224+
var str_b = STJson.Serialize(aaa);
225+
var bool_a = str_a == str_b;
226+
Console.WriteLine(bool_a);
227+
Console.WriteLine(STJson.Format(str_a, 4));
228+
```
229+
230+
Output
231+
232+
```json
233+
true
234+
{
235+
"STR": "string",
236+
"INT": 10,
237+
"DB": -0.123,
238+
"DEC": 0.123,
239+
"Enum": 0,
240+
"TIME": "2022-09-21T12:24:32.1520390+08:00",
241+
"OBJ": {
242+
"ARR_INT": [1, 2, 3],
243+
"LST_INT": [11, 12, 13],
244+
"LST_OBJ": [true, false, 123, "string", {
245+
"DY_STR": "str"
246+
}, -0.0005],
247+
"DIC_STR_INT": {
248+
"key_1": 10,
249+
"key_2": 10
250+
},
251+
"DIC_STR_OBJ": {
252+
"key_1": true,
253+
"key_2": false,
254+
"key_3": null,
255+
"key_4": "string",
256+
"key_5": 1234,
257+
"key_6": {
258+
"DY_INT": 100
259+
}
260+
},
261+
"ARR_INT_READONLY": [101, 202],
262+
"ARR_ARR_INT": [
263+
[],
264+
[0],
265+
[0, 0],
266+
[0, 0, 0],
267+
[0, 0, 0, 0]
268+
],
269+
"ARRARR_INT": [
270+
[0, 0, 0],
271+
[0, 0, 0]
272+
],
273+
"ARRARRARR_INT": [
274+
[
275+
[0, 0, 0, 0],
276+
[0, 0, 0, 0],
277+
[0, 0, 0, 0]
278+
],
279+
[
280+
[0, 0, 0, 0],
281+
[0, 0, 0, 0],
282+
[0, 0, 0, 0]
283+
]
284+
],
285+
"LST_INT_READONLY": [100, 200],
286+
"ARR_INT_READONLY_LENGTH": 2
287+
},
288+
"ARR_OBJ": [null, {
289+
"ARR_INT": [60, 70],
290+
"LST_INT": null,
291+
"LST_OBJ": null,
292+
"DIC_STR_INT": null,
293+
"DIC_STR_OBJ": null,
294+
"ARR_INT_READONLY": [101, 202],
295+
"ARR_ARR_INT": [
296+
[],
297+
[0],
298+
[0, 0],
299+
[0, 0, 0],
300+
[0, 0, 0, 0]
301+
],
302+
"ARRARR_INT": [
303+
[0, 0, 0],
304+
[0, 0, 0]
305+
],
306+
"ARRARRARR_INT": [
307+
[
308+
[0, 0, 0, 0],
309+
[0, 0, 0, 0],
310+
[0, 0, 0, 0]
311+
],
312+
[
313+
[0, 0, 0, 0],
314+
[0, 0, 0, 0],
315+
[0, 0, 0, 0]
316+
]
317+
],
318+
"LST_INT_READONLY": [100, 200],
319+
"ARR_INT_READONLY_LENGTH": 2
320+
}, null]
321+
}
127322
```

0 commit comments

Comments
 (0)