0

I'm trying to serialize a large struct with loads of enums, and when a property in the struct is not set, I would like to get the first enum string value to be serialized in json instead im getting 0 as a default value.

public enum YesNoUnknown
{
    [EnumName(Name = "unknown")]
    Unknown,

    [EnumName(Name = "yes")]
    Yes,

    [EnumName(Name = "no")]
    No
}

[JsonProperty(PropertyName = "property1", ItemConverterType = typeof(EnumAttributeConverter<YesNoUnknown>))]
public YesNoUnknown Property1 { get; set; }

I want the default result to be: property1: "unknown" instead of: property1: 0

Thanks in advance!

2

1 Answer 1

1

Turns out the problem was that i should have used the Json EnumString attribute instead of using my custom attribute. So these are the changes I made to make it work:

public enum YesNoUnknown
{
    [EnumString("unknown")]
    Unknown,

    [EnumString("yes")]
    Yes,

    [EnumString("no")]
    No
}

    [JsonProperty(PropertyName = "property1")]
    [JsonConverter(typeof(EnumAttributeConverter<YesNoUnknown>))]
    public YesNoUnknown Property1 { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.