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

Generating C# Models compatible with both Newtonsoft.Json and System.Text.Json #1414

Open
GREsau opened this issue Sep 20, 2021 · 0 comments

Comments

@GREsau
Copy link

GREsau commented Sep 20, 2021

Currently, we can generate C# models for use with either Newtonsoft or STJ, by setting the CSharpGeneratorSettings.JsonLibrary property. But in most cases, there's no need for these to be mutually exclusive. For example, where the generator currently emits [Newtonsoft.Json.JsonProperty(...)] or [System.Text.Json.Serialization.JsonPropertyName(...)], it could instead emit both of them together, which would make the generated class compatible with both libraries.

Our use case for this is that we currently have a central repository for message JSON schemas, which are automatically converted to C# models and made available as a nuget package used by many of our teams. We currently use Newtonsoft for this, but I'd like to start migrating to STJ where possible. We could make a "big bang" change which switches our generated nuget package to use STJ, or start generating a separate nuget package for STJ. My concern with this is that it's easy for someone to inadvertently use a model designed for STJ while still using Newtonsoft to deserialise the JSON, which would cause subtle breakages. I would feel much more comfortable using models compatible with both libraries - then, our teams could migrate their consumers to STJ at their leisure.

Assuming you wouldn't want this to be the default behaviour, this could be achieved by making the CSharpJsonLibrary a flags enum. NJsonSchema consumers could then opt in to this behaviour by setting JsonLibrary to CSharpJsonLibrary.NewtonsoftJson | CSharpJsonLibrary.SystemTextJson.

Please let me know if you'd be open to a PR for this

Related to #1013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant