-
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
System.Text.Json: Add option to write trailing commas on serialization. #79793
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationCurrently trailing commas are only supported on deserialization using JsonSerializerOptions.AllowTrailingCommas, it'd be nice if they can be supported on serialization as well. One of the benefits of this feature would be improving diff views (for example with this library: https://github.com/mmanela/diffplex), since diffs look better when the JSON texts have trailing commas. API Proposalnamespace System.Text.Json;
public class JsonSerializerOptions
{
public bool WriteTrailingCommas { get; set; }
} API Usagevar options = new JsonSerializerOptions
{
WriteTrailingCommas = true,
WriteIndented = true,
};
var oldText = JsonSerializer.Serialize(oldObject, options);
var newText = JsonSerializer.Serialize(newObject, options);
var diffView = new DiffViewer
{
Dock = DockStyle.Fill,
OldText = oldText,
NewText = newText,
}; Alternative DesignsNo response RisksNo response
|
Allowing to read non-standard extensions like commas (and probably much more often, comments) is very different from allowing to generate non-standard output. Being able to read allows you to create a well-behaving application that happens to tolerate slightly invalid input (again, mostly comments) without having to reinvent systemtextjson, or without having to preprocess user input. Being able to write would make dotnet generate objectively invalid output, which is probably not something you want as a framework.
This is probably something for difftools to improve on. A Json-aware diff tool could offer to hide those details, the same way most difftools offer to hide insignificant whitespace. |
Even though writing invalid JSON is something we would never consider for STJ, it might be possible to include opt-in support for reading certain forms of invalid JSON. This is tracked by #32291. |
Background and motivation
Currently trailing commas are only supported on deserialization using JsonSerializerOptions.AllowTrailingCommas, it'd be nice if they can be supported on serialization as well.
One of the benefits of this feature would be improving diff views (for example with this library: https://github.com/mmanela/diffplex), since diffs look better when the JSON texts have trailing commas.
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: