96

Imgur api调用返回一个列表,其中包含以 JSON 表示的Gallery ImageGallery Album类。

我看不到如何使用 Json.NET 自动反序列化这些,因为没有 $type 属性告诉反序列化器要表示哪个类。有一个名为“IsAlbum”的属性可用于区分两者。

这个问题似乎显示了一种方法,但它看起来有点像黑客。

我该如何反序列化这些类?(使用 C#、Json.NET)

样本数据:

图库图片

{
    "id": "OUHDm",
    "title": "My most recent drawing. Spent over 100 hours.",
        ...
    "is_album": false
}

相册相册

{
    "id": "lDRB2",
    "title": "Imgur Office",
    ...
    "is_album": true,
    "images_count": 3,
    "images": [
        {
            "id": "24nLu",
            ...
            "link": "http://i.imgur.com/24nLu.jpg"
        },
        {
            "id": "Ziz25",
            ...
            "link": "http://i.imgur.com/Ziz25.jpg"
        },
        {
            "id": "9tzW6",
            ...
            "link": "http://i.imgur.com/9tzW6.jpg"
        }
    ]
}
}
4

6 回答 6

137

您可以通过创建自定义JsonConverter来处理对象实例化来相当容易地做到这一点。假设您的类定义如下:

public abstract class GalleryItem
{
    public string id { get; set; }
    public string title { get; set; }
    public string link { get; set; }
    public bool is_album { get; set; }
}

public class GalleryImage : GalleryItem
{
    // ...
}

public class GalleryAlbum : GalleryItem
{
    public int images_count { get; set; }
    public List<GalleryImage> images { get; set; }
}

您将像这样创建转换器:

public class GalleryItemConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(GalleryItem).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, 
        Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);

        // Using a nullable bool here in case "is_album" is not present on an item
        bool? isAlbum = (bool?)jo["is_album"];

        GalleryItem item;
        if (isAlbum.GetValueOrDefault())
        {
            item = new GalleryAlbum();
        }
        else
        {
            item = new GalleryImage();
        }

        serializer.Populate(jo.CreateReader(), item);

        return item;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, 
        object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

这是一个显示转换器的示例程序:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        [
            {
                ""id"": ""OUHDm"",
                ""title"": ""My most recent drawing. Spent over 100 hours."",
                ""link"": ""http://i.imgur.com/OUHDm.jpg"",
                ""is_album"": false
            },
            {
                ""id"": ""lDRB2"",
                ""title"": ""Imgur Office"",
                ""link"": ""http://alanbox.imgur.com/a/lDRB2"",
                ""is_album"": true,
                ""images_count"": 3,
                ""images"": [
                    {
                        ""id"": ""24nLu"",
                        ""link"": ""http://i.imgur.com/24nLu.jpg""
                    },
                    {
                        ""id"": ""Ziz25"",
                        ""link"": ""http://i.imgur.com/Ziz25.jpg""
                    },
                    {
                        ""id"": ""9tzW6"",
                        ""link"": ""http://i.imgur.com/9tzW6.jpg""
                    }
                ]
            }
        ]";

        List<GalleryItem> items = 
            JsonConvert.DeserializeObject<List<GalleryItem>>(json, 
                new GalleryItemConverter());

        foreach (GalleryItem item in items)
        {
            Console.WriteLine("id: " + item.id);
            Console.WriteLine("title: " + item.title);
            Console.WriteLine("link: " + item.link);
            if (item.is_album)
            {
                GalleryAlbum album = (GalleryAlbum)item;
                Console.WriteLine("album images (" + album.images_count + "):");
                foreach (GalleryImage image in album.images)
                {
                    Console.WriteLine("    id: " + image.id);
                    Console.WriteLine("    link: " + image.link);
                }
            }
            Console.WriteLine();
        }
    }
}

这是上述程序的输出:

id: OUHDm
title: My most recent drawing. Spent over 100 hours.
link: http://i.imgur.com/OUHDm.jpg

id: lDRB2
title: Imgur Office
link: http://alanbox.imgur.com/a/lDRB2
album images (3):
    id: 24nLu
    link: http://i.imgur.com/24nLu.jpg
    id: Ziz25
    link: http://i.imgur.com/Ziz25.jpg
    id: 9tzW6
    link: http://i.imgur.com/9tzW6.jpg

小提琴:https ://dotnetfiddle.net/1kplME

于 2013-10-11T00:38:28.353 回答
58

只需使用适用于 Json.NET 的JsonSubTypes属性

    [JsonConverter(typeof(JsonSubtypes), "is_album")]
    [JsonSubtypes.KnownSubType(typeof(GalleryAlbum), true)]
    [JsonSubtypes.KnownSubType(typeof(GalleryImage), false)]
    public abstract class GalleryItem
    {
        public string id { get; set; }
        public string title { get; set; }
        public string link { get; set; }
        public bool is_album { get; set; }
    }

    public class GalleryImage : GalleryItem
    {
        // ...
    }

    public class GalleryAlbum : GalleryItem
    {
        public int images_count { get; set; }
        public List<GalleryImage> images { get; set; }
    }
于 2018-03-10T21:13:28.077 回答
4

高级到布赖恩·罗杰斯的回答。关于“使用 Serializer.Populate() 而不是 item.ToObject()”。如果派生类型具有构造函数或它们的某些具有自己的自定义转换器,则必须使用通用方式来反序列化 JSON。所以你必须把实例化新对象的工作留给 NewtonJson。这样你就可以在你的 CustomJsonConverter 中实现它:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    ..... YOU Code For Determine Real Type of Json Record .......

    // 1. Correct ContractResolver for you derived type
    var contract = serializer.ContractResolver.ResolveContract(DeterminedType);
    if (converter != null && !typeDeserializer.Type.IsAbstract && converter.GetType() == GetType())
    {
        contract.Converter = null; // Clean Wrong Converter grabbed by DefaultContractResolver from you base class for derived class
    }

    // Deserialize in general way           
    var jTokenReader = new JTokenReader(jObject);
    var result = serializer.Deserialize(jTokenReader, DeterminedType);

    return (result);
}

如果您有对象递归,则此工作。

于 2019-02-15T13:24:43.177 回答
1

我只是发布这个来消除一些混乱。如果您正在使用预定义的格式并需要对其进行反序列化,我发现这是最有效的,并演示了机制,以便其他人可以根据需要对其进行调整。

public class BaseClassConverter : JsonConverter
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var j = JObject.Load(reader);
            var retval = BaseClass.From(j, serializer);
            return retval;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }

        public override bool CanConvert(Type objectType)
        {
            // important - do not cause subclasses to go through this converter
            return objectType == typeof(BaseClass);
        }
    }

    // important to not use attribute otherwise you'll infinite loop
    public abstract class BaseClass
    {
        internal static Type[] Types = new Type[] {
            typeof(Subclass1),
            typeof(Subclass2),
            typeof(Subclass3)
        };

        internal static Dictionary<string, Type> TypesByName = Types.ToDictionary(t => t.Name.Split('.').Last());

        // type property based off of class name
        [JsonProperty(PropertyName = "type", Required = Required.Always)]
        public string JsonObjectType { get { return this.GetType().Name.Split('.').Last(); } set { } }

        // convenience method to deserialize a JObject
        public static new BaseClass From(JObject obj, JsonSerializer serializer)
        {
            // this is our object type property
            var str = (string)obj["type"];

            // we map using a dictionary, but you can do whatever you want
            var type = TypesByName[str];

            // important to pass serializer (and its settings) along
            return obj.ToObject(type, serializer) as BaseClass;
        }


        // convenience method for deserialization
        public static BaseClass Deserialize(JsonReader reader)
        {
            JsonSerializer ser = new JsonSerializer();
            // important to add converter here
            ser.Converters.Add(new BaseClassConverter());

            return ser.Deserialize<BaseClass>(reader);
        }
    }
于 2017-09-18T20:42:01.910 回答
1

以下实现应该让您在不改变设计类的方式的情况下进行反序列化,并使用 $type 以外的字段来决定将其反序列化为什么。

public class GalleryImageConverter : JsonConverter
{   
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(GalleryImage) || objectType == typeof(GalleryAlbum));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        try
        {
            if (!CanConvert(objectType))
                throw new InvalidDataException("Invalid type of object");
            JObject jo = JObject.Load(reader);
            // following is to avoid use of magic strings
            var isAlbumPropertyName = ((MemberExpression)((Expression<Func<GalleryImage, bool>>)(s => s.is_album)).Body).Member.Name;
            JToken jt;
            if (!jo.TryGetValue(isAlbumPropertyName, StringComparison.InvariantCultureIgnoreCase, out jt))
            {
                return jo.ToObject<GalleryImage>();
            }
            var propValue = jt.Value<bool>();
            if(propValue) {
                resultType = typeof(GalleryAlbum);
            }
            else{
                resultType = typeof(GalleryImage);
            }
            var resultObject = Convert.ChangeType(Activator.CreateInstance(resultType), resultType);
            var objectProperties=resultType.GetProperties();
            foreach (var objectProperty in objectProperties)
            {
                var propType = objectProperty.PropertyType;
                var propName = objectProperty.Name;
                var token = jo.GetValue(propName, StringComparison.InvariantCultureIgnoreCase);
                if (token != null)
                {
                    objectProperty.SetValue(resultObject,token.ToObject(propType)?? objectProperty.GetValue(resultObject));
                }
            }
            return resultObject;
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}
于 2018-01-16T18:31:10.700 回答
0

@ИгорьОрлов 的答案适用于您的类型只能由 JSON.net 直接实例化(由于[JsonConstructor]和/或[JsonProperty]直接在构造函数参数上使用) 。但是,当 JSON.net 已经缓存了要使用的转换器时,覆盖contract.Converter = null 不起作用。

(如果 JSON.NET 使用不可变类型来指示数据和配置何时不再可变,这将不是问题,le sigh )

就我而言,我这样做了:

  1. 实现了一个自定义JsonConverter<T>T我的 DTO 的基类在哪里)。
  2. 定义了一个DefaultContractResolver子类,该子类覆盖以ResolveContractConverter返回基类的自定义。JsonConverter

详细并举例:

假设我有这些代表远程文件系统的不可变 DTO(所以有'DirectoryDto并且FileDto它们都继承FileSystemDto,就像如何DirectoryInfoFileInfo派生一样System.IO.FileSystemInfo):

public enum DtoKind
{
    None = 0,
    File,
    Directory
}

public abstract class FileSystemDto
{
    protected FileSystemDto( String name, DtoKind kind )
    {
        this.Name = name ?? throw new ArgumentNullException(nameof(name));
        this.Kind = kind;
    }

    [JsonProperty( "name" )]
    public String Name { get; }

    [JsonProperty( "kind" )]
    public String Kind { get; }
}

public class FileDto : FileSystemDto
{
    [JsonConstructor]
    public FileDto(
        [JsonProperty("name"  )] String  name,
        [JsonProperty("length")] Int64   length,
        [JsonProperty("kind")  ] DtoKind kind
    )
        : base( name: name, kind: kind )
    {
        if( kind != DtoKind.File ) throw new InvalidOperationException( "blargh" );
        this.Length = length;
    }

    [JsonProperty( "length" )]
    public Int64 Length { get; }
}

public class DirectoryDto : FileSystemDto
{
    [JsonConstructor]
    public FileDto(
        [JsonProperty("name")] String  name,
        [JsonProperty("kind")] DtoKind kind
    )
        : base( name: name, kind: kind )
    {
        if( kind != DtoKind.Directory ) throw new InvalidOperationException( "blargh" );
    }
}

假设我有一个 JSON 数组FileSystemDto

[
    { "name": "foo.txt", "kind": "File", "length": 12345 },
    { "name": "bar.txt", "kind": "File", "length": 12345 },
    { "name": "subdir", "kind": "Directory" },
]

我希望 Json.net 将其反序列化为List<FileSystemDto>...

所以定义一个子类DefaultContractResolver(或者如果你已经有一个解析器实现,那么子类(或compose))并覆盖ResolveContractConverter

public class MyContractResolver : DefaultContractResolver
{
    protected override JsonConverter? ResolveContractConverter( Type objectType )
    {
        if( objectType == typeof(FileSystemDto) )
        {
            return MyJsonConverter.Instance;
        }
        else if( objectType == typeof(FileDto ) )
        {
            // use default
        }
        else if( objectType == typeof(DirectoryDto) )
        {
            // use default
        }

        return base.ResolveContractConverter( objectType );
    }
}

然后实施MyJsonConverter

public class MyJsonConverter : JsonConverter<FileSystemDto>
{
    public static MyJsonConverter Instance { get; } = new MyJsonConverter();

    private MyJsonConverter() {}

    // TODO: Override `CanWrite => false` and `WriteJson { throw; }` if you like.

    public override FileSystemDto? ReadJson( JsonReader reader, Type objectType, FileSystemDto? existingValue, Boolean hasExistingValue, JsonSerializer serializer )
    {
        if( reader.TokenType == JsonToken.Null ) return null;

        if( objectType == typeof(FileSystemDto) )
        {
            JObject jsonObject = JObject.Load( reader );
            if( jsonObject.Property( "kind" )?.Value is JValue jv && jv.Value is String kind )
            {
                if( kind == "File" )
                {
                    return jsonObject.ToObject<FileDto>( serializer );
                }
                else if( kind == "Directory" )
                {
                    return jsonObject.ToObject<DirectoryDto>( serializer );
                }
            }
        }

        return null; // or throw, depending on your strictness.
    }
}

然后,要反序列化,请使用正确设置的JsonSerializer实例ContractResolver,例如:

public static IReadOnlyList<FileSystemDto> DeserializeFileSystemJsonArray( String json )
{
    JsonSerializer jss = new JsonSerializer()
    {
        ContractResolver = new KuduDtoContractResolver()
    };

    using( StringReader strRdr = new StringReader( json ) )
    using( JsonTextReader jsonRdr = new JsonTextReader( strRdr ) )
    {
        List<FileSystemDto>? list = jss.Deserialize< List<FileSystemDto> >( jsonRdr );
        // TODO: Throw if `list` is null.
        return list;
    }
}
于 2021-08-17T23:31:33.517 回答