You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TicketSerializer.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tickets;
  17. import java.lang.reflect.Type;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Date;
  21. import java.util.List;
  22. import com.gitblit.models.TicketModel;
  23. import com.gitblit.models.TicketModel.Change;
  24. import com.gitblit.models.TicketModel.Score;
  25. import com.gitblit.utils.ArrayUtils;
  26. import com.gitblit.utils.JsonUtils.ExcludeField;
  27. import com.gitblit.utils.JsonUtils.GmtDateTypeAdapter;
  28. import com.google.gson.ExclusionStrategy;
  29. import com.google.gson.Gson;
  30. import com.google.gson.GsonBuilder;
  31. import com.google.gson.JsonDeserializationContext;
  32. import com.google.gson.JsonDeserializer;
  33. import com.google.gson.JsonElement;
  34. import com.google.gson.JsonPrimitive;
  35. import com.google.gson.JsonSerializationContext;
  36. import com.google.gson.JsonSerializer;
  37. import com.google.gson.JsonSyntaxException;
  38. import com.google.gson.reflect.TypeToken;
  39. /**
  40. * Serializes and deserializes tickets, change, and journals.
  41. *
  42. * @author James Moger
  43. *
  44. */
  45. public class TicketSerializer {
  46. protected static final Type JOURNAL_TYPE = new TypeToken<Collection<Change>>() {}.getType();
  47. public static List<Change> deserializeJournal(String json) {
  48. Collection<Change> list = gson().fromJson(json, JOURNAL_TYPE);
  49. return new ArrayList<Change>(list);
  50. }
  51. public static TicketModel deserializeTicket(String json) {
  52. return gson().fromJson(json, TicketModel.class);
  53. }
  54. public static TicketLabel deserializeLabel(String json) {
  55. return gson().fromJson(json, TicketLabel.class);
  56. }
  57. public static TicketMilestone deserializeMilestone(String json) {
  58. return gson().fromJson(json, TicketMilestone.class);
  59. }
  60. public static String serializeJournal(List<Change> changes) {
  61. try {
  62. Gson gson = gson();
  63. return gson.toJson(changes);
  64. } catch (Exception e) {
  65. // won't happen
  66. }
  67. return null;
  68. }
  69. public static String serialize(TicketModel ticket) {
  70. if (ticket == null) {
  71. return null;
  72. }
  73. try {
  74. Gson gson = gson(
  75. new ExcludeField("com.gitblit.models.TicketModel$Attachment.content"),
  76. new ExcludeField("com.gitblit.models.TicketModel$Attachment.deleted"),
  77. new ExcludeField("com.gitblit.models.TicketModel$Comment.deleted"));
  78. return gson.toJson(ticket);
  79. } catch (Exception e) {
  80. // won't happen
  81. }
  82. return null;
  83. }
  84. public static String serialize(Change change) {
  85. if (change == null) {
  86. return null;
  87. }
  88. try {
  89. Gson gson = gson(
  90. new ExcludeField("com.gitblit.models.TicketModel$Attachment.content"));
  91. return gson.toJson(change);
  92. } catch (Exception e) {
  93. // won't happen
  94. }
  95. return null;
  96. }
  97. public static String serialize(TicketLabel label) {
  98. if (label == null) {
  99. return null;
  100. }
  101. try {
  102. Gson gson = gson();
  103. return gson.toJson(label);
  104. } catch (Exception e) {
  105. // won't happen
  106. }
  107. return null;
  108. }
  109. public static String serialize(TicketMilestone milestone) {
  110. if (milestone == null) {
  111. return null;
  112. }
  113. try {
  114. Gson gson = gson();
  115. return gson.toJson(milestone);
  116. } catch (Exception e) {
  117. // won't happen
  118. }
  119. return null;
  120. }
  121. // build custom gson instance with GMT date serializer/deserializer
  122. // http://code.google.com/p/google-gson/issues/detail?id=281
  123. public static Gson gson(ExclusionStrategy... strategies) {
  124. GsonBuilder builder = new GsonBuilder();
  125. builder.registerTypeAdapter(Date.class, new GmtDateTypeAdapter());
  126. builder.registerTypeAdapter(Score.class, new ScoreTypeAdapter());
  127. if (!ArrayUtils.isEmpty(strategies)) {
  128. builder.setExclusionStrategies(strategies);
  129. }
  130. return builder.create();
  131. }
  132. private static class ScoreTypeAdapter implements JsonSerializer<Score>, JsonDeserializer<Score> {
  133. private ScoreTypeAdapter() {
  134. }
  135. @Override
  136. public synchronized JsonElement serialize(Score score, Type type,
  137. JsonSerializationContext jsonSerializationContext) {
  138. return new JsonPrimitive(score.getValue());
  139. }
  140. @Override
  141. public synchronized Score deserialize(JsonElement jsonElement, Type type,
  142. JsonDeserializationContext jsonDeserializationContext) {
  143. try {
  144. int value = jsonElement.getAsInt();
  145. for (Score score : Score.values()) {
  146. if (score.getValue() == value) {
  147. return score;
  148. }
  149. }
  150. return Score.not_reviewed;
  151. } catch (Exception e) {
  152. throw new JsonSyntaxException(jsonElement.getAsString(), e);
  153. }
  154. }
  155. }
  156. }