Home >Backend Development >C++ >How Can I Perform Polymorphic JSON Deserialization in Json.NET Without Type Information?
JSON.NET's powerful feature allows back -to -order polymorphic JSON data even without type information. This is especially useful when processing the data sources of different types of types of types of types, for example, the Imgur API also returns the situation of the Gallery Image and Gallery Album. In order to achieve this, we created a custom JSONCONVERter to handle the instantiated process. First of all, we define our Galleryitem base class and its derived class GalleryImage and Galleryalbum:
Next, we realize our JSONCONVERTER, GalleryitemConverter:
<code class="language-csharp">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; } }</code>The converter checks whether there is a "IS_ALBUM" property to determine the type to be instantiated. Then, it fills the attributes of the object based on the data in the JSON object.
In order to demonstrate its usage, we define an example of the example data provided by the analysis:
<code class="language-csharp">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) { // 解析包含数据的JSON对象 JObject jo = JObject.Load(reader); // 检查是否存在"is_album"以确定对象类型 bool? isAlbum = (bool?)jo["is_album"]; GalleryItem item; if (isAlbum.GetValueOrDefault()) item = new GalleryAlbum(); else item = new GalleryImage(); // 使用JsonSerializer填充对象的属性 serializer.Populate(jo.CreateReader(), item); return item; } // 对于我们的目的,不需要实现CanWrite和WriteJson }</code>
The output of the program shows the successful degradation of the Gallery Image and Gallery Album objects, and the accurate filling of their respective attributes.
In this way, custom JSONCONVERRTER allows us to process polymorphic JSON back serialization without explicit type information, so that we can seamlessly collaborate with different data sources.
<code class="language-csharp">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());</code>
The above is the detailed content of How Can I Perform Polymorphic JSON Deserialization in Json.NET Without Type Information?. For more information, please follow other related articles on the PHP Chinese website!