JSON.NET - 条件类型反序列化 [英] JSON.NET - Conditional Type Deserialization

查看:114
本文介绍了JSON.NET - 条件类型反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我消耗了一些ArcGIS Web服务,而且他们有一些不幸的JSON设计。例如,他们可能会放弃这样的:

I'm consuming some ARCGis web services, and they have some unfortunate JSON design. for example, they might give something like this:

{
geometryType: "esriGeometryPolygon"
geometry: {
-rings: [
-[.blah..... }}

现在,根据传递在 geometryType 的值时,的几何的对象可能是几种不同的对象类型之一。在上述情况下,该几何节点的类型的多边形

Now, depending on the geometryType value passed in, the geometry object may be one of several different object types. in the case above, the geometry node is of type Polygon.

所以,问题是;在JSON.NET有什么办法notate这个条件打字?如果不是(我怀疑有),有没有建立反序列化的提供商的方式几何的节点,基于上述对象的信息?如果不是,是否有解决这个任何推荐的方式。

so, question is; in JSON.NET is there any way to notate this conditional typing? if not (which i doubt there is), is there a way to build a provider for deserializing that geometry node, based on the object info above? if not, are there any recommended ways for solving this?

编辑:我看起来相当广泛投入建设一个自定义的转换器,但与转换器的问题是,他们有这个抽象方法:

edit: i looked pretty extensively into building a custom converter, but the problem with the converter is that they have this abstract method:

public override T Create (Type objectType)

不过,我不知道要在这里建立什么类型的方式,我需要知道什么样的对象是在上面的JSON指定。

however, i have no way of knowing what type to create here, i need to know what kind of object was specified in the JSON above.

谢谢!

推荐答案

我把一个样品转换器,你指出正确的方向。下面是我的示例JSON字符串:

I put together a sample converter to point you in the right direction. Here are my sample JSON strings:

{geometryType:esriGeometryPolygon几何:{环:5}}

{geometryType: "esriGeometryPolygon", geometry: { rings: 5 } }

{geometryType:esriGeometryOther几何:{环:5}}

{geometryType: "esriGeometryOther", geometry: { rings: 5 } }

我测试了它像这样的:

var serializer = new JsonSerializer();
var geometry = serializer.Deserialize<Geometry>(new JsonTextReader(new StringReader(jsonData)));

//Should have correctly typed instance here...



在这里是转换器和样品几何对象:

And here is the converter and sample geometry objects:

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

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

        reader.Read(); // startobject

        //we should be at geometry type property now
        if ((string)reader.Value != "geometryType") throw new InvalidOperationException();

        reader.Read(); //propertyName

        var type = (string)reader.Value;

        Geometry value;

        switch(type)
        {
            case "esriGeometryPolygon":
                value = new PolygonGeometry();
                break;
            case "esriGeometryOther":
                value = new OtherGeometry();
                break;
            default:
                throw new NotSupportedException();
        }

        reader.Read(); // move to inner object property
        //should probably confirm name here

        reader.Read(); //move to inner object

        serializer.Populate(reader, value);

        reader.Read(); //move outside container (should be end object)

        return value;
    }

    public override bool CanConvert(Type objectType)
    {
        return typeof(Geometry).IsAssignableFrom(objectType);
    }
}

[JsonConverter(typeof(GeometryConverter))]
public class OtherGeometry : Geometry
{

}

[JsonConverter(typeof(GeometryConverter))]
public class PolygonGeometry : Geometry
{

}

[JsonConverter(typeof(GeometryConverter))]
public class Geometry
{
    public int rings { get; set; }
}

这篇关于JSON.NET - 条件类型反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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