public string SerializeMethod()
return JsonSerializer.Serialize(_clubList);
In this method, we call the Serialize()
method from the JsonSerializer
class, passing in the List<T>
object that we want to serialize as a parameter.
This method returns a minified (no indentation, whitespaces, and newline characters) string:
[{"Name":"Arsenal","YearFounded":1886,"Country":"England","NumberOfPlayers":"26"},
{"Name":"Manchester City","YearFounded":1880,"Country":"England","NumberOfPlayers":"25"},
{"Name":"Sunderland","YearFounded":1879,"Country":"England","NumberOfPlayers":"30"}]
This is the output string we desire and we can use it in this form to store data in a file or send information over a network to another application. However, it is not easily readable and the property names are not in the recommended JSON format (camelcase). Please note, in the code sample, we broke the string into several lines to enhance readability.
To address these issues, let’s define an instance of the JsonSerializerOptions
class: