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

The JSON value could not be converted to System.String #37354

Closed
tinyVishal opened this issue Jun 3, 2020 · 4 comments
Closed

The JSON value could not be converted to System.String #37354

tinyVishal opened this issue Jun 3, 2020 · 4 comments

Comments

@tinyVishal
Copy link

The JSON value could not be converted to System.String

Describe the bug

When a property is explicitly tagged to a datatype, a framework should not try to parse it.
Sample code works fine with Newtonsoft.json

To Reproduce

code sample

namespace ConsoleApp2
{

    using System;
    using System.Collections.Generic;
    using System.Text.Json;

    public class Sample
    {
        public string Id { get; set; }
        public string Data { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string data = "[{\"Id\":1, \"Data\":\"item 1\"}, {\"Id\":\"id2\", \"Data\":\"item 2\"}]";
            var parsedData = JsonSerializer.Deserialize<List<Sample>>(data);

            Console.WriteLine(parsedData.Count);
        }
    }
}

Exceptions (if any)

Cannot get the value of a token type 'Number' as a string.

Stack trace

at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)

Further technical details

IDE Microsoft Visual Studio Professional 2019
Version 16.5.4

Framework Info
.NET Core SDK (reflecting any global.json):
Version: 3.1.201
Commit: b1768b4ae7

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.201\

Host (useful for support):
Version: 3.1.3
Commit: 4a9f85e9f8

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/aspnetcore Jun 3, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jun 3, 2020
@Tornhoof
Copy link
Contributor

Tornhoof commented Jun 3, 2020

Why do you think there is a bug?
Your example json contains "Id":1 and that is a Number, but your data model has string Id.

System.Text.Json expects correct type maps.

There is a bit of discussion about looser requirements in
#30255 (comment)

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jun 10, 2020
@layomia layomia added this to the 5.0 milestone Jun 10, 2020
@layomia
Copy link
Contributor

layomia commented Jun 10, 2020

When a property is explicitly tagged to a datatype, a framework should not try to parse it.

What do you mean by this?

There is a bit of discussion about looser requirements in #30255 (comment)

Apart from providing an option to deserialize strings into numbers (#30255), which satisfies common usage patterns on the web, I don't envision the serializer reinterpreting or ignoring JSON input that doesn't match with the CLR type.

@layomia layomia closed this as completed Jun 10, 2020
@tinyVishal
Copy link
Author

Sorry for delay.
@layomia, @Tornhoof
Please run code, if you still could not able to find issue, please email me.

@tinyVishal
Copy link
Author

@richlander can you please take a look at my code, Let me know if it don't make sense to you.

Anyway it works in NewtonSoftJson

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 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