-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
System.Text.Json fails to deserialize objects with a Guid #31465
Comments
@RasmusWesterlundh can you provide a snippet of your |
Hi @layomia, The only content-payload I have at the moment is with sensitive system information from production data, so I cannot share that with you. I hope this should be fairly easy to replicate none-the-less. In the content-payload the Guid is 32 digits separated with hyphens ("D"-specifier). In the successful deserialization by Newtonsoft.Json that same value is however noted as 32-digits separated by hyphens, enclosed in bracers ("B"-specifier). |
@RasmusWesterlundh - can you provide a simplified/anonymized repro of your issue by extracting that out from your specific application (removing/replacing any sensitive info)? Does your class schema contain public properties with getters and setters? Only those are supported. Does your JSON payload contain the Guid in the "B" format, i.e. surrounded by braces? The This works as expected: [Fact]
public static void GuidDeserializationTest()
{
Foo f = JsonSerializer.Deserialize<Foo>("{\"MyGuid\":\"090bbc52-bfe3-4fa3-9b36-25d7e624e662\"}");
Guid expected = new Guid("090bbc52-bfe3-4fa3-9b36-25d7e624e662");
Assert.Equal(expected, f.MyGuid);
}
public class Foo
{
public Guid MyGuid { get; set; }
} |
The Guid is in "D"-format in the json payload. Edit: I'm sorry, my previous testing of this must have been very incomplete. It is not related to the Guid at all it seems. Removing the Guid property from the class object and removing it from the content string still results in the System.Text.JsonSerializer to fail the deserialization. string content = "{\"data\":{\"Type\":\"Foo\",\"Description\":\"bar \",\"Software\":\"baz\",\"Version\":\"qux\",\"Url\":\"http://example.com\",\"DatabasePortNumber\":1,\"Id\":\"2cdc66f1-0000-0000-39ac-233c00000000\"}}";
var result_SystemText = System.Text.Json.JsonSerializer.Deserialize<SystemApplicationResponse>(content); //Will result in null-object
var result_Newtonsoft = Newtonsoft.Json.JsonConvert.DeserializeObject<SystemApplicationResponse>(content); //Will result in correctly serialized object public class SystemApplicationResponse
{
public SystemApplicationDto Data { get; set; }
}
public class SystemApplicationDto
{
public SystemApplicationDtoType Type { get; set; }
public string Description { get; set; }
public string Software { get; set; }
public string Version { get; set; }
public string Url { get; set; }
public int DatabasePortNumber { get; set; }
public System.Guid Id { get; set; }
}
public enum SystemApplicationDtoType
{
Foo = 0
} |
The very first property ( |
@khellang But at this point it is very apparent that this belongs to StackOverflow rather than as a github issue.... 🙃 |
That's likely because you are deserializing an enum with string values (the default only supports integer values for enums). Register the var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter());
var result_SystemText = JsonSerializer.Deserialize<SystemApplicationResponse>(content, options);
I suspected that.
No problem. I am glad the issue got resolved :) |
Description
Deserializing an object containing a Guid will result in a null value, even if the json string and Guid is correctly formatted.
Deserializing the same object using Newtonsoft.Json is successful and gives the expected object.
Example code to reproduce
dotnet --info
The text was updated successfully, but these errors were encountered: