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