Newtonsoft.json: JsonProperty.ItemConverterType does not work when decorated on base class

Created on 22 Feb 2013  ·  3Comments  ·  Source: JamesNK/Newtonsoft.Json

[TestFixture]
public class JsonPropertyItemConverterType
{
    public enum Thingy
    {
        Test
    }

    [JsonObject(MemberSerialization.OptIn)]
    public abstract class Base
    {
        [JsonProperty(PropertyName = "type", Required = Required.Always, Order = 0,
            ItemConverterType = typeof (StringEnumConverter))]
        public Thingy Type { get; internal set; }
    }

    public class Thing : Base
    {
        public Thing()
        {
            Type = Thingy.Test;
        }
    }

    [Test]
    public void ItShouldWork()
    {
        var json = JsonConvert.SerializeObject(new Thing());

        Assert.That(json, Is.EqualTo(@"{""type"":""Test""}"));
    }

    [Test]
    public void ItShouldWork2()
    {
        var json = JsonConvert.SerializeObject(new Thing(), new StringEnumConverter());

        Assert.That(json, Is.EqualTo(@"{""type"":""Test""}"));
    }
}

Most helpful comment

i'm using the wring property,

    [JsonConverter(typeof(StringEnumConverter))]

Works fine.

All 3 comments

the propertyname works but the itemcovertertype does not

i'm using the wring property,

    [JsonConverter(typeof(StringEnumConverter))]

Works fine.

6 years later and the documentation still misled me to make this mistake.

Was this page helpful?
0 / 5 - 0 ratings