1 | package omq.common.util.Serializers; |
---|
2 | |
---|
3 | import java.util.List; |
---|
4 | |
---|
5 | import omq.common.event.Event; |
---|
6 | import omq.common.message.Request; |
---|
7 | import omq.common.message.Response; |
---|
8 | import omq.exception.OmqException; |
---|
9 | import omq.exception.SerializerException; |
---|
10 | import omq.server.RemoteObject; |
---|
11 | |
---|
12 | import com.google.gson.Gson; |
---|
13 | import com.google.gson.JsonArray; |
---|
14 | import com.google.gson.JsonElement; |
---|
15 | import com.google.gson.JsonObject; |
---|
16 | import com.google.gson.JsonParser; |
---|
17 | |
---|
18 | public class GsonImp implements ISerializer { |
---|
19 | private final Gson gson = new Gson(); |
---|
20 | |
---|
21 | @Override |
---|
22 | public byte[] serialize(Object obj) throws SerializerException { |
---|
23 | String json = gson.toJson(obj); |
---|
24 | return json.getBytes(); |
---|
25 | } |
---|
26 | |
---|
27 | @Override |
---|
28 | public Request deserializeRequest(byte[] bytes, RemoteObject obj) throws SerializerException { |
---|
29 | String json = new String(bytes); |
---|
30 | |
---|
31 | JsonParser parser = new JsonParser(); |
---|
32 | JsonObject jsonObj = parser.parse(json).getAsJsonObject(); |
---|
33 | |
---|
34 | String id = jsonObj.get("id").getAsString(); |
---|
35 | String method = jsonObj.get("method").getAsString(); |
---|
36 | |
---|
37 | List<Class<?>> types = obj.getParams(method); |
---|
38 | |
---|
39 | try { |
---|
40 | JsonArray jsonArgs = (JsonArray) jsonObj.get("params"); |
---|
41 | |
---|
42 | // TODO: if (jsonArgs.size() == types.size()) |
---|
43 | int length = jsonArgs.size(); |
---|
44 | Object[] arguments = new Object[length]; |
---|
45 | |
---|
46 | int i = 0; |
---|
47 | for (JsonElement element : jsonArgs) { |
---|
48 | arguments[i] = gson.fromJson(element, types.get(i)); |
---|
49 | i++; |
---|
50 | } |
---|
51 | |
---|
52 | return new Request(id, method, arguments); |
---|
53 | } catch (NullPointerException e) { |
---|
54 | return new Request(id, method, null); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | public Response deserializeResponse(byte[] bytes, Class<?> type) throws SerializerException { |
---|
60 | String json = new String(bytes); |
---|
61 | |
---|
62 | JsonParser parser = new JsonParser(); |
---|
63 | JsonObject jsonObj = parser.parse(json).getAsJsonObject(); |
---|
64 | |
---|
65 | String id = jsonObj.get("id").getAsString(); |
---|
66 | String idOmq = jsonObj.get("idOmq").getAsString(); |
---|
67 | |
---|
68 | JsonElement jsonElement = jsonObj.get("result"); |
---|
69 | Object result = gson.fromJson(jsonElement, type); |
---|
70 | |
---|
71 | JsonElement jsonError = jsonObj.get("error"); |
---|
72 | OmqException error = gson.fromJson(jsonError, OmqException.class); |
---|
73 | |
---|
74 | return new Response(id, idOmq, result, error); |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public Event deserializeEvent(byte[] bytes) throws SerializerException { |
---|
79 | try { |
---|
80 | String json = new String(bytes); |
---|
81 | |
---|
82 | JsonParser parser = new JsonParser(); |
---|
83 | JsonObject jsonObj = parser.parse(json).getAsJsonObject(); |
---|
84 | |
---|
85 | String type = jsonObj.get("type").getAsString(); |
---|
86 | |
---|
87 | JsonElement jsonElement = jsonObj.get("event"); |
---|
88 | Event event; |
---|
89 | |
---|
90 | event = (Event) gson.fromJson(jsonElement, Class.forName(type)); |
---|
91 | |
---|
92 | return event; |
---|
93 | } catch (Exception e) { |
---|
94 | throw new SerializerException("Deserialize event", e.getCause()); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|