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

System.Text.Json: Add option to write trailing commas on serialization. #79793

Closed
Drake53 opened this issue Dec 17, 2022 · 3 comments
Closed
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json

Comments

@Drake53
Copy link

Drake53 commented Dec 17, 2022

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

namespace System.Text.Json;

public class JsonSerializerOptions
{
    public bool WriteTrailingCommas { get; set; }
}

API Usage

var 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 Designs

No response

Risks

No response

@Drake53 Drake53 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 17, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 17, 2022
@ghost
Copy link

ghost commented Dec 17, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

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

namespace System.Text.Json;

public class JsonSerializerOptions
{
    public bool WriteTrailingCommas { get; set; }
}

API Usage

var 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 Designs

No response

Risks

No response

Author: Drake53
Assignees: -
Labels:

api-suggestion, area-System.Text.Json

Milestone: -

@fbrosseau
Copy link

fbrosseau commented Dec 17, 2022

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.

since diffs look better when the JSON texts have trailing commas.

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.

@eiriktsarpalis
Copy link
Member

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.

@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Dec 19, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

3 participants