Skip to content

Commit b60ef83

Browse files
committed
add readme
1 parent fb6fa88 commit b60ef83

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,127 @@
11
# STJson
22
A Json serialization operation calling library. Convert between objects and Json.
3+
4+
# Support DataType
5+
6+
|.Net|Json|e.g|
7+
|:---|:---|:---|
8+
|byte|Number|byte num = 0|
9+
|sbyte|Number|sbyte num = 0|
10+
|short|Number|short num = 0|
11+
|ushort|Number|ushort num = 0|
12+
|int|Number|int num = 0|
13+
|uint|Number|uint num = 0|
14+
|long|Number|long num = 0|
15+
|ulong|Number|ulong num = 0|
16+
|float|Number|float num = 0|
17+
|double|Number|double num = 0|
18+
|decimal|Number|decimal num = 0|
19+
|bool|Boolean|bool b = true|
20+
|char|String|char ch = 0|
21+
|string|String|string str = 0|
22+
|DateTime|String|DateTime dt = DateTime.Now|
23+
|Array|Array|int[],int[,],int[][]|
24+
|ICollection|Array|List<int>,List<object>|
25+
|IDectionary|Object|Dictionary<int,string>|
26+
|Other object|Object|Recursively reflect all fields.|
27+
28+
# Simple object
29+
30+
```cs
31+
[STJson(STJsonSerilizaModel.OnlyMarked)] // optional
32+
public class Student
33+
{
34+
public string Name;
35+
public int Age;
36+
public Gender Gender;
37+
[STJsonProperty] // optional
38+
public List<string> Hobby;
39+
}
40+
41+
public enum Gender
42+
{
43+
Male, Female
44+
}
45+
```
46+
47+
# Convert [string and object]
48+
49+
```cs
50+
var stu = new Student() {
51+
Name = "Tom",
52+
Age = 100,
53+
Gender = Gender.Male,
54+
Hobby = new List<string>() { "Game", "Sing" }
55+
};
56+
//string str = STJson.Serialize(stu);
57+
//str = STJson.Format(str);
58+
//str = STJson.Format(str, 4); // 4 -> the format space count.
59+
string str = STJson.Serialize(stu, 4);
60+
Console.WriteLine(str);
61+
STJson json = STJson.Deserialize(str);
62+
//Console.WriteLine("[STJson] - " + json["Hobby"][0]); // return STJson
63+
//Console.WriteLine("[STJson] - " + json["Hobby"][0].Value); // return object
64+
//Console.WriteLine("[STJson] - " + json["Hobby"][0].GetValue()); // return string
65+
Console.WriteLine("[STJson] - " + json["Hobby"][0].GetValue<string>());
66+
stu = STJson.Deserialize<Student>(str);
67+
Console.WriteLine("[Student] - " + stu.Hobby[0]);
68+
```
69+
70+
Output
71+
72+
```
73+
{
74+
"Name": "Tom",
75+
"Age": 100,
76+
"Gender": 0,
77+
"Hobby": [
78+
"Game", "Sing"
79+
]
80+
}
81+
[STJson] - Game
82+
[Student] - Game
83+
```
84+
85+
# Convert [STJson and object]
86+
87+
```cs
88+
var json = new STJson();
89+
json.SetKey("Name").SetValue("Andy");
90+
json.SetItem("Age", 200);
91+
json.SetItem("Gender", 1);
92+
json.SetItem("Hobby", STJson.CreateArray(
93+
STJson.FromValue("Cooking"),
94+
STJson.FromValue("Sports")
95+
));
96+
Console.WriteLine(json.ToString(4));
97+
var stu = STJson.Deserialize<Student>(json);
98+
Console.WriteLine(stu.Hobby[0]);
99+
100+
json = new STJson();
101+
json.SetItem("Name", "Jack");
102+
// attach to a exists project, but stu is struct.
103+
STJson.Deserialize(json, stu);
104+
Console.WriteLine(stu.Name);
105+
```
106+
107+
Output
108+
109+
```
110+
Cooking
111+
Andy
112+
```
113+
114+
# Use Attribute
115+
116+
```cs
117+
//str = STJson.Serialize(stu, ignoreAttribute: false);
118+
str = STJson.Serialize(stu, false);
119+
Console.WriteLine(str);
120+
/*ignoreAttribute default value is true*/
121+
```
122+
123+
Output
124+
125+
```
126+
{"Hobby":["Cooking","Sports"]}
127+
```

0 commit comments

Comments
 (0)