在没有typeNameHandling的情况下将Json对象反序列化为多态C#对象 [英] Deserialize Json Object to polymorphic C# object without typeNameHandling

查看:153
本文介绍了在没有typeNameHandling的情况下将Json对象反序列化为多态C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想将json对象反序列化为C#对象,但是诀窍在于C#对象包含List<抽象类>,并且该抽象类是另外10个类的超类.

My problem is I want to deserialize a json object to a C# object, but the trick is that the C# object contains List< abstract class > and this abstract class is a super class of another 10 classes.

public sealed class SearchAPIResult
{
    public string Status;

    public SearchAPIQuery Query;

    public SearchResults Result;

    public SearchAPIResult()
    {

    }

    public SearchAPIResult(string status)
    {
        Status = status;
    }
}

SearchAPIResult是:

public sealed class SearchResults
{
    public string TextAnswer;

    public List<APIResultWidget> Items;

    public SearchResults()
    {
        Items = new List<APIResultWidget>();
    }
}

,这里的对象APIResultWidget是一个抽象类,它继承了大约10个类.

and here the object APIResultWidget is an abstract class that has about 10 classes inherit from it.

问题是JSON对象没有自动的东西(例如JSON.NET中的typeNameHandling)来指导反序列化器将10个派生类的对象投射到哪个对象.而是用两个字段来标记对象:Type和SubType ...类似于以下内容

The problem is that the JSON object doesn't have something automatic (like typeNameHandling in JSON.NET) to guide the deserializer to which object of the 10 derived classes to cast to. instead the objects are marked by two fields: Type and SubType... like the following

{
    "Status": "OK",
    "Query": {
        "Query": "this is a query",
        "QueryLanguage": "EN"
    },
    "Result": {
        "TextAnswer": "This is your text answer",
        "Items": [{
                "Type": "list",
                "SubType": null,
                "Title": null,
                "Items": []
            }, {
                "Type": "text",
                "Content": "this is some content"
            }
        ]
    }
}

在上一个json对象中,结果"列表包含两个对象,一个:

in the previous json object, the Result list contains two objects, one:

{
    "Type": "list",
    "SubType": null,
    "Title": null,
    "Items": []
}

映射到类型为listWidget的类(该类继承自抽象APIResultWidget和两个:

which maps to a class of type listWidget (which inherits from the abstract APIResultWidget and two:

{
    "Type": "text",
    "Content": "this is some content"
}

映射到类textWidget,该类也继承自同一抽象类

which maps to class textWidget which also inherits from the same abstract class

当我使用Json.NET方式

when I use the Json.NET way

SearchAPIResult converted = (SearchAPIResult)JsonConvert.DeserializeObject(json, typeof(SearchAPIResult), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });

它引发以下异常:

无法创建类型为Kngine.API.APIResultWidget的实例.类型是接口或抽象类,无法实例化.路径"Result.Items [0] .Type",第1行,位置136.

Could not create an instance of type Kngine.API.APIResultWidget. Type is an interface or abstract class and cannot be instantiated. Path 'Result.Items[0].Type', line 1, position 136.

我猜想有一种自定义的方式来指出该类型同时由Type和SubType字段定义,并为Converter提供自定义类型注释器,对吗?

I am guessing there's a custom way of pointing out that that type is defined by both the fields Type and SubType and giving the Converter that custom type annotator, is that right?

推荐答案

实际上,我实施的解决方案是最基本的解决方案,我从System.Web.Extensions扩展了JavaScriptConverter类,实现了deseriealize方法,该方法自动接收每个原始对象中的一个小的json对象(如Dictionary),我可以根据对象本身填写字段,这是一些手动的方式,但这是我能想到并起作用的唯一解决方案,自定义类实现看起来像这样:

Actually the solution I implmented is the very basic solution, I extended the JavaScriptConverter class from System.Web.Extentions, I implemented the deseriealize method, which automatically receives each small json object in the original object as a Dictionary and I can just fill in the fields according to the object itself, it's a bit manual way, but that was the only solution that I could come up with and works, the custom class implmentation looks like this:

class myCustomResolver  : JavaScriptConverter

{
    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (type == typeof(APIResultWidget))
        {
            switch ((string)dictionary["Type"])
            {
                case "weather":
                    {
                        WeatherWidget x = new WeatherWidget();
                        x.Location = (string)dictionary["Location"];
                        x.Current = (CurrentWeather)dictionary["Current"];
                        //x.Forcast = (List<WeatherForcastItem>)dictionary["Forcast"];

                        System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Forcast"]);
                        foreach (var item in itemss)
                        {
                            x.Forcast.Add(serializer.ConvertToType<WeatherForcastItem>(item));
                        }

                        return x;
                    };
                case "text":
                    {
                        TextWidget x = new TextWidget();
                        x.Content = (string)dictionary["Content"];
                        return x;
                    }; 
                case "keyValueText":
                    {
                        KeyValueTextWidget x = new KeyValueTextWidget();
                        x.Key = (string)dictionary["Key"];
                        x.Key = (string)dictionary["Value"];
                        x.Key = (string)dictionary["ValueURL"];
                        return x;
                    };
                case "keyValuesText":
                    {
                        KeyValuesTextWidget x = new KeyValuesTextWidget();
                        x.Key = (string)dictionary["Key"];
                        //x.Values = (List<ValueItem>)dictionary["ValueItem"];

                        System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["ValueItem"]);
                        foreach (var item in itemss)
                        {
                            x.Values.Add(serializer.ConvertToType<ValueItem>(item));
                        }


                        return x;


                    }; 
                case "url":
                    {
                        URLWidget x = new URLWidget();
                        x.ThumbnailImageURL = (string)dictionary["ThumbnailImageURL"];
                        x.Title = (string)dictionary["Title"];
                        x.URL = (string)dictionary["URL"];
                        x.HTMLContent = (string)dictionary["HTMLContent"];
                        return x;

                    }; 
                case "map":
                    {
                        MapWidget x = new MapWidget();
                        System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Pins"]);
                        foreach (var item in itemss)
                        {
                            x.Pins.Add(serializer.ConvertToType<MapPoint>(item));
                        }

                        //x.Pins = (List<MapPoint>)dictionary["Pins"];
                        return x;

                    }; 
                case "image":
                    {
                        ImageWidget x = new ImageWidget();
                        x.Title = (string)dictionary["Title"];
                        x.ImageURL = (string)dictionary["ImageURL"];
                        x.ThumbnailURL = (string)dictionary["ThumbnailURL"];
                        x.PageURL = (string)dictionary["PageURL"];
                        return x;
                    }; 
                case "html":
                    {
                        HTMLWidget x = new HTMLWidget();
                        x.Title = (string)dictionary["Title"];
                        x.HTML = (string)dictionary["HTML"];
                        return x;


                    }; 
                case "entity":
                    {
                        EntityWidget x = new EntityWidget();
                        x.SubType = (string)dictionary["SubType"];
                        x.Title = (string)dictionary["Title"];
                        x.Abstract = (string)dictionary["Abstract"];
                        x.ImageURL = (string)dictionary["ImageURL"];
                        x.Url = (string)dictionary["Url"];
                        return x;

                    }; 
                case "chart":
                    {
                        ChartWidget x = new ChartWidget();
                        x.Title = (string)dictionary["Title"];
                        //x.Categories = (List<string>)dictionary["Categories"];
                        System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Categories"]);
                        foreach (var item in itemss)
                        {
                            x.Categories.Add(serializer.ConvertToType<string>(item));
                        }



                        System.Collections.ArrayList itemss2 = ((System.Collections.ArrayList)dictionary["Data"]);
                        foreach (var item in itemss2)
                        {
                            x.Data.Add(serializer.ConvertToType<ChartsData>(item));
                        }

                        //x.Data = (List<ChartsData>)dictionary["Data"];
                        return x;
                    }; 
                case "businessEntity":
                    {
                        BusinessEntityWidget x = new BusinessEntityWidget();
                        x.SubType = (string)dictionary["SubType"];
                        x.Title = (string)dictionary["Title"];
                        x.Abstract = (string)dictionary["Abstract"];
                        x.ImageURL = (string)dictionary["ImageURL"];
                        x.URL = (string)dictionary["URL"];
                        //x.Attributes = (List<KeyValueTextWidget>)dictionary["Attributes"];
                        System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Attributes"]);
                        foreach (var item in itemss)
                        {
                            x.Attributes.Add(serializer.ConvertToType<KeyValueTextWidget>(item));
                        }

                        x.Address = (string)dictionary["Address"];
                        x.Phone = (string)dictionary["Phone"];
                        x.Lat = (double)dictionary["Lat"];
                        x.Lng = (double)dictionary["Lng"];


                        System.Collections.ArrayList itemss2 = ((System.Collections.ArrayList)dictionary["OtherURLs"]);
                        foreach (var item in itemss2)
                        {
                            x.OtherURLs.Add(serializer.ConvertToType<URLWidget>(item));
                        }
                        //x.OtherURLs = (List<URLWidget>)dictionary["OtherURLs"];

                        return x;



                    }; 

                case "list":
                    {
                        switch ((string)dictionary["SubType"])
                        {
                            case null:
                                {
                                    ListWidget x = new ListWidget();
                                    x.Title = (string)dictionary["Title"];
                                    System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Items"]);
                                    foreach (var item in itemss)
                                    {
                                        x.Items.Add(serializer.ConvertToType<APIResultWidget>(item));
                                    }
                                    return x;

                                }; 
                            case "videos":
                                {
                                    ListOfVideosWidget x = new ListOfVideosWidget();

                                    System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Items"]);
                                    foreach (var item in itemss)
                                    {
                                        x.Items.Add(serializer.ConvertToType<URLWidget>(item));
                                    }
                                    return x;
                                }; 
                            case "images":
                                {
                                    ListOfImagesWidget x = new ListOfImagesWidget();


                                    System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Items"]);
                                    foreach (var item in itemss)
                                    {
                                        x.Items.Add(serializer.ConvertToType<ImageWidget>(item));
                                    }
                                    return x;
                                }; 
                            case "webResults":
                                {

                                    ListOfWebsitesWidget x = new ListOfWebsitesWidget();

                                    System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Items"]);
                                    foreach (var item in itemss)
                                    {
                                        x.Items.Add(serializer.ConvertToType<URLWidget>(item));
                                    }
                                    return x;
                                }; 
                            case "businesses":
                                {
                                    ListOfBusinessesWidget x = new ListOfBusinessesWidget();
                                    x.Title = (string)dictionary["Title"];
                                    System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["Items"]);
                                    foreach (var item in itemss)
                                    {
                                        x.Items.Add(serializer.ConvertToType<BusinessEntityWidget>(item));
                                    }
                                    return x;
                                }; 



                        }
                    }; break;


            }
        }

        else //in case of objects not inheriting from the abstract class, in this case we identify each one by something else, not "type"
        {
            if (dictionary.ContainsKey("Day")) //WeatherForcastItem
            {
                WeatherForcastItem x = new WeatherForcastItem();
                x.Day = (string)dictionary["Day"];
                x.Hi = (string)dictionary["Hi"];
                x.Lo = (string)dictionary["Lo"];
                x.Status = (string)dictionary["Status"];
                x.IconURL = (string)dictionary["IconURL"];
                return x;

            }
            else if (dictionary.ContainsKey("Temprature")) // CurrentWeather
            {
                CurrentWeather x = new CurrentWeather();
                x.Temprature = (string)dictionary["Temprature"];
                x.Status = (string)dictionary["Status"];
                x.WindSpeed = (string)dictionary["WindSpeed"];
                x.WindDirection = (string)dictionary["WindDirection"];
                x.Humidity = (string)dictionary["Humidity"];
                x.IconURL = (string)dictionary["IconURL"];
                x.IsNight = (string)dictionary["IsNight"];
                return x;

            }
            else if (dictionary.ContainsKey("Lat")) //MapPoint
            {
                MapPoint x = new MapPoint();
                x.Title = (string)dictionary["Title"];
                x.Lat = (double)dictionary["Lat"];
                x.Lng = (double)dictionary["Lng"];
                return x;
            }
            else if (dictionary.ContainsKey("Value")) //ValueItem
            {
                ValueItem x = new ValueItem();
                x.Value = (string)dictionary["Value"];
                x.ValueURL = (string)dictionary["ValueURL"];
                return x;
            }
            else if (dictionary.ContainsKey("name")) //ChartsData
            {
                ChartsData x = new ChartsData();
                x.name = (string)dictionary["name"];
                System.Collections.ArrayList itemss = ((System.Collections.ArrayList)dictionary["name"]);
                foreach (var item in itemss)
                {
                    x.values.Add(serializer.ConvertToType<string>(item));
                }
                return x;
            }
        }
        return null;
    }

    public override IDictionary<string, object> Serialize(
 object obj,
 JavaScriptSerializer serializer)
    { return null; }

    private static readonly Type[] _supportedTypes = new[]
{
    typeof( APIResultWidget )
};

    public override IEnumerable<Type> SupportedTypes
    {
        get { return _supportedTypes; }
    }

}

应该将每个json对象映射到正确的类,然后使用deseriealizer相当容易:

that should map each json object to it's right class, the usage of the deseriealizer is fairly easy then:

var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new myCustomResolver() });
var dataObj = serializer.Deserialize<SearchAPIResult>(response);

这解决了我在抽象类中遇到的反序列化问题,因为它首先解决了类之间如何相互关联的问题.我不知道这是否是最正确的解决方案,但至少它解决了我的问题

this solved the deserialization issue I was having with the abstract classes, because it totally worked around how the classes are related to each other in the first place. I don't know if this is the most right solution or not, but at least it solved my porblem

这篇关于在没有typeNameHandling的情况下将Json对象反序列化为多态C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆