Newtonsoft.json: JsonProperty.ItemConverterType não funciona quando decorado na classe base

Criado em 22 fev. 2013  ·  3Comentários  ·  Fonte: 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""}"));
    }
}

Comentários muito úteis

estou usando a propriedade wring,

    [JsonConverter(typeof(StringEnumConverter))]

Funciona bem.

Todos 3 comentários

o nome da propriedade funciona, mas o itemcovertertype não

estou usando a propriedade wring,

    [JsonConverter(typeof(StringEnumConverter))]

Funciona bem.

6 anos depois e a documentação ainda me induziu a cometer esse erro.

Esta página foi útil?
0 / 5 - 0 avaliações