Skip to content

Commit e0d036f

Browse files
committed
v2.0
1 parent 1a883ca commit e0d036f

File tree

17 files changed

+262
-101
lines changed

17 files changed

+262
-101
lines changed

Src/STLib.Json.Test/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public enum Gender
2222
}
2323

2424
static void Main(string[] args) {
25-
// Console.WriteLine(STJsonPath.GetBuildInFunctionList().ToString(4));
26-
2725
DataTable dt = new DataTable();
2826
dt.Columns.Add("name");
2927
dt.Columns.Add("age", typeof(int));

Src/STLib.Json/Converter/FPInfo.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@ internal class FPInfo
2121

2222
public Type Type { get; private set; }
2323

24-
public bool CanSetValue {
25-
get {
26-
if (m_f_info != null) return true;
27-
return m_p_info.GetSetMethod() != null;
28-
}
29-
}
24+
public bool CanSetValue { get; private set; }
3025

31-
public bool CanGetValue {
32-
get {
33-
if (m_f_info != null) return true;
34-
return m_p_info.GetGetMethod() != null;
35-
}
36-
}
26+
public bool CanGetValue { get; private set; }
27+
28+
//public bool CanSetValue {
29+
// get {
30+
// if (m_f_info != null) return true;
31+
// return m_p_info.GetSetMethod() != null;
32+
// }
33+
//}
34+
35+
//public bool CanGetValue {
36+
// get {
37+
// if (m_f_info != null) return true;
38+
// return m_p_info.GetGetMethod() != null;
39+
// }
40+
//}
3741

3842
public object GetValue(object obj) {
3943
if (m_f_info != null) {
@@ -88,13 +92,23 @@ public static List<FPInfo> GetFPInfo(Type type) {
8892
fp.m_f_info = v;
8993
fp.Name = v.Name;
9094
fp.Type = v.FieldType;
95+
fp.CanGetValue = true;
96+
fp.CanSetValue = true;
9197
lst.Add(fp);
9298
}
9399
foreach (var v in ps) {
94100
FPInfo fp = new FPInfo();
95101
fp.m_p_info = v;
96102
fp.Name = v.Name;
97103
fp.Type = v.PropertyType;
104+
fp.CanGetValue = v.GetGetMethod() != null;
105+
fp.CanSetValue = v.GetSetMethod() != null;
106+
if (fp.CanGetValue) {
107+
fp.CanGetValue = v.GetGetMethod().GetParameters().Length == 0;
108+
}
109+
if (fp.CanSetValue) {
110+
fp.CanSetValue = v.GetSetMethod().GetParameters().Length == 0;
111+
}
98112
lst.Add(fp);
99113
}
100114
for (int i = 0; i < lst.Count; i++) {

Src/STLib.Json/Converter/ObjectToSTJson.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public static STJson Get(object obj, STJsonSetting setting) {
8686
//#endif
8787
}
8888
foreach (var p in fps) {
89+
if (!p.CanGetValue) {
90+
continue;
91+
}
8992
switch (serilizaModel) {
9093
case STJsonSerilizaMode.All:
9194
break;

Src/STLib.Json/Converter/ObjectToString.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace STLib.Json
88
{
99
internal class ObjectToString
1010
{
11+
private static Type m_type_json = typeof(STJson);
1112
private static Type m_type_attr_stjson = typeof(STJsonAttribute);
1213

1314
public static void Get(StringBuilder sb, object obj, STJsonSetting setting) {
@@ -16,6 +17,10 @@ public static void Get(StringBuilder sb, object obj, STJsonSetting setting) {
1617
return;
1718
}
1819
var t = obj.GetType();
20+
if (t == m_type_json) {
21+
sb.Append(obj.ToString());
22+
return;
23+
}
1924
bool bProcessed = true;
2025
STJsonConverter converter = STJson.GetConverter(t);
2126
if (converter != null) {
@@ -96,6 +101,9 @@ public static void Get(StringBuilder sb, object obj, STJsonSetting setting) {
96101
}
97102
sb.Append('{');
98103
foreach (var p in fps) {
104+
if (!p.CanGetValue) {
105+
continue;
106+
}
99107
switch (serilizaModel) {
100108
case STJsonSerilizaMode.All:
101109
break;

Src/STLib.Json/Converter/STJsonBuildInConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ public override string ObjectToString(Type t, object obj, ref bool bProcessed) {
441441

442442
public override STJson ObjectToJson(Type t, object obj, ref bool bProcessed) {
443443
STJson json = new STJson();
444-
json.SetValue((obj == null ? "1970-01-01T00:00:00.0000+00:00" : ((DateTime)obj).ToString("O")));
444+
//json.SetValue((obj == null ? "1970-01-01T00:00:00.0000+00:00" : ((DateTime)obj).ToString("O")));
445+
json.SetValue(Convert.ToDateTime(obj));
445446
return json;
446447
}
447448

Src/STLib.Json/Converter/STJsonToObject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public static void SetObject(STJson json, object obj, STJsonSetting setting) {
3838
var fps = FPInfo.GetFPInfo(t);// t.GetProperties();
3939
foreach (var p in fps) {
4040
if (json[p.KeyName] == null) continue;
41+
if (!p.CanSetValue) {
42+
continue;
43+
}
4144
switch (serilizaModel) {
4245
case STJsonSerilizaMode.All:
4346
break;

Src/STLib.Json/DevLog.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Fixed JsonPath error for [?()]
2+
Fixed some string serialization cannot be parsed in other languages.
3+
4+
[1.0.4][2023-11-27]
5+
--------------------------------------------------
6+
Fixed parsing string error for ["....\\"]
7+
Fixed parsing number error for [12E-12]
8+
Changed STJsonPathCallBack
9+
10+
[1.0.3][2023-11-10]
11+
--------------------------------------------------
12+
Fixed DateTime type sorting error.
13+
Fixed parsing scientific notation string error.
14+
Fixed STJson.GetValue<object>() error.
15+
16+
[1.0.1][2023-07-24]
17+
--------------------------------------------------
18+
Fixed STJson.GetValue<T>() error.
19+
20+
[1.0.0][2023-06-04]
21+
--------------------------------------------------
22+
Publish project.
23+
Support Json serialization and deserialization.
24+
Support JsonPath syntax.
25+
Simple data aggregation processing.

Src/STLib.Json/ExtensionMethods/STJsonExtension.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public static T GetValue<T>(this STJson json, STJsonPath jsonPath) {
160160
return (T)value;
161161
}
162162
}
163-
return (T)j.Value;
163+
return STJson.Deserialize<T>(j);
164+
//return (T)j.Value;
164165
}
165166

166167
public static T GetValue<T>(this STJson json, string strJsonPath, T defaultValue) {
@@ -205,7 +206,7 @@ public static bool GetValue<T>(this STJson json, STJsonPath jsonPath, T defaultV
205206
return true;
206207
}
207208
}
208-
result = (T)j.Value;
209+
result = STJson.Deserialize<T>(j);// (T)j.Value;
209210
return true;
210211
} catch {
211212
return false;
@@ -339,9 +340,9 @@ public static STJson Select(this STJson json, string strJsonPath, STJsonPathCall
339340
return new STJsonPath(strJsonPath).Select(json, STJsonPathSelectMode.ItemOnly, callBack);
340341
}
341342

342-
public static STJson Select(this STJson json, string strJsonPath, STJsonPathCallBackVoid callBack) {
343-
return new STJsonPath(strJsonPath).Select(json, STJsonPathSelectMode.ItemOnly, callBack);
344-
}
343+
//public static STJson Select(this STJson json, string strJsonPath, STJsonPathCallBackVoid callBack) {
344+
// return new STJsonPath(strJsonPath).Select(json, STJsonPathSelectMode.ItemOnly, callBack);
345+
//}
345346

346347
public static STJson Select(this STJson json, string strJsonPath, STJsonPathSelectMode model) {
347348
return new STJsonPath(strJsonPath).Select(json, model, null);
@@ -351,9 +352,9 @@ public static STJson Select(this STJson json, string strJsonPath, STJsonPathSele
351352
return new STJsonPath(strJsonPath).Select(json, model, callBack);
352353
}
353354

354-
public static STJson Select(this STJson json, string strJsonPath, STJsonPathSelectMode model, STJsonPathCallBackVoid callBack) {
355-
return new STJsonPath(strJsonPath).Select(json, model, callBack);
356-
}
355+
//public static STJson Select(this STJson json, string strJsonPath, STJsonPathSelectMode model, STJsonPathCallBackVoid callBack) {
356+
// return new STJsonPath(strJsonPath).Select(json, model, callBack);
357+
//}
357358

358359
public static STJson Select(this STJson json, STJsonPath jsonPath) {
359360
return jsonPath.Select(json, STJsonPathSelectMode.ItemWithPath, null);
@@ -363,9 +364,9 @@ public static STJson Select(this STJson json, STJsonPath jsonPath, STJsonPathCal
363364
return jsonPath.Select(json, STJsonPathSelectMode.ItemOnly, callBack);
364365
}
365366

366-
public static STJson Select(this STJson json, STJsonPath jsonPath, STJsonPathCallBackVoid callBack) {
367-
return jsonPath.Select(json, STJsonPathSelectMode.ItemOnly, callBack);
368-
}
367+
//public static STJson Select(this STJson json, STJsonPath jsonPath, STJsonPathCallBackVoid callBack) {
368+
// return jsonPath.Select(json, STJsonPathSelectMode.ItemOnly, callBack);
369+
//}
369370

370371
public static STJson Select(this STJson json, STJsonPath jsonPath, STJsonPathSelectMode model) {
371372
return jsonPath.Select(json, model, null);
@@ -375,9 +376,9 @@ public static STJson Select(this STJson json, STJsonPath jsonPath, STJsonPathSel
375376
return jsonPath.Select(json, model, callBack);
376377
}
377378

378-
public static STJson Select(this STJson json, STJsonPath jsonPath, STJsonPathSelectMode model, STJsonPathCallBackVoid callBack) {
379-
return jsonPath.Select(json, model, callBack);
380-
}
379+
//public static STJson Select(this STJson json, STJsonPath jsonPath, STJsonPathSelectMode model, STJsonPathCallBackVoid callBack) {
380+
// return jsonPath.Select(json, model, callBack);
381+
//}
381382

382383

383384
public static STJson SelectFirst(this STJson json, string strJsonPath) {
@@ -619,7 +620,7 @@ private static MergeSortInfo[] GetMergeSortInfos(STJson json, STJson jsonSort) {
619620
d_temp = item.GetValue<bool>() ? 1 : 0;
620621
break;
621622
case STJsonValueType.Datetime:
622-
d_temp = Convert.ToDouble(item.GetValue<DateTime>());
623+
d_temp = Convert.ToDouble(item.GetValue<DateTime>().Ticks);
623624
break;
624625
case STJsonValueType.String:
625626
d_temp = item.Value == null ? 0 : item.Value.ToString().Length;
@@ -882,10 +883,7 @@ public static STJson Sum(this STJson json, params STJsonPath[] paths) {
882883
if (json_item == null) {
883884
continue;
884885
}
885-
if (json_item.ValueType != STJsonValueType.Long) {
886-
continue;
887-
}
888-
if (json_item.ValueType != STJsonValueType.Double) {
886+
if (json_item.ValueType != STJsonValueType.Long && json_item.ValueType != STJsonValueType.Double) {
889887
continue;
890888
}
891889
lst_counter[i]++;

Src/STLib.Json/JsonParser/STJsonTokenizer.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Runtime.CompilerServices;
23
using System.Text;
34

45
using ME = STLib.Json.STJsonTokenizer;
@@ -110,27 +111,49 @@ public static List<Token> GetTokens(string strJson) {
110111
}
111112

112113
private static Token GetNumber(string strText, int nIndex) {
113-
bool bDot = false;
114+
bool bDot = false, bE = false;
114115
Token token = new Token() {
115116
Index = nIndex,
116117
Type = TokenType.Long
117118
};
118119
for (int i = nIndex; i < strText.Length; i++) {
119120
var c = strText[i];
120-
if (c == '-') {
121+
if (c == '-') { // -123
121122
if (i != nIndex) {
122123
throw new STJsonParseException(i, "Invalid char. Index: " + i);
123124
}
124125
continue;
125126
}
126-
if (c == '.') {
127-
if (bDot || i == nIndex || (strText[nIndex] == '-' && i == nIndex + 1)) {
127+
if (c == '.') { // 1.23
128+
if (bDot || bE || i == nIndex || strText[i - 1] == '-') {
128129
throw new STJsonParseException(i, "Invalid char. Index: " + i);
129130
}
130131
bDot = true;
131132
token.Type = TokenType.Double;
132133
continue;
133134
}
135+
if (c == 'E' || c == 'e') { // 12E+3 Regex:-?\d+(\.\d+)?E[+-]?(\d+)
136+
if (bE || i == nIndex || strText[i - 1] == '-' || strText[i - 1] == '.') {
137+
throw new STJsonParseException(i, "Invalid char. Index: " + i);
138+
}
139+
if (i + 2 >= strText.Length) {
140+
throw new STJsonParseException(i, "Invalid char. Index: " + i);
141+
}
142+
c = strText[++i];
143+
if (c == '-' || c == '+') { // E+ | E-
144+
c = strText[++i];
145+
}
146+
//if (strText[++i] != '+') { // E+
147+
// throw new STJsonParseException(i + 1, "Invalid char. Index: " + (i + 1));
148+
//}
149+
bE = true;
150+
//c = strText[++i];
151+
if (c < '0' && '9' < c) { // E+[0-9]
152+
throw new STJsonParseException(i + 2, "Invalid char. Index: " + (i + 2));
153+
}
154+
token.Type = TokenType.Double;
155+
continue;
156+
}
134157
if ('0' <= c && c <= '9') continue;
135158
token.Value = strText.Substring(nIndex, i - nIndex);
136159
return token;
@@ -152,7 +175,7 @@ private static Token GetKeyWord(string strText, int nIndex, string strKeyWord) {
152175
}
153176

154177
private static Token GetString(string strText, int nIndex) {
155-
char ch_last = '\0';
178+
//char ch_last = '\0';
156179
Token token = new Token() {
157180
Index = nIndex,
158181
Type = TokenType.String
@@ -162,11 +185,20 @@ private static Token GetString(string strText, int nIndex) {
162185
}
163186
for (int i = nIndex + 1; i < strText.Length; i++) {
164187
var ch = strText[i];
165-
if (ch == '"' && ch_last != '\\') {
188+
if (ch == '\\') {
189+
i++;
190+
continue;
191+
}
192+
if (ch == '"') {
166193
token.Value = strText.Substring(nIndex + 1, i - nIndex - 1);
167194
return token;
168195
}
169-
ch_last = ch;
196+
//if (ch_last == '\\') ch_last = '\0';
197+
//if (ch == '"' && ch_last != '\\') {
198+
// token.Value = strText.Substring(nIndex + 1, i - nIndex - 1);
199+
// return token;
200+
//}
201+
//ch_last = ch;
170202
}
171203
throw new STJsonParseException(nIndex, "Can not get a string. Index: " + nIndex);
172204
}

Src/STLib.Json/JsonPathParser/STJsonPath.DataType.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,32 @@ public enum STJsonPathSelectMode
88
ItemOnly, ItemWithPath, KeepStructure
99
}
1010

11-
public struct STJsonPathCallBackResult
11+
public class STJsonPathCallBackArgs
1212
{
13-
public bool Selected;
14-
public STJson Json;
15-
16-
public STJsonPathCallBackResult(bool selected, STJson json) {
17-
this.Selected = selected;
18-
this.Json = json;
19-
}
13+
public bool Selected { get; set; }
14+
public STJson Path { get; internal set; }
15+
public STJson Json { get; set; }
2016
}
2117

18+
//public struct STJsonPathCallBackResult
19+
//{
20+
// public bool Selected;
21+
// public STJson Json;
22+
23+
// public STJsonPathCallBackResult(bool selected, STJson json) {
24+
// this.Selected = selected;
25+
// this.Json = json;
26+
// }
27+
//}
28+
2229
public struct SelectSetting
2330
{
2431
public STJson Root;
2532
public STJsonPathSelectMode Mode;
2633
public Stack<object> Path;
27-
public STJsonPathCallBackVoid CallbackVoid;
28-
public STJsonPathCallBack CallbackReturn;
34+
//public STJsonPathCallBackVoid CallbackVoid;
35+
//public STJsonPathCallBack CallbackReturn;
36+
public STJsonPathCallBack Callback;
2937

3038
public static SelectSetting Create() {
3139
return new SelectSetting() {
@@ -34,8 +42,10 @@ public static SelectSetting Create() {
3442
}
3543
}
3644

37-
public delegate void STJsonPathCallBackVoid(STJson path, STJson json);
38-
public delegate STJsonPathCallBackResult STJsonPathCallBack(STJson path, STJson json);
45+
46+
public delegate void STJsonPathCallBack(STJsonPathCallBackArgs args);
47+
//public delegate void STJsonPathCallBackVoid(STJson path, STJson json);
48+
//public delegate STJsonPathCallBackResult STJsonPathCallBack(STJson path, STJson json);
3949
public delegate STJson STJsonPathCustomFuncHander(STJsonPathExpFuncArg[] args);
4050

4151
public enum STJsonPathExpFuncArgType

0 commit comments

Comments
 (0)