Json.NET: deserializing nested arrays into a strongly typed object

I am writing a client application that should handle server responses. The answers are in JSON. I decided to use Json.NET to deserialize them. And I could not simplify or modify these answers (server side). The particular difficulties of this particular JSON response are that different types of objects are in the same array: a hash and an array of files. Therefore, I would like to deserialize this array directly in a strongly typed object, and not in an array of objects. I think it can be achieved using CustomCreationConverter , but I could not figure out how to do this.

JSON:

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

The structure of the object I'm trying to achieve is:

 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(); } } 
+4
source share
1 answer

I finally figured out how to do this. I needed to use serializer.Deserialize inside a JsonConverter . So here is a working example.

 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(); } } 
+4
source

Source: https://habr.com/ru/post/1442023/


All Articles