Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable with JsonSerializer #725

Closed
IgorMenshikov opened this issue Dec 10, 2019 · 4 comments
Closed

Nullable with JsonSerializer #725

IgorMenshikov opened this issue Dec 10, 2019 · 4 comments

Comments

@IgorMenshikov
Copy link

IgorMenshikov commented Dec 10, 2019

I am migrating to .NET Core 3.1 and have a class with null-able values (decimal, DateTime) and want deserialize JSON to it. I received JSON from a client with property values as strings. Here is the sample code:

public class Range
{
    public decimal? Start { get; set; }
    public decimal? End { get; set; }
}

static void Main(string[] args)
{
    var json = "{\"Start\":\"1\",\"End\":\"2\"}";
    var jsonNumber = "{\"Start\": 1,\"End\": 2}";

    var j1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Range>(json); // works fine
    j1.ToString();

    var jn = System.Text.Json.JsonSerializer.Deserialize<Range>(jsonNumber); // works fine
    jn.ToString();

    var js = System.Text.Json.JsonSerializer.Deserialize<Range>(json); // exception
    js.ToString();
}

This code throws an error:

'The JSON value could not be converted to System.Nullable`1[System.Decimal].

I have tried to use a workarounf at:
https://github.com/dotnet/corefx/issues/41070#issuecomment-555395612

but it does not help. Can something be some with that? Any workaround? Newtonsoft can handle that without problems.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Dec 10, 2019
@steveharter
Copy link
Member

This is not related to nullable but instead using strings to represent numbers which is not currently supported by the serializer, reader\writer or JsonDocument.

Currently the best workaround (if you cannot control the incoming JSON to not use strings to represent numbers) is to create a custom converter for the number types. One example of a converter for Int32 is here.

@IgorMenshikov
Copy link
Author

The workaround works but it does not look nice to have converters for all system number types (even I made a base generic class for that). Will such "string - number" conversion be included at the System.Text.Json by default? In my situation I cannot control what JSON we receive, so our system need to "eat" all "look correct" values.

@steveharter
Copy link
Member

We are currently prioritizing features for 5.0 and support for string numeric types has been requested previously including the common case where numeric types are used as the Key for a Dictionary (assuming the JSON format for Dictionaries uses "property":value JSON object syntax where property must be a string.)

I'll mark this 5.0 for now until to triage it and link to related issues.

@steveharter steveharter added this to the 5.0 milestone Dec 10, 2019
@layomia
Copy link
Contributor

layomia commented Dec 12, 2019

As @steveharter mentioned, this issue is about deserializing JSON strings into numbers which isn't supported by the serializer. We have an issue tracking this, so I'll close this as a duplicate: https://github.com/dotnet/corefx/issues/39473.

@layomia layomia closed this as completed Dec 12, 2019
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Dec 12, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants