如何处理 JSON 和变体对象?
我使用 .NET 的 JavascriptSerializer 将 JSON 反序列化为运行时对象,并且在大多数情况下,JSON 字段和对象字段之间的映射是自动的。但是,我面临以下情况,欢迎提供有关如何处理的建议。
假设我们有一个 Shape 的 JSON 表示,它可以是正方形或圆形。例如,
{"ShapeType":"Circle","Shape":{"Color":"Blue", "Radius":"5.3"}}
或
{"ShapeType":"Square","Shape":{"Color":"Red", "Side":"2.1"}}
这些 JSON 字符串是根据下面所示的类层次结构建模的。
class ShapePacket
{
public string ShapeType; // either "Square" or "Circle"
public Shape Shape;
}
class Shape // all Shapes have a Color
{
public string Color;
}
class Square : Shape
{
public float Side;
}
class Circle : Shape
{
public float Radius;
}
在这种涉及变体类型的情况下,简单地调用 JavascriptSerializer.Deserialize 是行不通的。尽管我的数据类型中有“分支”,有什么方法可以诱导 JavascriptSerializer 反序列化吗?我也对第三方解决方案持开放态度。
I'm using .NET's JavascriptSerializer
to deserialize JSON into runtime objects and for the most part the mapping between JSON fields and object fields has been automatic. However, I am faced with the following scenario and would welcome advice on how to handle it.
Imagine we have a JSON representation of a Shape, which can be a Square or a Circle. For example,
{"ShapeType":"Circle","Shape":{"Color":"Blue", "Radius":"5.3"}}
or
{"ShapeType":"Square","Shape":{"Color":"Red", "Side":"2.1"}}
These JSON strings are modeled after the class hierarchy shown below.
class ShapePacket
{
public string ShapeType; // either "Square" or "Circle"
public Shape Shape;
}
class Shape // all Shapes have a Color
{
public string Color;
}
class Square : Shape
{
public float Side;
}
class Circle : Shape
{
public float Radius;
}
Simply calling JavascriptSerializer.Deserialize
doesn't work in this case, where there is a variant type involved. Is there any way to coax JavascriptSerializer
to deserialize despite the "branch" in my data type? I am also open to third-party solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据类型中的分支可能需要代码中的分支。我不相信除了明确的方式之外还有其他方法可以做到这一点。
我将分两步完成此操作:
首先,使用
JsonConvert.DeserializeObject
将传入的 JSON 对象转换为无类型哈希,然后,在“ShapeType”字段上手动分支以选择适当的
Shape
类(Square
或Circle
),并自己构造一个实例。(这里包含了供后代使用的明确解决方案,尽管我怀疑您不需要我的帮助;)
The branch in your data type likely necessitates a branch in your code. I don't believe there's a way to do this besides the explicit way.
I would do this in two steps:
First, turn the incoming JSON object into a typeless hash using
JsonConvert.DeserializeObject
Then, manually branch on the 'ShapeType' field to choose the appropriate
Shape
class (Square
orCircle
), and construct an instance yourself.(explicit solution included here for posterity, although I suspect you don't need my help with it ;)
我认为您需要使用像这样的 JavaScriptTypeResolver 实现来初始化 JavascriptSerializer(SimpleTypeResolver 内置在类库中):
以便启用自动类型解析。我认为它会在输出 JSON 中添加一个 __type 字段,稍后将使用该字段来解析回类型。
I think you need to initialize the JavascriptSerializer with a JavaScriptTypeResolver implementation like this (SimpleTypeResolver is built-in in the class library):
in order to enable automatic type resolution. I think as a result it will add a __type field to the output JSON, which it will later use to resolve back the type.