Я думаю, что пример 2 предпочтительнее. Я считаю, что наилучшей практикой является объявление вне конструктора и инициализация в конструкторе.
Вы должны установить DefaultValueAttribute
в своем классе Bar. Альтернативой также было бы установить свойство null
.
https://dotnetfiddle.net/aEoBlT
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var inputs = new List<object>()
{new Foo{Prop2 = "" , Bar = new Bar()}};
foreach (object input in inputs)
{
string NormalSerialisation = JsonConvert.SerializeObject(input, Formatting.Indented, new JsonSerializerSettings{});
string CustomSerialisation = JsonConvert.SerializeObject(input, Formatting.Indented, new JsonSerializerSettings{NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore});
Console.WriteLine("normal:");
Console.WriteLine(NormalSerialisation);
Console.WriteLine("custom:");
Console.WriteLine(CustomSerialisation);
}
}
public class Foo
{
public string Prop2
{
get;
set;
}
public Bar Bar
{
get;
set;
}
}
public class Bar
{
[DefaultValue("")]
public string Prop2;
}
}
Выход:
normal:
{
"Prop2": "",
"Bar": {
"Prop2": null
}
}
custom:
{
"Prop2": "",
"Bar": {}
}