/mustacheTemplates/libraries/retrofit2/JSON.mustache

https://github.com/sk89q/commandhelper · Mustache · 446 lines · 386 code · 60 blank · 0 comment · 45 complexity · 9fe476cb92e952949740accb569f6f44 MD5 · raw file

  1. {{>licenseInfo}}
  2. package {{invokerPackage}};
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.JsonParseException;
  6. import com.google.gson.TypeAdapter;
  7. import com.google.gson.internal.bind.util.ISO8601Utils;
  8. import com.google.gson.stream.JsonReader;
  9. import com.google.gson.stream.JsonWriter;
  10. import com.google.gson.JsonElement;
  11. import io.gsonfire.GsonFireBuilder;
  12. import io.gsonfire.TypeSelector;
  13. {{#joda}}
  14. import org.joda.time.DateTime;
  15. import org.joda.time.LocalDate;
  16. import org.joda.time.format.DateTimeFormatter;
  17. import org.joda.time.format.DateTimeFormatterBuilder;
  18. import org.joda.time.format.ISODateTimeFormat;
  19. {{/joda}}
  20. {{#threetenbp}}
  21. import org.threeten.bp.LocalDate;
  22. import org.threeten.bp.OffsetDateTime;
  23. import org.threeten.bp.format.DateTimeFormatter;
  24. {{/threetenbp}}
  25. {{#hasModel}}import {{modelPackage}}.*;{{/hasModel}}
  26. import java.io.IOException;
  27. import java.io.StringReader;
  28. import java.lang.reflect.Type;
  29. import java.text.DateFormat;
  30. import java.text.ParseException;
  31. import java.text.ParsePosition;
  32. {{#java8}}
  33. import java.time.LocalDate;
  34. import java.time.OffsetDateTime;
  35. import java.time.format.DateTimeFormatter;
  36. {{/java8}}
  37. import java.util.Date;
  38. import java.util.Map;
  39. import java.util.HashMap;
  40. public class JSON {
  41. private Gson gson;
  42. private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
  43. private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
  44. {{#joda}}
  45. private DateTimeTypeAdapter dateTimeTypeAdapter = new DateTimeTypeAdapter();
  46. private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
  47. {{/joda}}
  48. {{#jsr310}}
  49. private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
  50. private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
  51. {{/jsr310}}
  52. public static GsonBuilder createGson() {
  53. GsonFireBuilder fireBuilder = new GsonFireBuilder()
  54. {{#parent}}
  55. .registerTypeSelector({{classname}}.class, new TypeSelector() {
  56. @Override
  57. public Class getClassForElement(JsonElement readElement) {
  58. Map classByDiscriminatorValue = new HashMap();
  59. {{#children}}
  60. classByDiscriminatorValue.put("{{name}}".toUpperCase(), {{classname}}.class);
  61. {{/children}}
  62. classByDiscriminatorValue.put("{{classname}}".toUpperCase(), {{classname}}.class);
  63. return getClassByDiscriminator(
  64. classByDiscriminatorValue,
  65. getDiscriminatorValue(readElement, "{{discriminator.propertyName}}"));
  66. }
  67. })
  68. {{/parent}}
  69. ;
  70. return fireBuilder.createGsonBuilder();
  71. }
  72. private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
  73. JsonElement element = readElement.getAsJsonObject().get(discriminatorField);
  74. if(null == element) {
  75. throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">");
  76. }
  77. return element.getAsString();
  78. }
  79. private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
  80. Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase());
  81. if(null == clazz) {
  82. throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
  83. }
  84. return clazz;
  85. }
  86. public JSON() {
  87. gson = createGson()
  88. .registerTypeAdapter(Date.class, dateTypeAdapter)
  89. .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
  90. {{#joda}}
  91. .registerTypeAdapter(DateTime.class, dateTimeTypeAdapter)
  92. .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
  93. {{/joda}}
  94. {{#jsr310}}
  95. .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
  96. .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
  97. {{/jsr310}}
  98. .create();
  99. }
  100. /**
  101. * Get Gson.
  102. *
  103. * @return Gson
  104. */
  105. public Gson getGson() {
  106. return gson;
  107. }
  108. /**
  109. * Set Gson.
  110. *
  111. * @param gson Gson
  112. * @return JSON
  113. */
  114. public JSON setGson(Gson gson) {
  115. this.gson = gson;
  116. return this;
  117. }
  118. {{#joda}}
  119. /**
  120. * Gson TypeAdapter for Joda DateTime type
  121. */
  122. public static class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
  123. private DateTimeFormatter formatter;
  124. public DateTimeTypeAdapter() {
  125. this(new DateTimeFormatterBuilder()
  126. .append(ISODateTimeFormat.dateTime().getPrinter(), ISODateTimeFormat.dateOptionalTimeParser().getParser())
  127. .toFormatter());
  128. }
  129. public DateTimeTypeAdapter(DateTimeFormatter formatter) {
  130. this.formatter = formatter;
  131. }
  132. public void setFormat(DateTimeFormatter dateFormat) {
  133. this.formatter = dateFormat;
  134. }
  135. @Override
  136. public void write(JsonWriter out, DateTime date) throws IOException {
  137. if (date == null) {
  138. out.nullValue();
  139. } else {
  140. out.value(formatter.print(date));
  141. }
  142. }
  143. @Override
  144. public DateTime read(JsonReader in) throws IOException {
  145. switch (in.peek()) {
  146. case NULL:
  147. in.nextNull();
  148. return null;
  149. default:
  150. String date = in.nextString();
  151. return formatter.parseDateTime(date);
  152. }
  153. }
  154. }
  155. /**
  156. * Gson TypeAdapter for Joda LocalDate type
  157. */
  158. public class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
  159. private DateTimeFormatter formatter;
  160. public LocalDateTypeAdapter() {
  161. this(ISODateTimeFormat.date());
  162. }
  163. public LocalDateTypeAdapter(DateTimeFormatter formatter) {
  164. this.formatter = formatter;
  165. }
  166. public void setFormat(DateTimeFormatter dateFormat) {
  167. this.formatter = dateFormat;
  168. }
  169. @Override
  170. public void write(JsonWriter out, LocalDate date) throws IOException {
  171. if (date == null) {
  172. out.nullValue();
  173. } else {
  174. out.value(formatter.print(date));
  175. }
  176. }
  177. @Override
  178. public LocalDate read(JsonReader in) throws IOException {
  179. switch (in.peek()) {
  180. case NULL:
  181. in.nextNull();
  182. return null;
  183. default:
  184. String date = in.nextString();
  185. return formatter.parseLocalDate(date);
  186. }
  187. }
  188. }
  189. public JSON setDateTimeFormat(DateTimeFormatter dateFormat) {
  190. dateTimeTypeAdapter.setFormat(dateFormat);
  191. return this;
  192. }
  193. public JSON setLocalDateFormat(DateTimeFormatter dateFormat) {
  194. localDateTypeAdapter.setFormat(dateFormat);
  195. return this;
  196. }
  197. {{/joda}}
  198. {{#jsr310}}
  199. /**
  200. * Gson TypeAdapter for JSR310 OffsetDateTime type
  201. */
  202. public static class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
  203. private DateTimeFormatter formatter;
  204. public OffsetDateTimeTypeAdapter() {
  205. this(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  206. }
  207. public OffsetDateTimeTypeAdapter(DateTimeFormatter formatter) {
  208. this.formatter = formatter;
  209. }
  210. public void setFormat(DateTimeFormatter dateFormat) {
  211. this.formatter = dateFormat;
  212. }
  213. @Override
  214. public void write(JsonWriter out, OffsetDateTime date) throws IOException {
  215. if (date == null) {
  216. out.nullValue();
  217. } else {
  218. out.value(formatter.format(date));
  219. }
  220. }
  221. @Override
  222. public OffsetDateTime read(JsonReader in) throws IOException {
  223. switch (in.peek()) {
  224. case NULL:
  225. in.nextNull();
  226. return null;
  227. default:
  228. String date = in.nextString();
  229. if (date.endsWith("+0000")) {
  230. date = date.substring(0, date.length()-5) + "Z";
  231. }
  232. return OffsetDateTime.parse(date, formatter);
  233. }
  234. }
  235. }
  236. /**
  237. * Gson TypeAdapter for JSR310 LocalDate type
  238. */
  239. public class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
  240. private DateTimeFormatter formatter;
  241. public LocalDateTypeAdapter() {
  242. this(DateTimeFormatter.ISO_LOCAL_DATE);
  243. }
  244. public LocalDateTypeAdapter(DateTimeFormatter formatter) {
  245. this.formatter = formatter;
  246. }
  247. public void setFormat(DateTimeFormatter dateFormat) {
  248. this.formatter = dateFormat;
  249. }
  250. @Override
  251. public void write(JsonWriter out, LocalDate date) throws IOException {
  252. if (date == null) {
  253. out.nullValue();
  254. } else {
  255. out.value(formatter.format(date));
  256. }
  257. }
  258. @Override
  259. public LocalDate read(JsonReader in) throws IOException {
  260. switch (in.peek()) {
  261. case NULL:
  262. in.nextNull();
  263. return null;
  264. default:
  265. String date = in.nextString();
  266. return LocalDate.parse(date, formatter);
  267. }
  268. }
  269. }
  270. public JSON setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
  271. offsetDateTimeTypeAdapter.setFormat(dateFormat);
  272. return this;
  273. }
  274. public JSON setLocalDateFormat(DateTimeFormatter dateFormat) {
  275. localDateTypeAdapter.setFormat(dateFormat);
  276. return this;
  277. }
  278. {{/jsr310}}
  279. /**
  280. * Gson TypeAdapter for java.sql.Date type
  281. * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used
  282. * (more efficient than SimpleDateFormat).
  283. */
  284. public static class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
  285. private DateFormat dateFormat;
  286. public SqlDateTypeAdapter() {
  287. }
  288. public SqlDateTypeAdapter(DateFormat dateFormat) {
  289. this.dateFormat = dateFormat;
  290. }
  291. public void setFormat(DateFormat dateFormat) {
  292. this.dateFormat = dateFormat;
  293. }
  294. @Override
  295. public void write(JsonWriter out, java.sql.Date date) throws IOException {
  296. if (date == null) {
  297. out.nullValue();
  298. } else {
  299. String value;
  300. if (dateFormat != null) {
  301. value = dateFormat.format(date);
  302. } else {
  303. value = date.toString();
  304. }
  305. out.value(value);
  306. }
  307. }
  308. @Override
  309. public java.sql.Date read(JsonReader in) throws IOException {
  310. switch (in.peek()) {
  311. case NULL:
  312. in.nextNull();
  313. return null;
  314. default:
  315. String date = in.nextString();
  316. try {
  317. if (dateFormat != null) {
  318. return new java.sql.Date(dateFormat.parse(date).getTime());
  319. }
  320. return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
  321. } catch (ParseException e) {
  322. throw new JsonParseException(e);
  323. }
  324. }
  325. }
  326. }
  327. /**
  328. * Gson TypeAdapter for java.util.Date type
  329. * If the dateFormat is null, ISO8601Utils will be used.
  330. */
  331. public static class DateTypeAdapter extends TypeAdapter<Date> {
  332. private DateFormat dateFormat;
  333. public DateTypeAdapter() {
  334. }
  335. public DateTypeAdapter(DateFormat dateFormat) {
  336. this.dateFormat = dateFormat;
  337. }
  338. public void setFormat(DateFormat dateFormat) {
  339. this.dateFormat = dateFormat;
  340. }
  341. @Override
  342. public void write(JsonWriter out, Date date) throws IOException {
  343. if (date == null) {
  344. out.nullValue();
  345. } else {
  346. String value;
  347. if (dateFormat != null) {
  348. value = dateFormat.format(date);
  349. } else {
  350. value = ISO8601Utils.format(date, true);
  351. }
  352. out.value(value);
  353. }
  354. }
  355. @Override
  356. public Date read(JsonReader in) throws IOException {
  357. try {
  358. switch (in.peek()) {
  359. case NULL:
  360. in.nextNull();
  361. return null;
  362. default:
  363. String date = in.nextString();
  364. try {
  365. if (dateFormat != null) {
  366. return dateFormat.parse(date);
  367. }
  368. return ISO8601Utils.parse(date, new ParsePosition(0));
  369. } catch (ParseException e) {
  370. throw new JsonParseException(e);
  371. }
  372. }
  373. } catch (IllegalArgumentException e) {
  374. throw new JsonParseException(e);
  375. }
  376. }
  377. }
  378. public JSON setDateFormat(DateFormat dateFormat) {
  379. dateTypeAdapter.setFormat(dateFormat);
  380. return this;
  381. }
  382. public JSON setSqlDateFormat(DateFormat dateFormat) {
  383. sqlDateTypeAdapter.setFormat(dateFormat);
  384. return this;
  385. }
  386. }