Newtonsoft.json: JsonProperty.ItemConverterType no funciona cuando se decora en la clase base

Creado en 22 feb. 2013  ·  3Comentarios  ·  Fuente: 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""}"));
    }
}

Comentario más útil

estoy usando la propiedad wring,

    [JsonConverter(typeof(StringEnumConverter))]

Funciona bien.

Todos 3 comentarios

el propertyname funciona pero el itemcovertertype no

estoy usando la propiedad wring,

    [JsonConverter(typeof(StringEnumConverter))]

Funciona bien.

6 años después y la documentación todavía me engañaba para cometer este error.

¿Fue útil esta página
0 / 5 - 0 calificaciones