Serialize

1
string json = JsonSerializer.Serialize(poco);

Deserialize

1
Poco poco = JsonSerializer.Deserialize<Poco>(json);

Specified property name

add attribute [JsonPropertyName(name)] on Poco property.

e.g.
1
2
[JsonPropertyName("summary")]
double Summarycurrency{get;init;}

will serialize as {"summary":1.0.0}

ignore null

ignore propertie serialize when its value is null.

JsonSerializer.Serialize has second param:options to control behavior
set options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull

e.g.
1
2
var options = new JsonSerializerOptions(){ DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNul};
string json = JsonSerializer.Serialize(poco, options);

serialize with constructor

add attribute [JsonConstructor] on class constructor

enum to string

serialize enum value to its name(default is numerical) or deserialize.

add JsonStringEnumConverter to options.Converters

e.g.
1
var options = new JsonSerializerOptions(){Converters={new JsonStringEnumConverter()}}

enum to lower camel case string

convert GenderEnum.Male to "male"
set JsonStringEnumConverter use JsonNamingPolicy.CamelCase

e.g.
1
var options = new JsonSerializerOptions(){Converters={new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)}}