Repro steps:
public class MyRecord
{
public int IDValue { get; set; }
public string NameValue { get; set; }
public DateTime DateTimeValue { get; set; }
public Decimal DecimalValue { get; set; }
}
...
// create a record, fill-in only some properties
MyRecord myRecord = new MyRecord { IDValue = 1 };
// serialize to string
string serializedValue = System.Text.Json.Serialization.JsonSerializer.ToString(myRecord);
// deserialize
MyRecord deserializedRecord = System.Text.Json.Serialization.JsonSerializer.Parse<MyRecord>(serializedValue);
This produces an error:
System.Text.Json.JsonException
Message=The JSON value could not be converted to System.DateTime. Path: $.DateTimeValue | LineNumber: 0 | BytePositionInLine: 67.
Source=System.Text.Json
Is this a bug in System.Text.Json, or is there any recommendation how to handle "empty" datetimes?
Thanks
Looks like you're on .NET Core 3.0 P5 or the NuGet pacakge before P6; have you tried it on P6? it seems to work fine on the latest...ish master branch.
(Note that JsonSerializer has been moved to System.Text.Json out of the Serialization namespace)
I'm on P6 (don't have P5 installed) -
位 dotnet --list-runtimes
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview6.19307.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
位 dotnet --list-sdks
2.1.507 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.1.800-preview-009696 [C:\Program Files\dotnet\sdk]
2.2.300 [C:\Program Files\dotnet\sdk]
3.0.100-preview6-012264 [C:\Program Files\dotnet\sdk]
And this is from the compiler output:
C:\Program Files\dotnet\sdk\3.0.100-preview6-012264\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(158,5): message NETSDK1057: You are using a preview version of .NET Core.
Hmm, maybe it's fixed post-P6 perhaps. (I'm on 3.0.100-preview7-012426) I'm able to deserialize the JSON string back and read the properties:
// create a record, fill-in only some properties
MyRecord myRecord = new MyRecord { IDValue = 1 };
// serialize to string
string serializedValue = JsonSerializer.ToString(myRecord);
// deserialize
MyRecord deserializedRecord = JsonSerializer.Parse<MyRecord>(serializedValue);
Console.ReadLine();
Console.WriteLine($"DateTimeValue: {deserializedRecord.DateTimeValue:O}");
Console.WriteLine($"DecimalValue: {deserializedRecord.DecimalValue}");
Console.WriteLine($"IDValue: {deserializedRecord.IDValue}");
Console.WriteLine($"NameValue: {deserializedRecord.NameValue ?? "null"}");
// Outputs:
// DateTimeValue: 0001-01-01T00:00:00.0000000
// DecimalValue: 0
// IDValue: 1
// NameValue: null
ok then, I will wait for P7, no problem.
Thanks for your time.
If you want to try it yourself, you could follow this:
https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/dogfooding.md
As @Gnbrkm41 pointed out, this issue is not reproducible in the current build, so I'm closing this for now.
@radek7210 , please feel free to re-open if you sync to the latest build and still get this issue - https://github.com/dotnet/core-sdk#installers-and-binaries.
Most helpful comment
As @Gnbrkm41 pointed out, this issue is not reproducible in the current build, so I'm closing this for now.
@radek7210 , please feel free to re-open if you sync to the latest build and still get this issue - https://github.com/dotnet/core-sdk#installers-and-binaries.