Skip to content

Commit

Permalink
🐛解决DynamicObjectExt动态对象中DateTime数据序列化反序列化问题
Browse files Browse the repository at this point in the history
  • Loading branch information
softwaiter committed Jan 16, 2024
1 parent 2a71656 commit 0d7c882
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ Xmtool是一个基于.NetCore的常用功能集成工具类库,目的是做成
##### Package Manager

```shell
Install-Package Xmtool -Version 2.1.1
Install-Package Xmtool -Version 2.1.2
```

##### .NET CLI

```shell
dotnet add package Xmtool --version 2.1.1
dotnet add package Xmtool --version 2.1.2
```

##### PackageReference

```xml
<PackageReference Include="Xmtool" Version="2.1.1" />
<PackageReference Include="Xmtool" Version="2.1.2" />
```

##### Paket CLI

```shell
paket add Xmtool --version 2.1.1
paket add Xmtool --version 2.1.2
```


Expand Down
12 changes: 12 additions & 0 deletions Source/DateTimeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,24 @@ public long GetUtcTimestamp10()
return (long)ts.TotalSeconds;
}

public long GetUtcTimestamp10(DateTime datetime)
{
TimeSpan ts = datetime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
return (long)ts.TotalSeconds;
}

public long GetUtcTimestamp13()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
return (long)ts.TotalMilliseconds;
}

public long GetUtcTimestamp13(DateTime datetime)
{
TimeSpan ts = datetime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
return (long)ts.TotalMilliseconds;
}

public DateTime GetUtcDateTimeFromUtcTimestamp10(long ts)
{
DateTime st = new DateTime(1970, 1, 1, 0, 0, 0);
Expand Down
4 changes: 4 additions & 0 deletions Source/DynamicObject/DynamicObjectExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ private string SerializeValue(dynamic value)
{
sbResult.Append(value.ToString().ToLower());
}
else if (_typ == typeof(DateTime))
{
sbResult.Append(string.Concat("\"", value.ToString("s"), "\""));
}
else
{
sbResult.Append(value.ToString());
Expand Down
12 changes: 11 additions & 1 deletion Source/Json/JsonConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace CodeM.Common.Tools.Json
{
Expand Down Expand Up @@ -56,6 +57,7 @@ public dynamic Parse(string jsonStr = null)
return result;
}

private static Regex reDateTime = new Regex("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$");
private void BindConfigObject(DynamicObjectExt configObj, JsonElement element, string key = null)
{
if (configObj == null)
Expand Down Expand Up @@ -138,7 +140,15 @@ private void BindConfigObject(DynamicObjectExt configObj, JsonElement element, s
case JsonValueKind.String:
if (!string.IsNullOrWhiteSpace(key))
{
configObj.TrySetValue(key, element.GetString());
string value = element.GetString();
if (reDateTime.IsMatch(value))
{
configObj.TrySetValue(key, DateTime.Parse(value));
}
else
{
configObj.TrySetValue(key, value);
}
}
break;
case JsonValueKind.Number:
Expand Down
2 changes: 1 addition & 1 deletion Source/Source.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AssemblyName>Xmtool</AssemblyName>
<RootNamespace>CodeM.Common.Tools</RootNamespace>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Version>2.1.1</Version>
<Version>2.1.2</Version>
<AssemblyVersion></AssemblyVersion>
<FileVersion></FileVersion>
<Authors>softwaiter</Authors>
Expand Down

0 comments on commit 0d7c882

Please sign in to comment.