Skip to content

Commit 0d7c882

Browse files
committed
🐛解决DynamicObjectExt动态对象中DateTime数据序列化反序列化问题
1 parent 2a71656 commit 0d7c882

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ Xmtool是一个基于.NetCore的常用功能集成工具类库,目的是做成
1919
##### Package Manager
2020

2121
```shell
22-
Install-Package Xmtool -Version 2.1.1
22+
Install-Package Xmtool -Version 2.1.2
2323
```
2424

2525
##### .NET CLI
2626

2727
```shell
28-
dotnet add package Xmtool --version 2.1.1
28+
dotnet add package Xmtool --version 2.1.2
2929
```
3030

3131
##### PackageReference
3232

3333
```xml
34-
<PackageReference Include="Xmtool" Version="2.1.1" />
34+
<PackageReference Include="Xmtool" Version="2.1.2" />
3535
```
3636

3737
##### Paket CLI
3838

3939
```shell
40-
paket add Xmtool --version 2.1.1
40+
paket add Xmtool --version 2.1.2
4141
```
4242

4343

Source/DateTimeTool.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,24 @@ public long GetUtcTimestamp10()
2121
return (long)ts.TotalSeconds;
2222
}
2323

24+
public long GetUtcTimestamp10(DateTime datetime)
25+
{
26+
TimeSpan ts = datetime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
27+
return (long)ts.TotalSeconds;
28+
}
29+
2430
public long GetUtcTimestamp13()
2531
{
2632
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
2733
return (long)ts.TotalMilliseconds;
2834
}
2935

36+
public long GetUtcTimestamp13(DateTime datetime)
37+
{
38+
TimeSpan ts = datetime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
39+
return (long)ts.TotalMilliseconds;
40+
}
41+
3042
public DateTime GetUtcDateTimeFromUtcTimestamp10(long ts)
3143
{
3244
DateTime st = new DateTime(1970, 1, 1, 0, 0, 0);

Source/DynamicObject/DynamicObjectExt.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ private string SerializeValue(dynamic value)
278278
{
279279
sbResult.Append(value.ToString().ToLower());
280280
}
281+
else if (_typ == typeof(DateTime))
282+
{
283+
sbResult.Append(string.Concat("\"", value.ToString("s"), "\""));
284+
}
281285
else
282286
{
283287
sbResult.Append(value.ToString());

Source/Json/JsonConfigParser.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Text;
66
using System.Text.Json;
7+
using System.Text.RegularExpressions;
78

89
namespace CodeM.Common.Tools.Json
910
{
@@ -56,6 +57,7 @@ public dynamic Parse(string jsonStr = null)
5657
return result;
5758
}
5859

60+
private static Regex reDateTime = new Regex("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$");
5961
private void BindConfigObject(DynamicObjectExt configObj, JsonElement element, string key = null)
6062
{
6163
if (configObj == null)
@@ -138,7 +140,15 @@ private void BindConfigObject(DynamicObjectExt configObj, JsonElement element, s
138140
case JsonValueKind.String:
139141
if (!string.IsNullOrWhiteSpace(key))
140142
{
141-
configObj.TrySetValue(key, element.GetString());
143+
string value = element.GetString();
144+
if (reDateTime.IsMatch(value))
145+
{
146+
configObj.TrySetValue(key, DateTime.Parse(value));
147+
}
148+
else
149+
{
150+
configObj.TrySetValue(key, value);
151+
}
142152
}
143153
break;
144154
case JsonValueKind.Number:

Source/Source.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<AssemblyName>Xmtool</AssemblyName>
99
<RootNamespace>CodeM.Common.Tools</RootNamespace>
1010
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
11-
<Version>2.1.1</Version>
11+
<Version>2.1.2</Version>
1212
<AssemblyVersion></AssemblyVersion>
1313
<FileVersion></FileVersion>
1414
<Authors>softwaiter</Authors>

0 commit comments

Comments
 (0)