Skip to content

Commit ec64d4b

Browse files
Amit JoshiAmit Joshi
authored andcommitted
Updated Json.java as it was earlier for attachment changes
1 parent 847da6d commit ec64d4b

File tree

1 file changed

+105
-102
lines changed

1 file changed

+105
-102
lines changed

src/main/java/sendinblue/Json.java

Lines changed: 105 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,67 @@
1010
* Do not edit the class manually.
1111
*/
1212

13-
14-
package sendinblue;
15-
16-
import com.google.gson.Gson;
17-
import com.google.gson.GsonBuilder;
18-
import com.google.gson.JsonParseException;
19-
import com.google.gson.TypeAdapter;
20-
import com.google.gson.internal.bind.util.ISO8601Utils;
21-
import com.google.gson.stream.JsonReader;
22-
import com.google.gson.stream.JsonWriter;
23-
import com.google.gson.JsonElement;
24-
import io.gsonfire.GsonFireBuilder;
25-
import io.gsonfire.TypeSelector;
26-
import org.threeten.bp.LocalDate;
27-
import org.threeten.bp.OffsetDateTime;
28-
import org.threeten.bp.format.DateTimeFormatter;
29-
30-
import sibModel.*;
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-
import java.util.Date;
39-
import java.util.Map;
40-
import java.util.HashMap;
13+
package sendinblue;
14+
15+
import com.google.gson.*;
16+
import com.google.gson.internal.bind.util.ISO8601Utils;
17+
import com.google.gson.stream.JsonReader;
18+
import com.google.gson.stream.JsonWriter;
19+
import io.gsonfire.GsonFireBuilder;
20+
import org.threeten.bp.LocalDate;
21+
import org.threeten.bp.OffsetDateTime;
22+
import org.threeten.bp.format.DateTimeFormatter;
23+
24+
import java.io.IOException;
25+
import java.io.StringReader;
26+
import java.lang.reflect.Type;
27+
import java.text.DateFormat;
28+
import java.text.ParseException;
29+
import java.text.ParsePosition;
30+
import java.util.Base64;
31+
import java.util.Date;
32+
import java.util.Map;
4133

4234
public class Json {
4335
private Gson gson;
4436
private boolean isLenientOnJson = false;
4537
private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
38+
private ByteArrayToBase64TypeAdapter byteArrayToBase64TypeAdapter = new ByteArrayToBase64TypeAdapter();
4639
private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
4740
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
4841
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
4942

43+
public Json() {
44+
gson = createGson()
45+
.registerTypeAdapter(Date.class, dateTypeAdapter)
46+
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
47+
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
48+
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
49+
.registerTypeHierarchyAdapter(byte[].class, byteArrayToBase64TypeAdapter)
50+
.create();
51+
}
52+
5053
public static GsonBuilder createGson() {
51-
GsonFireBuilder fireBuilder = new GsonFireBuilder()
52-
;
54+
GsonFireBuilder fireBuilder = new GsonFireBuilder();
5355
return fireBuilder.createGsonBuilder();
5456
}
5557

5658
private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
5759
JsonElement element = readElement.getAsJsonObject().get(discriminatorField);
58-
if(null == element) {
60+
if (null == element) {
5961
throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">");
6062
}
6163
return element.getAsString();
6264
}
6365

6466
private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
6567
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase());
66-
if(null == clazz) {
68+
if (null == clazz) {
6769
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
6870
}
6971
return clazz;
7072
}
7173

72-
public Json() {
73-
gson = createGson()
74-
.registerTypeAdapter(Date.class, dateTypeAdapter)
75-
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
76-
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
77-
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
78-
.create();
79-
}
80-
8174
/**
8275
* Get Gson.
8376
*
@@ -91,7 +84,7 @@ public Gson getGson() {
9184
* Set Gson.
9285
*
9386
* @param gson Gson
94-
* @return JSON
87+
* @return Json
9588
*/
9689
public Json setGson(Gson gson) {
9790
this.gson = gson;
@@ -104,20 +97,20 @@ public Json setLenientOnJson(boolean lenientOnJson) {
10497
}
10598

10699
/**
107-
* Serialize the given Java object into JSON string.
100+
* Serialize the given Java object into Json string.
108101
*
109102
* @param obj Object
110-
* @return String representation of the JSON
103+
* @return String representation of the Json
111104
*/
112105
public String serialize(Object obj) {
113106
return gson.toJson(obj);
114107
}
115108

116109
/**
117-
* Deserialize the given JSON string to Java object.
110+
* Deserialize the given Json string to Java object.
118111
*
119112
* @param <T> Type
120-
* @param body The JSON string
113+
* @param body The Json string
121114
* @param returnType The type to deserialize into
122115
* @return The deserialized Java object
123116
*/
@@ -133,14 +126,34 @@ public <T> T deserialize(String body, Type returnType) {
133126
return gson.fromJson(body, returnType);
134127
}
135128
} catch (JsonParseException e) {
136-
// Fallback processing when failed to parse JSON form response body:
129+
// Fallback processing when failed to parse Json form response body:
137130
// return the response body string directly for the String return type;
138131
if (returnType.equals(String.class))
139132
return (T) body;
140133
else throw (e);
141134
}
142135
}
143136

137+
public Json setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
138+
offsetDateTimeTypeAdapter.setFormat(dateFormat);
139+
return this;
140+
}
141+
142+
public Json setLocalDateFormat(DateTimeFormatter dateFormat) {
143+
localDateTypeAdapter.setFormat(dateFormat);
144+
return this;
145+
}
146+
147+
public Json setDateFormat(DateFormat dateFormat) {
148+
dateTypeAdapter.setFormat(dateFormat);
149+
return this;
150+
}
151+
152+
public Json setSqlDateFormat(DateFormat dateFormat) {
153+
sqlDateTypeAdapter.setFormat(dateFormat);
154+
return this;
155+
}
156+
144157
/**
145158
* Gson TypeAdapter for JSR310 OffsetDateTime type
146159
*/
@@ -178,64 +191,23 @@ public OffsetDateTime read(JsonReader in) throws IOException {
178191
default:
179192
String date = in.nextString();
180193
if (date.endsWith("+0000")) {
181-
date = date.substring(0, date.length()-5) + "Z";
194+
date = date.substring(0, date.length() - 5) + "Z";
182195
}
183196
return OffsetDateTime.parse(date, formatter);
184197
}
185198
}
186199
}
187200

188-
/**
189-
* Gson TypeAdapter for JSR310 LocalDate type
190-
*/
191-
public class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
192-
193-
private DateTimeFormatter formatter;
194-
195-
public LocalDateTypeAdapter() {
196-
this(DateTimeFormatter.ISO_LOCAL_DATE);
197-
}
198-
199-
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
200-
this.formatter = formatter;
201-
}
202-
203-
public void setFormat(DateTimeFormatter dateFormat) {
204-
this.formatter = dateFormat;
205-
}
206-
207-
@Override
208-
public void write(JsonWriter out, LocalDate date) throws IOException {
209-
if (date == null) {
210-
out.nullValue();
211-
} else {
212-
out.value(formatter.format(date));
213-
}
201+
public static class ByteArrayToBase64TypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
202+
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
203+
return Base64.getDecoder().decode(json.getAsString());
214204
}
215205

216-
@Override
217-
public LocalDate read(JsonReader in) throws IOException {
218-
switch (in.peek()) {
219-
case NULL:
220-
in.nextNull();
221-
return null;
222-
default:
223-
String date = in.nextString();
224-
return LocalDate.parse(date, formatter);
225-
}
206+
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
207+
return new JsonPrimitive(Base64.getEncoder().encodeToString(src));
226208
}
227209
}
228210

229-
public Json setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
230-
offsetDateTimeTypeAdapter.setFormat(dateFormat);
231-
return this;
232-
}
233-
234-
public Json setLocalDateFormat(DateTimeFormatter dateFormat) {
235-
localDateTypeAdapter.setFormat(dateFormat);
236-
return this;
237-
}
238-
239211
/**
240212
* Gson TypeAdapter for java.sql.Date type
241213
* If the dateFormat is null, a simple "yyyy-MM-dd" format will be used
@@ -349,14 +321,45 @@ public Date read(JsonReader in) throws IOException {
349321
}
350322
}
351323

352-
public Json setDateFormat(DateFormat dateFormat) {
353-
dateTypeAdapter.setFormat(dateFormat);
354-
return this;
355-
}
324+
/**
325+
* Gson TypeAdapter for JSR310 LocalDate type
326+
*/
327+
public class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
356328

357-
public Json setSqlDateFormat(DateFormat dateFormat) {
358-
sqlDateTypeAdapter.setFormat(dateFormat);
359-
return this;
329+
private DateTimeFormatter formatter;
330+
331+
public LocalDateTypeAdapter() {
332+
this(DateTimeFormatter.ISO_LOCAL_DATE);
333+
}
334+
335+
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
336+
this.formatter = formatter;
337+
}
338+
339+
public void setFormat(DateTimeFormatter dateFormat) {
340+
this.formatter = dateFormat;
341+
}
342+
343+
@Override
344+
public void write(JsonWriter out, LocalDate date) throws IOException {
345+
if (date == null) {
346+
out.nullValue();
347+
} else {
348+
out.value(formatter.format(date));
349+
}
350+
}
351+
352+
@Override
353+
public LocalDate read(JsonReader in) throws IOException {
354+
switch (in.peek()) {
355+
case NULL:
356+
in.nextNull();
357+
return null;
358+
default:
359+
String date = in.nextString();
360+
return LocalDate.parse(date, formatter);
361+
}
362+
}
360363
}
361364

362-
}
365+
}

0 commit comments

Comments
 (0)