Skip to content

Commit 6f7d67b

Browse files
authored
Merge pull request #20 from DanKE123abc/dev
v1.4.3
2 parents e7eada1 + 00f1d9f commit 6f7d67b

File tree

15 files changed

+336
-269
lines changed

15 files changed

+336
-269
lines changed

.github/workflows/myget-publish.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
$nupkg = Get-ChildItem -Path .\artifacts\*.nupkg | Select-Object -First 1
3333
dotnet nuget push $nupkg.FullName --source "https://www.myget.org/F/danke/api/v2/package" --api-key ${{ secrets.MYGET_API_KEY }}
3434
shell: pwsh
35-
35+
3636
- name: Get short commit hash
3737
id: commit-hash
3838
run: |
@@ -47,3 +47,4 @@ jobs:
4747
prerelease: true
4848
files: artifacts/*.nupkg
4949
body: "https://www.myget.org/feed/danke/package/nuget/DanKeJson"
50+

.github/workflows/nuget-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ jobs:
4444
- name: Publish to GitHub Release
4545
uses: softprops/action-gh-release@v2
4646
with:
47-
tag_name: v${{ steps.commit-hash.outputs.version }}re
48-
name: Release v${{ steps.commit-hash.outputs.version }}
47+
tag_name: v${{ steps.package-version.outputs.version }}re
48+
name: Release v${{ steps.package-version.outputs.version }}
4949
files: artifacts/*.nupkg
50-
body: "https://www.nuget.org/packages/DanKeJson/${{ steps.commit-hash.outputs.version }}"
50+
body: "https://www.nuget.org/packages/DanKeJson/${{ steps.package-version.outputs.version }}"
5151

5252
- name: Create new branch from main
5353
run: |

DanKeJson.Demo/Program.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
using DanKeJson;
22

3-
var test1 = JSONL.LineToData("C:\\Users\\15860\\Downloads\\lora_medical.jsonl",80);
3+
var text =
4+
"{\n \"model\": \"gpt-3.5-turbo\",\n \"messages\": [\n {\"role\": \"system\", \"content\": \"你是一个助手\"},\n {\"role\": \"user\", \"content\": \"你好!\"}\n ],\n \"temperature\": 0.7,\n \"max_tokens\": 100,\n \"stream\": false\n}";
45

5-
var test2 = JSONL.AllLineToData("C:\\Users\\15860\\Downloads\\lora_medical.jsonl");
6-
7-
var test3 = JSONL.ListToJson(test2);
6+
var json = JSON.ToData<JsonRoot>(text);
87

98
Console.WriteLine();
109

1110

11+
public class JsonRoot
12+
{
13+
[JsonProperty("model")]
14+
public string Model { get;set; }
15+
16+
public List<MessagesItem> messages { get;set; }
17+
18+
public double temperature { get;set; }
19+
20+
public int max_tokens { get;set; }
21+
22+
public bool stream { get;set; }
23+
}
24+
25+
public class MessagesItem
26+
{
27+
public string role { get;set; }
28+
29+
public string content { get;set; }
30+
}

DanKeJson.sln.DotSettings.user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fad1d58b8dd22123dfae3f2cf5d8b199992465c9c344d032f93a826d8e5539ff_003FRuntimeType_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
23

34
<s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String>
45
<s:String x:Key="/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue">C:\Program Files\dotnet\sdk\6.0.202\MSBuild.dll</s:String>

DanKeJson/DanKeJson.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<OutputPath>$(ProjectDir)lib\</OutputPath>
1010

1111
<!-- NuGet 包元数据 -->
12-
<Version>1.4.2</Version>
12+
<Version>1.4.3</Version>
1313
<PackageId>DanKeJson</PackageId>
1414
<Title>DanKeJson</Title>
1515
<Authors>DanKe</Authors>

DanKeJson/JSON.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Reflection;
65
using System.Text;
7-
using System.Linq;
86
using DanKeJson.Json;
97
using DanKeJson.Utils;
108

DanKeJson/JSON5.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Reflection;
65
using System.Text;
7-
using System.Linq;
86
using DanKeJson.Json5;
97
using DanKeJson.Utils;
108

DanKeJson/JSONL.cs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.IO;
32
using DanKeJson.Utils;
43

54
namespace DanKeJson
@@ -13,14 +12,15 @@ public class JSONL
1312
/// <returns>JsonData</returns>
1413
public static List<JsonData> AllLineToData(string filePath)
1514
{
16-
var jsonLines = new List<string>();
1715
var dataLines = new List<JsonData>();
1816
if (FilePathUtility.IsFilePath(filePath))
1917
{
20-
jsonLines = FileLineReader.ReadAllLines(filePath);
18+
var jsonLines = FileLineReader.ReadAllLines(filePath);
2119
foreach (var l in jsonLines)
2220
{
23-
dataLines.Add(JSON.ToData(l));
21+
// 添加空行检查
22+
if (string.IsNullOrWhiteSpace(l)) continue;
23+
dataLines.Add(JSON.ToData(l, true));
2424
}
2525
}
2626
return dataLines;
@@ -34,14 +34,15 @@ public static List<JsonData> AllLineToData(string filePath)
3434
/// <returns>JsonData</returns>
3535
public static List<T> AllLineToData<T>(string filePath) where T : class, new()
3636
{
37-
var jsonLines = new List<string>();
3837
var dataLines = new List<T>();
3938
if (FilePathUtility.IsFilePath(filePath))
4039
{
41-
jsonLines = FileLineReader.ReadAllLines(filePath);
40+
var jsonLines = FileLineReader.ReadAllLines(filePath);
4241
foreach (var l in jsonLines)
4342
{
44-
dataLines.Add(JSON.ToData<T>(l));
43+
// 添加空行检查
44+
if (string.IsNullOrWhiteSpace(l)) continue;
45+
dataLines.Add(JSON.ToData<T>(l, true));
4546
}
4647
}
4748
return dataLines;
@@ -55,14 +56,15 @@ public static List<JsonData> AllLineToData(string filePath)
5556
/// <returns>JsonData</returns>
5657
public static JsonData LineToData(string filePath, int lineNumber)
5758
{
58-
string jsonLine = null;
59-
JsonData dataLine = null;
6059
if (FilePathUtility.IsFilePath(filePath))
6160
{
62-
jsonLine = FileLineReader.ReadLine(filePath, lineNumber);
63-
dataLine = JSON.ToData(jsonLine);
61+
var jsonLine = FileLineReader.ReadLine(filePath, lineNumber);
62+
if (!string.IsNullOrWhiteSpace(jsonLine))
63+
{
64+
return JSON.ToData(jsonLine, true);
65+
}
6466
}
65-
return dataLine;
67+
return null;
6668
}
6769

6870
/// <summary>
@@ -74,17 +76,22 @@ public static JsonData LineToData(string filePath, int lineNumber)
7476
/// <returns>JsonData</returns>
7577
public static T LineToData<T>(string filePath, int lineNumber) where T : class, new()
7678
{
77-
string jsonLine = null;
78-
T dataLine = null;
7979
if (FilePathUtility.IsFilePath(filePath))
8080
{
81-
jsonLine = FileLineReader.ReadLine(filePath, lineNumber);
82-
dataLine = JSON.ToData<T>(jsonLine);
81+
var jsonLine = FileLineReader.ReadLine(filePath, lineNumber);
82+
if (!string.IsNullOrWhiteSpace(jsonLine))
83+
{
84+
return JSON.ToData<T>(jsonLine, true);
85+
}
8386
}
84-
return dataLine;
87+
return null;
8588
}
8689

87-
90+
/// <summary>
91+
/// Deserializing JsonData List to Json(String)
92+
/// </summary>
93+
/// <param name="jsonDataList">the JsonData list</param>
94+
/// <returns></returns>
8895
public static string ListToJson(List<JsonData> jsonDataList)
8996
{
9097
if (jsonDataList == null)
@@ -99,7 +106,14 @@ public static string ListToJson(List<JsonData> jsonDataList)
99106
return string.Join("\n", jsonLines);
100107
}
101108

102-
public static string ListToJson(List<object> jsonDataList)
109+
110+
/// <summary>
111+
/// Deserializing Object List to Json(String)
112+
/// </summary>
113+
/// <param name="jsonDataList">the JsonData list</param>
114+
/// <typeparam name="T">Class</typeparam>
115+
/// <returns></returns>
116+
public static string ListToJson<T>(List<T> jsonDataList) where T : class, new()
103117
{
104118
if (jsonDataList == null)
105119
{

DanKeJson/Json/Reader.cs

Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#pragma warning disable CS8603
1+
using System.Globalization;
2+
using System.Text;
3+
4+
#pragma warning disable CS8603
25

36
namespace DanKeJson.Json
47
{
@@ -24,25 +27,87 @@ public static JsonData ToString_Double(string json, ref int index)
2427
return null;
2528
}
2629

27-
int start = index++;
28-
29-
while (index < json.Length && json[index] != '\"')
30+
StringBuilder sb = new StringBuilder();
31+
int start = index; // 记录起始位置
32+
index++; // 跳过起始引号
33+
34+
while (index < json.Length)
3035
{
31-
index++;
32-
}
36+
char current = json[index];
37+
38+
if (current == '\"')
39+
{
40+
index++;
41+
return new JsonData(JsonData.Type.String)
42+
{
43+
json = sb.ToString()
44+
};
45+
}
3346

34-
if (index >= json.Length)
35-
{
36-
return null;
37-
}
47+
// 4. 处理转义序列
48+
if (current == '\\')
49+
{
50+
index++; // 跳过反斜杠
51+
if (index >= json.Length) break; // 防止越界
3852

39-
return new JsonData(JsonData.Type.String)
40-
{
41-
json = json[start..(++index)]
42-
};
53+
switch (json[index++]) // 处理转义字符并移动索引
54+
{
55+
case '\"':
56+
sb.Append('\"');
57+
break;
58+
case '\\':
59+
sb.Append('\\');
60+
break;
61+
case '/':
62+
sb.Append('/');
63+
break;
64+
case 'b':
65+
sb.Append('\b');
66+
break;
67+
case 'f':
68+
sb.Append('\f');
69+
break;
70+
case 'n':
71+
sb.Append('\n');
72+
break;
73+
case 'r':
74+
sb.Append('\r');
75+
break;
76+
case 't':
77+
sb.Append('\t');
78+
break;
79+
case 'u': // Unicode转义处理
80+
if (index + 4 <= json.Length)
81+
{
82+
string hex = json.Substring(index, 4);
83+
if (int.TryParse(hex, NumberStyles.HexNumber, null, out int code))
84+
{
85+
sb.Append((char)code);
86+
}
87+
88+
index += 4;
89+
}
90+
91+
break;
92+
default: // 未知转义序列保持原样
93+
sb.Append('\\');
94+
sb.Append(json[index - 1]);
95+
break;
96+
}
97+
}
98+
else
99+
{
100+
// 5. 普通字符直接添加
101+
sb.Append(current);
102+
index++;
103+
}
104+
}
43105

106+
// 6. 未找到结束引号(字符串未闭合)
107+
return null;
44108
}
45109

110+
46111
public static JsonData ToBoolean(string json, ref int index)
47112
{
48113
if (index < 0)
@@ -174,7 +239,7 @@ public static JsonData ToObject(string json, ref int index)
174239
{
175240
return null;
176241
}
177-
242+
178243
int start = index++;
179244
JsonData obj = new JsonData(JsonData.Type.Object);
180245
do
@@ -206,34 +271,34 @@ public static JsonData ToObject(string json, ref int index)
206271
{
207272
return null;
208273
}
209-
274+
210275
SkipWhiteSpace(json, ref index);
211276
if (json[index] != ':')
212277
{
213278
return null;
214279
}
215-
280+
216281
index++;
217-
282+
218283
SkipWhiteSpace(json, ref index);
219284
JsonData sub = Serializer.ProcessJson(json, ref index);
220285
if (sub == null)
221286
{
222287
return null;
223288
}
224-
289+
225290
obj[key] = sub;
226-
291+
227292
SkipWhiteSpace(json, ref index);
228-
293+
229294
} while (json[index] == ',');
230-
295+
231296
if (json[index] == '}')
232297
{
233298
obj.json = json[start..(++index)];
234299
return obj;
235300
}
236-
301+
237302
return null;
238303
}
239304

@@ -273,7 +338,7 @@ public static JsonData ToArray(string json, ref int index)
273338
arr.json = json[start..index];
274339
return arr;
275340
}
276-
341+
277342
public static JsonData ToNone(string json, ref int index)
278343
{
279344
if (index < 0)
@@ -311,4 +376,4 @@ public static int GetDistanceToNextComma(string json, ref int startIndex)
311376
return json.Length - startIndex - 1;
312377
}
313378
}
314-
}
379+
}

0 commit comments

Comments
 (0)