Newtonsoft.Json
editSerialize an ID
When the project references Newtonsoft.Json, the generator adds a Newtonsoft.Json converter to each ID. It writes the value of the ID, like the System.Text.Json converter:
public sealed record OrderLine(ProductId ProductId, ProjectId ProjectId, int Quantity);var line = new OrderLine(ProductId.FromGuid(new Guid("0f8fad5b-d9cb-469f-a165-70867728950e")), ProjectId.FromInt32(42), Quantity: 3);
var json = Newtonsoft.Json.JsonConvert.SerializeObject(line);
Assert.Equal("""{"ProductId":"0f8fad5b-d9cb-469f-a165-70867728950e","ProjectId":42,"Quantity":3}""", json);
var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<OrderLine>(json);
Assert.Equal(line, deserialized);The converter is registered with [Newtonsoft.Json.JsonConverter] on the ID: there is nothing to add to the JsonSerializerSettings.
Null values and the object form
- A
nullID of a class type, and anullnullable struct (ProjectId?), are written and read asnull. Readingnullinto a non-nullable struct ID throws aJsonSerializationException. - An object that has a
Valueproperty is read as the ID, and its other properties are ignored. An object without aValueproperty gives an ID with the default value. Int128andUInt128are written as numbers with all their digits, and read from numbers or strings.