是否可以将 ShouldSerialize 与 2 个相同的 json 属性 名称一起使用?
Is it possible to use ShouldSerialize with 2 same json property name?
public class Style
{
[JsonProperty("cornerRadius")]
public double CornerRadius { get; set; }
[JsonProperty("cornerRadius")]
public BorderRadiusObject BorderRadius { get; set; }
public bool ShouldSerializeCornerRadius()
{
return false;
}
public bool ShouldSerializeBorderRadius()
{
return true;
}
}
public class BorderRadiusObject
{
public double A { get; set; }
public double B { get; set; }
public double C { get; set; }
public double D { get; set; }
}
是否可以使用 ShouldSerialize 方法序列化具有 2 个相同 json 属性 名称的 Style
对象?
当我尝试序列化 Style
对象时,我在下面看到错误
Newtonsoft.Json.JsonSerializationException: A member with the name
'cornerRadius' already exists
- 你可以试试DefaultContractResolver。
- 不要标记相同的 属性 名称。
public class ShouldSerializeContractResolver : DefaultContractResolver
{
public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (member.Name == "BorderRadius" && Style.ShouldSerializeBorderRadius())
{
property.PropertyName = "cornerRadius";
property.ShouldSerialize =
instance => { return true; };
}
if (member.Name == "CornerRadius" && Style.ShouldSerializeCornerRadius())
{
property.PropertyName = "cornerRadius";
property.ShouldSerialize =
instance => { return true; };
}
return property;
}
}
public class Style
{
public double CornerRadius { get; set; } = 9.9;
public BorderRadiusObject BorderRadius { get; set; } = new BorderRadiusObject();
public static bool ShouldSerializeCornerRadius()
{
return false;
}
public static bool ShouldSerializeBorderRadius()
{
return true;
}
}
public class BorderRadiusObject
{
public double A { get; set; } = 1;
public double B { get; set; } = 2;
public double C { get; set; } = 3;
public double D { get; set; } = 4;
}
class Program
{
static void Main(string[] args)
{
var memberJson = Newtonsoft.Json.JsonConvert.SerializeObject(new Style(),
new JsonSerializerSettings {ContractResolver = new ShouldSerializeContractResolver()});
Console.WriteLine(memberJson);
}
}
public class Style
{
[JsonProperty("cornerRadius")]
public double CornerRadius { get; set; }
[JsonProperty("cornerRadius")]
public BorderRadiusObject BorderRadius { get; set; }
public bool ShouldSerializeCornerRadius()
{
return false;
}
public bool ShouldSerializeBorderRadius()
{
return true;
}
}
public class BorderRadiusObject
{
public double A { get; set; }
public double B { get; set; }
public double C { get; set; }
public double D { get; set; }
}
是否可以使用 ShouldSerialize 方法序列化具有 2 个相同 json 属性 名称的 Style
对象?
当我尝试序列化 Style
对象时,我在下面看到错误
Newtonsoft.Json.JsonSerializationException: A member with the name 'cornerRadius' already exists
- 你可以试试DefaultContractResolver。
- 不要标记相同的 属性 名称。
public class ShouldSerializeContractResolver : DefaultContractResolver
{
public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (member.Name == "BorderRadius" && Style.ShouldSerializeBorderRadius())
{
property.PropertyName = "cornerRadius";
property.ShouldSerialize =
instance => { return true; };
}
if (member.Name == "CornerRadius" && Style.ShouldSerializeCornerRadius())
{
property.PropertyName = "cornerRadius";
property.ShouldSerialize =
instance => { return true; };
}
return property;
}
}
public class Style
{
public double CornerRadius { get; set; } = 9.9;
public BorderRadiusObject BorderRadius { get; set; } = new BorderRadiusObject();
public static bool ShouldSerializeCornerRadius()
{
return false;
}
public static bool ShouldSerializeBorderRadius()
{
return true;
}
}
public class BorderRadiusObject
{
public double A { get; set; } = 1;
public double B { get; set; } = 2;
public double C { get; set; } = 3;
public double D { get; set; } = 4;
}
class Program
{
static void Main(string[] args)
{
var memberJson = Newtonsoft.Json.JsonConvert.SerializeObject(new Style(),
new JsonSerializerSettings {ContractResolver = new ShouldSerializeContractResolver()});
Console.WriteLine(memberJson);
}
}