JSON.NET:对嵌套数组进行强烈键入的对象



我正在编写客户端应用程序,该应用程序应该处理服务器响应。回答是在JSON中。我决定使用JSON.NET进行挑选。而且我无法简化或修改这些响应(在服务器端)。此特定JSON响应的特殊困难是不同对象类型在同一数组中:哈希和数组文件。因此,我想将此数组直接化为强烈键入的对象,而不是对象数组。我认为应该可以使用CustomCreationConverter实现,但我无法弄清楚。

json:

{
    "files":
    [
        "hash string",
        [
            ["first file name", 12],
            ["second file name", 34]
        ]
    ]
}

我要实现的对象结构:

public class RootObject
{
    [JsonProperty("files")]
    public FilesContainer Container
    {
        get;
        set;
    }
}
public class FilesContainer
{
    public string Hash
    {
        get;
        set;
    }
    public File[] Files
    {
        get;
        set;
    }
}
[JsonConverter(typeof(FileJsonConverter))]
public class File
{
    public string Name
    {
        get;
        set;
    }
    public int Size
    {
        get;
        set;
    }
}
class FileJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        File obj = new File
        {
            Name = reader.ReadAsString(),
            Size = reader.ReadAsInt32().GetValueOrDefault()
        };
        reader.Read();
        return obj;
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

我终于弄清楚了如何做到这一点。我需要在JsonConverter内使用serializer.Deserialize。因此,这是一个工作示例。

class Program
{
    static string json = @"{
                                ""files"":
                                [
                                    ""hash string"",
                                    [
                                        [""first file name"", 12],
                                        [""second file name"", 34]
                                    ]
                                ]
                            }";
    static void Main(string[] args)
    {
        RootObject container = JsonConvert.DeserializeObject<RootObject>(json);
    }
}
public class RootObject
{
    [JsonProperty("files")]
    public FilesContainer Container
    {
        get;
        set;
    }
}
[JsonConverter(typeof(FilesContainerJsonConverter))]
public class FilesContainer
{
    public string Hash
    {
        get;
        set;
    }
    public File[] Files
    {
        get;
        set;
    }
}
public class FilesContainerJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        FilesContainer obj = new FilesContainer();
        obj.Hash = reader.ReadAsString();
        reader.Read(); // to StartArray
        obj.Files = serializer.Deserialize<File[]>(reader);
        reader.Read(); // to EndArray
        return obj;
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}
[JsonConverter(typeof(FileJsonConverter))]
public class File
{
    public string Name
    {
        get;
        set;
    }
    public int Size
    {
        get;
        set;
    }
}
class FileJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        File obj = new File
        {
            Name = reader.ReadAsString(),
            Size = reader.ReadAsInt32().GetValueOrDefault()
        };
        reader.Read(); // to EndArray
        return obj;
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

相关内容

  • 没有找到相关文章

最新更新