You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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 settingJsonLibrary
toCSharpJsonLibrary.NewtonsoftJson | CSharpJsonLibrary.SystemTextJson
.Please let me know if you'd be open to a PR for this
Related to #1013
The text was updated successfully, but these errors were encountered: