source: trunk/src/main/java/omq/common/util/Serializers/GsonImp.java @ 44

Last change on this file since 44 was 44, checked in by stoda, 11 years ago

Objectmq converted to maven project

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