Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

c# - 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; }
}

Is it possible to serialize Style object with 2 same json property name using ShouldSerialize method?

When I try to serialize Style object I see error at below

Newtonsoft.Json.JsonSerializationException: A member with the name 'cornerRadius' already exists

question from:https://stackoverflow.com/questions/65897462/is-it-possible-to-use-shouldserialize-with-2-same-json-property-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
    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);
        }
    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...