Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cf9d968

Browse files
committedApr 21, 2025·
fix(#113926): Fix invalid deserialization JsonNode with DateOnly value.
1 parent 86a8d37 commit cf9d968

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed
 

‎src/libraries/System.Text.Json/src/System.Text.Json.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
130130
<Compile Include="System\Text\Json\Serialization\Converters\Node\Generics\JsonValuePrimitiveByteConverter.cs" />
131131
<Compile Include="System\Text\Json\Serialization\Converters\Node\Generics\JsonValuePrimitiveCharConverter.cs" />
132132
<Compile Include="System\Text\Json\Serialization\Converters\Node\Generics\JsonValuePrimitiveConverter.cs" />
133+
<Compile Include="System\Text\Json\Serialization\Converters\Node\Generics\JsonValuePrimitiveDateOnlyConverter.cs" />
133134
<Compile Include="System\Text\Json\Serialization\Converters\Node\Generics\JsonValuePrimitiveDateTimeConverter.cs" />
134135
<Compile Include="System\Text\Json\Serialization\Converters\Node\Generics\JsonValuePrimitiveDateTimeOffsetConverter.cs" />
135136
<Compile Include="System\Text\Json\Serialization\Converters\Node\Generics\JsonValuePrimitiveDecimalConverter.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Text.Json.Nodes.Generics;
5+
using System.Text.Json.Serialization.Metadata;
6+
7+
namespace System.Text.Json.Serialization.Converters.Node.Generics
8+
{
9+
#if NET
10+
internal sealed class JsonValuePrimitiveDateOnlyConverter : JsonValuePrimitiveConverter<DateOnly>
11+
{
12+
public override JsonValuePrimitive<DateOnly>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
13+
{
14+
if (reader.TokenType is JsonTokenType.Null)
15+
{
16+
return null;
17+
}
18+
19+
DateOnly value = reader.GetDateOnly();
20+
return new JsonValuePrimitive<DateOnly>(value, JsonMetadataServices.DateOnlyConverter, null);
21+
}
22+
}
23+
#endif
24+
}

‎src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Node/JsonNodeConverter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ private static JsonConverter PrepareJsonValuePrimitiveConverter(Type valueType)
102102
JsonValuePrimitiveKind.Boolean => new JsonValuePrimitiveBooleanConverter(),
103103
JsonValuePrimitiveKind.Byte => new JsonValuePrimitiveByteConverter(),
104104
JsonValuePrimitiveKind.Char => new JsonValuePrimitiveCharConverter(),
105+
#if NET
106+
JsonValuePrimitiveKind.DateOnly => new JsonValuePrimitiveDateOnlyConverter(),
107+
#endif
105108
JsonValuePrimitiveKind.DateTime => new JsonValuePrimitiveDateTimeConverter(),
106109
JsonValuePrimitiveKind.DateTimeOffset => new JsonValuePrimitiveDateTimeOffsetConverter(),
107110
JsonValuePrimitiveKind.Decimal => new JsonValuePrimitiveDecimalConverter(),

‎src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonValueTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public static IEnumerable<object[]> GetPrimitiveTypesTwoWaySerializationCases
1717
get
1818
{
1919
#if NET
20-
//yield return new object[]
21-
//{
22-
// JsonValue.Create(new DateOnly(2025, 4, 16))
23-
//};
20+
yield return new object[]
21+
{
22+
JsonValue.Create(new DateOnly(2025, 4, 16))
23+
};
2424
yield return new object[]
2525
{
2626
JsonValue.Create(Half.MaxValue)

0 commit comments

Comments
 (0)
Please sign in to comment.