You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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
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
The text was updated successfully, but these errors were encountered: