-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
ReferenceHandler.IgnoreCycles not handling duplicate strings properly #51837
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsDescriptionUsing System.Text.Json's ReferenceHandler.IgnoreCycles is not working properly with string properties. It seems that System.Text.Json is improperly detecting strings with the same value as a cycle. ConfigurationSystem.Text.Json Nuget Version: 6.0.0-preview.3.21201.4 Other informationHere is some sample code that demonstrates the issue. In this case, This same test works fine when not specifying [Fact]
public void Test()
{
var parent = new Parent
{
Name = "John",
Age = 60,
Child = new Child
{
Name = "John",
Age = 15
}
};
var json = JsonSerializer.Serialize(parent, new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.IgnoreCycles,
WriteIndented = true
});
}
public class Parent
{
public string Name { get; set; }
public int Age { get; set; }
public Child Child { get; set; }
}
public class Child
{
public string Name { get; set; }
public int Age { get; set; }
}
|
JSON team - may want to investigate this behavior for other primitives that have reference equality, such as if a single instance of a boxed integer appears in multiple places. |
Curiously this seem to only impact strings in my tests: using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
void Test(object? value)
{
var list = new List<object?> { value, value };
var options = new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.IgnoreCycles };
var json = JsonSerializer.Serialize(list, options);
Console.WriteLine(json);
}
Test(42); // [42,42]
Test(new object()); // [{},{}]
Test(new List<int>()); // [[],[]]
Test("John"); // ["John",null] There seems to be something about strings in particular that makes the serializer erroneously treat all recurring references as cycles. cc @jozkee |
Description
Using System.Text.Json's ReferenceHandler.IgnoreCycles is not working properly with string properties. It seems that System.Text.Json is improperly detecting strings with the same value as a cycle.
Configuration
System.Text.Json Nuget Version: 6.0.0-preview.3.21201.4
Other information
Here is some sample code that demonstrates the issue. In this case,
parent.child.name
will be serialized as null. No cycles here so I was expecting the name 'John' to be serialized for bothparent.name
andparent.child.name
.This same test works fine when not specifying
ReferenceHandler
and no cyclic exceptions are thrown.Output without specifying
ReferenceHandler
Output when specifying
ReferenceHandler.IgnoreCycles
The text was updated successfully, but these errors were encountered: