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