How To Serialize Numbers As Strings Using System.Text.Json In C# & .NET
[C#, .NET, Json]
Yesterday’s post, “How To Deserialize Numbers Serialized As Strings Using System.Text.Json In C# & .NET”, looked at a possible scenario of how to deserialize numbers encoded as strings, in situations where you are not in control of your Json input.
In this post, we will look at the opposite problem - how to produce Json compatible with a consumer beyond your control, that expects numbers to be encoded as strings.
The example type will be the Person:
public sealed class Person
{
public string Name { get; set; }
public byte Age { get; set; }
}
To serialize this with the numbers encoded as strings, we will use the JsonSerializerOptions class and set the NumberHandling as appropriate.
using System.Text.Json;
var person = new Person { Name = "James Bond", Age = 40 };
// Configure options for serialization
var options = new JsonSerializerOptions
{
WriteIndented = true,
NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.WriteAsString
};
// Serialize
var json = JsonSerializer.Serialize(person, options);
// Print to console
Console.WriteLine(json);
Here we are setting the WriteAsString option to instruct the serializer to output the number as a string.
If we run this code, it will print the following:
{
"Name": "James Bond",
"Age": "40"
}
Here, the number is encoded as a string, as we expect.
TLDR
We can serialize numbers as strings in Json by setting the NumberHandling property of an instance of the JsonSerializerOptions to System.Text.Json.Serialization.JsonNumberHandling.WriteAsString
The code is in my GitHub.
Happy hacking!