Skip to content

System.Text.Json fails to deserialize datetime #29964

Closed

Description

@radek7210

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

Activity

  1. Gnbrkm41 commented on Jun 20, 2019

    @Gnbrkm41
    Contributor

    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)

  2. radek7210 commented on Jun 20, 2019

    @radek7210
    Author

    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.

  3. Gnbrkm41 commented on Jun 20, 2019

    @Gnbrkm41
    Contributor

    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
  4. radek7210 commented on Jun 20, 2019

    @radek7210
    Author

    ok then, I will wait for P7, no problem.
    Thanks for your time.

  5. layomia commented on Jun 20, 2019

    @layomia
    Contributor

    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.

  6. transferred this issue fromdotnet/corefxon Feb 1, 2020
  7. added this to the 3.0 milestone on Feb 1, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions