使用不同的类通过GSON映射JSON数据 [英] Using different classes to map JSON data with GSON

查看:99
本文介绍了使用不同的类通过GSON映射JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在从WebSocket连接接收JSON消息.

My application is receiving JSON messages from a WebSocket connection.

答案的类型不同,其格式如下:

There are different types of answers, which are formatted like that:

{
    "type": "snapshot",
    "product_id": "BTC-EUR",
    "bids": [["1", "2"]],
    "asks": [["2", "3"]]
}

{
    "type": "l2update",
    "product_id": "BTC-EUR",
    "changes": [
        ["buy", "1", "3"],
        ["sell", "3", "1"],
        ["sell", "2", "2"],
        ["sell", "4", "0"]
    ]
}

...例如(请参阅完整的API 此处).

... for example (see full API here).

根据类型",我希望GSON映射不同的类(例如Snapshot.class和l2update.class).

Depending on the "type", I would like GSON to map a different class (e.g. Snapshot.class and l2update.class).

我有订阅WebSocket连接的消息处理程序,并且我希望消息由相关处理程序处理.例如:

I have message handlers that subscribe to the WebSocket connection and I want the message to be processed by the relevant handler. For instance:

  • ErrorMessageHandler将管理错误
  • SnapshotMessageHandler将创建初始订单簿
  • L2UpdateMessageHandler将更新订单
  • 依此类推

我的问题是根据消息的类型来分发消息.

My problem is to dispatch the messages depending on their type.

我正在考虑将它们转换为适当的类,然后使用工厂调用相关的处理程序.我目前停留在第一步,根据类型"在Error.class或Snapshot.class中转换JSON.

I was thinking to convert them to the appropriate class and then call the relevant handler using a factory. I'm currently stuck at the first step, converting the JSON in Error.class or Snapshot.class depending on the "type".

我该怎么做?

推荐答案

对于Gson,您可以使用com.google.gson.typeadapters.RuntimeTypeAdapterFactory.

For Gson you could use com.google.gson.typeadapters.RuntimeTypeAdapterFactory.

假设您有-例如-以下课程:

Assuming you have - for example - following classes:

public class BaseResponse {
    private String type, product_id;
    // rest of the common fields
}

public class Snapshot extends BaseResponse {    
    // rest of the fields
}

public class L2Update extends BaseResponse {    
    // rest of the fields
}

然后您将按照以下RuntimeTypeAdapterFactory进行构建:

then you would build following RuntimeTypeAdapterFactory:

RuntimeTypeAdapterFactory<BaseResponse> runtimeTypeAdapterFactory = 
    RuntimeTypeAdapterFactory
        .of(BaseResponse.class, "type") // set the field where to look for value
        .registerSubtype(L2Update.class, "l2update") // values map to 'type' 
        .registerSubtype(Snapshot.class, "snapshot");// value in json

将其注册到Gson之后,将启用每种响应类型的自动实例化:

Registering this with Gson will then enable automativcal instantiation of each type of responses:

Gson gson = new GsonBuilder()
        .registerTypeAdapterFactory(runtimeTypeAdapterFactory).create();

并为fromJson(..)提供BaseResponse(如果使用的话),例如:

and provide BaseResponse for fromJson(..) if using it , like:

gson.fromJson( json , BaseResponse.class);

注意:Gson省略了-&序列化type字段.但是,需要在Json中进行设置.就像现在收到的回复一样.

NOTE: that Gson omits de- & serializing the type field. However it needs to be set in Json. Just as it is now in responses you get.

这篇关于使用不同的类通过GSON映射JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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