Migrate from `Newtonsoft.Json` to `System.Text.Json`
For a long time Newtonsoft.Json was the best JSON library for .NET. However, since System.Text.Json was added in .NET Core 3.0, it's clearly the future. It's a core part of .NET, so there's no need for an additional dependency, and it's far more performant than Newtonsoft.Json
A couple of things to be aware of:
- Should we explicitly add
JsonPropertNameattributes to object properties? - Should we standardize on Camel Case (
exampleVariableName) over PascalCase (ExampleVariableName)?- Currently we use a mix of both
-
Newtonsoft.Jsoncan accept both by default, canSystem.Text.Jsontoo?
- Migrate to asynchronous methods instead of synchronous?
References:
Hi @JamieMagee , I would like to take a stab at it, if this has not been picked up already. Could you answer some of the queries I have, please
Should we explicitly add
JsonPropertyNameattributes to object properties?
just want to understand the reasoning behind this. I am assuming that JsonProperty attribute can be replaced with JsonPropertyName attribute. But, are you suggesting that we add JsonPropertyName on all object properties? One place, where we have to do this is all the models where JsonObject attribute is used currently, because I think System.Text.Json doesn't have any such attribute.
Should we standardize on Camel Case (exampleVariableName) over PascalCase (ExampleVariableName)?
I saw some classes where CamelCaseNamingStrategy is used but could not find any where PascalCase is being used
Newtonsoft.Json can accept both by default, can System.Text.Json too?
The only built-in property naming policy in System.Text.Json is for camel case We'll have to write custom naming policy to support PascalCase
Another thing to take into consideration (I tried this before -- maybe last year?) is maintaining the manifest output contract. If I remember correctly, there had to be some custom serializer implementations to make it line up.
@tevoina thanks for the pointers
@pn04 based on the info Teo shared someone from the Microsoft side will likely have to pick this item up. Thank you for volunteering though ❤️