/app/src/main/java/com/whatshere/utils/JSONConverter.java

https://bitbucket.org/mycodecamp/grxtracking_app · Java · 101 lines · 82 code · 19 blank · 0 comment · 4 complexity · 7d04f3799675cca640d220bb0ead9fc3 MD5 · raw file

  1. package com.whatshere.utils;
  2. import java.io.Serializable;
  3. import java.lang.reflect.Type;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.List;
  9. import com.google.gson.Gson;
  10. import com.google.gson.GsonBuilder;
  11. import com.google.gson.JsonDeserializationContext;
  12. import com.google.gson.JsonDeserializer;
  13. import com.google.gson.JsonElement;
  14. import com.google.gson.JsonObject;
  15. import com.google.gson.JsonParseException;
  16. import com.google.gson.JsonPrimitive;
  17. import com.google.gson.JsonSerializationContext;
  18. import com.google.gson.JsonSerializer;
  19. import com.google.gson.reflect.TypeToken;
  20. import grx.tracking.core.persistence.Address;
  21. import grx.tracking.core.persistence.OrderInfo;
  22. import grx.tracking.core.persistence.PermissionDescr;
  23. public class JSONConverter {
  24. private Gson gson;
  25. public JSONConverter() {
  26. GsonBuilder builder = new GsonBuilder();
  27. builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
  28. public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
  29. throws JsonParseException {
  30. try {
  31. return new Date(json.getAsJsonPrimitive().getAsLong());
  32. } catch (Exception e) {
  33. return Calendar.getInstance().getTime();
  34. }
  35. }
  36. });
  37. builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
  38. @Override
  39. public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
  40. return src == null ? null : new JsonPrimitive(src.getTime());
  41. }
  42. });
  43. builder.registerTypeAdapter(Serializable.class, new JsonDeserializer<List<PermissionDescr>>() {
  44. public List<PermissionDescr> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
  45. throws JsonParseException {
  46. return (List) context.deserialize(json, List.class);
  47. }
  48. });
  49. builder.registerTypeAdapter(Address.class, new JsonDeserializer<Address>() {
  50. @Override
  51. public Address deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  52. Address address = new Address();
  53. if (json.isJsonObject()) {
  54. JsonObject jobj = (JsonObject) json;
  55. address.setEndereco(getMemberAsString(jobj, "endereco"));
  56. address.setXBairro(getMemberAsString(jobj, "xbairro"));
  57. address.setNro(getMemberAsString(jobj, "nro"));
  58. address.setXMun(getMemberAsString(jobj, "xmun"));
  59. }
  60. return address;
  61. }
  62. });
  63. this.gson = builder.create();
  64. }
  65. private String getMemberAsString(JsonObject obj, String member) {
  66. try {
  67. if (obj.has(member)) {
  68. JsonElement mm = obj.get(member);
  69. String str = mm.getAsString();
  70. return str != null ? str.trim() : str;
  71. }
  72. } catch (Exception e) {
  73. }
  74. return null;
  75. }
  76. public String toJson(Object obj) throws Exception {
  77. return this.gson.toJson(obj);
  78. }
  79. public <T> T toObject(String json, Class<T> classs) throws Exception {
  80. return this.gson.fromJson(json, classs);
  81. }
  82. }