根据对象类型反序列化 JSON
我正在创建一个以 JSON 消息形式接收请求的服务。我需要解析消息并根据请求类型采取适当的操作。例如(在伪代码中):
switch(request.type) {
case "NewOrder":
createNewOrder(order);
break;
case "CancelOrder"
cancelOrder(orderId);
break;
}
似乎大多数 JSON API(至少是那些为您执行对象映射的 API)需要根对象类型来反序列化。有什么优雅的方法可以解决这个问题吗?
举个例子,在 Jackson API(使用完整对象映射)中,我需要按如下方式调用映射器:
NewOrder newOrder = mapper.readValue(src, NewOrder.class);
CancelOrder cancelOrder = mapper.readValue(src. CancelOrder.class);
这意味着我需要在解析对象之前就知道它的类。我真正需要的是某种方法来查看 JSON 字符串,确定请求类型,然后调用适当的 readValue() 方法 - 像这样:
String requestType = getRequestType(src);
switch(request.type) {
case "NewOrder":
NewOrder newOrder = mapper.readValue(src, NewOrder.class);
createNewOrder(newOrder.order);
break;
case "CancelOrder"
CancelOrder cancelOrder = mapper.readValue(src. CancelOrder.class);
cancelOrder(cancelOrder.orderId);
break;
}
Is it possible to do this using Jackson or any other Java JSON parser?我确信我可以进入较低的级别并使用流 API 或基于节点的 API,但如果可以的话,尽量避免这种复杂性。
I am creating a service that receives requests in the form of JSON messages. I need to parse the message and take the appropriate action based on the request type. For example (in pseudo code):
switch(request.type) {
case "NewOrder":
createNewOrder(order);
break;
case "CancelOrder"
cancelOrder(orderId);
break;
}
It seems that most JSON APIs (at least those that do the object mapping for you) need the root object type to deserialize. Is there any elegant way around this?
As an example, in the Jackson API (using full object mapping), I need to call the mapper as follows:
NewOrder newOrder = mapper.readValue(src, NewOrder.class);
CancelOrder cancelOrder = mapper.readValue(src. CancelOrder.class);
Which means that I need to know the object's class even before I have parsed it. What I really need is some way to peek into the JSON string, determine the request type and then call the appropriate readValue() method - something like this:
String requestType = getRequestType(src);
switch(request.type) {
case "NewOrder":
NewOrder newOrder = mapper.readValue(src, NewOrder.class);
createNewOrder(newOrder.order);
break;
case "CancelOrder"
CancelOrder cancelOrder = mapper.readValue(src. CancelOrder.class);
cancelOrder(cancelOrder.orderId);
break;
}
Is it possible to do this using Jackson or any other Java JSON parser? I am sure I can go to a lower level and use a streaming API or a node based API, but trying to avoid that complexity if I can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 Jackson 将 JSON 输入解析为 Map,则可以快速访问类型信息。然后,您可以创建该对象作为所需的类,并使用
ObjectMapper.convert
从 Jackson 获得的 Map 中配置该对象。这是一个例子:
If you use Jackson to parse the JSON input into a Map, you can quickly access the type information. You can then create the object as the required class and use
ObjectMapper.convert
to configure the object from the Map you got from Jackson.Here is an example:
您可以在订单周围使用包装器:
或
更新:
You could use a wrapper around your order:
or
UPDATE:
假设 Order 只是数据,将责任委托给 DoSomethingService 并通过工厂生成服务可能会有所帮助:
工厂将解析 JSON 对象:
具体服务是 Service 的子类:
Assuming the Order is just data, delegating responsibility to a DoSomethingService and producing the service via a factory might help:
The factory would parse the JSON object:
And the concrete services are sub-classes of Service:
谢谢莫里斯、西蒙和 nzroller。结合您所有回复的想法,这就是我想出的解决方案。欢迎反馈。
以下是序列化 NewOrder 的方法:
为了反序列化,我首先将 JSON 字符串读入 JsonNode 的树中。然后我读取了 messageType。最后,根据消息类型,我直接将有效负载读取为Java对象。
Thanks Maurice, Simon and nzroller. Combining ideas from all your responses here's the solution I came up with. Feedback welcome.
Here's how to serialize a NewOrder:
To deserialize, I first read the JSON string into a tree of JsonNode's. I then read the messageType. Finally, based on the message type, I read the payload directly as a Java object.