I have the following type:
public class Product : Dictionary<string, object>
{
[JsonInclude]
public string ProductId { get; set; }
public Product(string productId) : base()
{
ProductId = productId;
}
}
When serialising using System.Text.Json it does not include the properties (ie ProductId
).
Adding or removing the [JsonInclude]
does not seem to make any effect.
Test case:
[Fact]
public void SimpleTest()
{
var p = new Product("ABC123");
p["foo"] = "bar";
var json = JsonSerializer.Serialize(p);
Assert.Contains("productId", json, StringComparison.OrdinalIgnoreCase);
}
And output received:
{"foo":"bar"}
How do I make it include my custom properties on my type during serialisation? (note: don't care about deserialisation).