基于对象类型反序列化JSON [英] Deserializing JSON based on object type

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

问题描述

我正在创建一个以JSON消息的形式接收请求的服务。我需要解析消息并根据请求类型采取适当的操作。例如(伪代码):

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;
}

似乎大多数JSON API(至少那些执行对象映射的API) you)需要根对象类型来反序列化。有什么优雅的方法吗?

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?

例如,在Jackson API(使用完整对象映射)中,我需要按如下方式调用映射器:

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);

这意味着我需要在解析它之前知道对象的类。我真正需要的是一些方法来查看JSON字符串,确定请求类型,然后调用相应的readValue()方法 - 如下所示:

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;
}

是否可以使用Jackson或任何其他Java JSON解析器执行此操作?我确信我可以使用流API或基于节点的API,但是如果可以的话,尽量避免这种复杂性。

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.

推荐答案

如果您使用 Jackson 将JSON输入解析为Map,您可以快速访问类型信息。然后,您可以将对象创建为必需的类,并使用 ObjectMapper.convert 从您从Jackson获得的地图配置对象。

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.

以下是一个示例:

public class Test1 {
private String f;
private String b;

public void setFoo(String v) { f = v; }

public void setBim(String v) { b = v; }

public String toString() { return "f=" + f + ", b=" + b; }


public static void main(String[] args) throws Exception {
    String test = "{ \"foo\":\"bar\", \"bim\":\"baz\" }";
    ObjectMapper mapper = new ObjectMapper();
    HashMap map = mapper.readValue(new StringReader(test), HashMap.class);
    System.out.println(map);
    Test1 test1 = mapper.convertValue(map, Test1.class);
    System.out.println(test1);
}
}

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

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