Skip to content

Instantly share code, notes, and snippets.

@bilxio
Created August 1, 2018 03:22
Show Gist options
  • Select an option

  • Save bilxio/6ce559f28e92974aec08a2161aa6cdd8 to your computer and use it in GitHub Desktop.

Select an option

Save bilxio/6ce559f28e92974aec08a2161aa6cdd8 to your computer and use it in GitHub Desktop.
使用 GSON 解析 SpotPlanCreate 消息包
package cc.billworks.demo.spotplan;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class Message<T> implements Serializable {
private String apiVersion;
// 下发消息类型 <SpotPlanCreate>
private String type;
// 消息 ID
private String id;
// 消息创建日期
private Date createTime;
// 消息内容
private T data;
}
package cc.billworks.demo.spotplan;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class MessageTest {
@org.junit.Before
public void setUp() throws Exception {
}
@org.junit.After
public void tearDown() throws Exception {
}
@Test
public void testEncode() {
Message m = new Message();
Gson gson = new Gson();
m.setApiVersion("v1beta");
m.setCreateTime(new Date());
m.setId("m1");
m.setType("SpotPlanCreate");
SpotPlanRecord spotPlanRecord = new SpotPlanRecord();
spotPlanRecord.setSpFileId("f");
spotPlanRecord.setBuyingModel("F");
spotPlanRecord.setBuyingModelSubType("CCC");
spotPlanRecord.setOrderGroupName("babababa");
m.setData(spotPlanRecord);
String json = gson.toJson(m);
Assert.assertEquals("{}", json);
}
@Test
public void testDecode() {
String json = "{\"apiVersion\":\"v1beta\",\"type\":\"SpotPlanCreate\",\"id\":\"m1\",\"createTime\":\"Jul 23, 2018 6:24:30 PM\",\"data\":{\"spFileId\":\"f\",\"orderGroupName\":\"babababa\",\"buyingModel\":\"F\",\"buyingModelSubType\":\"CCC\"}}";
Gson gson = new Gson();
String messageType = null;
// 1. 按照 Map 解一级字段
Map<String, ?> metas = gson.fromJson(json, HashMap.class);
if (metas.containsKey("apiVersion") && "v1beta".equalsIgnoreCase(metas.get("apiVersion").toString())) {
// ...
} else {
throw new IllegalStateException(String.format("api version not support: %s", metas.get("apiVersion")));
}
if (metas.containsKey("type") && "SpotPlanCreate".equalsIgnoreCase(messageType = metas.get("type").toString())) {
// ...
} else {
throw new IllegalStateException(String.format("message type not support: %s", metas.get("type")));
}
// 2. 按照协议版本和消息类型解消息内容
// 使用泛型来反序列化准确的消息内容
switch (messageType) {
case "SpotPlanCreate": try {
System.out.println("------- >>> Message SpotPlanCreate Received : ");
// 这里就可以确定具体类型,使用泛型,结合 GSON 反序列化
Type typeToken = new TypeToken<Message<SpotPlanRecord>>(){ }.getType();
Message<SpotPlanRecord> m = gson.fromJson(json, typeToken);
System.out.println(m);
Assert.assertNotNull(m);
Assert.assertEquals("SpotPlanCreate", m.getType());
Assert.assertNotNull(m.getData());
Assert.assertEquals(m.getData().getSpFileId(), "f");
// ...
} catch (Exception e) {
// ...
}
default:
// ...
}
}
}
package cc.billworks.demo.spotplan;
import lombok.Data;
@Data
public class SpotPlanRecord {
private String spFileId;
private String orderGroupName;
private String buyingPlatform;
private String buyingModel;
private String buyingModelSubType;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment