37
37
import java .time .format .DateTimeFormatter ;
38
38
import java .util .Date ;
39
39
import java .util .Objects ;
40
+ import lombok .Getter ;
40
41
import okhttp3 .RequestBody ;
41
42
import okhttp3 .ResponseBody ;
42
43
import org .apache .fineract .client .models .ExternalId ;
43
44
import org .apache .fineract .client .util .adapter .ExternalIdAdapter ;
44
- import org .jetbrains .annotations .NotNull ;
45
45
import retrofit2 .Converter ;
46
46
import retrofit2 .Retrofit ;
47
47
import retrofit2 .converter .gson .GsonConverterFactory ;
51
51
// The original JSON class is deleted during the build (see FINERACT-1231).
52
52
public class JSON {
53
53
54
+ @ Getter
54
55
private final Gson gson ;
55
56
private final DateTypeAdapter dateTypeAdapter = new DateTypeAdapter ();
56
57
private final SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter ();
@@ -66,10 +67,6 @@ public JSON() {
66
67
.create ();
67
68
}
68
69
69
- public Gson getGson () {
70
- return gson ;
71
- }
72
-
73
70
/**
74
71
* GSON TypeAdapter for JSR-310 OffsetDateTime type.
75
72
*/
@@ -88,17 +85,16 @@ public void write(JsonWriter out, OffsetDateTime date) throws IOException {
88
85
89
86
@ Override
90
87
public OffsetDateTime read (JsonReader in ) throws IOException {
91
- switch (in .peek ()) {
92
- case NULL :
93
- in .nextNull ();
94
- return null ;
95
- default :
96
- String date = in .nextString ();
97
- if (date .endsWith ("+0000" )) {
98
- date = date .substring (0 , date .length () - 5 ) + "Z" ;
99
- }
100
- return OffsetDateTime .parse (date , formatter );
88
+ JsonToken token = in .peek ();
89
+ if (token == JsonToken .NULL ) {
90
+ in .nextNull ();
91
+ return null ;
92
+ }
93
+ String date = in .nextString ();
94
+ if (date .endsWith ("+0000" )) {
95
+ date = date .substring (0 , date .length () - 5 ) + "Z" ;
101
96
}
97
+ return OffsetDateTime .parse (date , formatter );
102
98
}
103
99
}
104
100
@@ -123,27 +119,32 @@ public void write(JsonWriter out, LocalDate date) throws IOException {
123
119
124
120
@ Override
125
121
public LocalDate read (JsonReader in ) throws IOException {
126
- switch (in .peek ()) {
127
- case NULL :
128
- in .nextNull ();
129
- return null ;
130
- case STRING :
131
- String dateString = in .nextString ();
132
- if (dateString != null && dateString .length () != 0 ) {
133
- return LocalDate .parse (dateString );
134
- }
135
- return null ;
136
- case BEGIN_ARRAY :
137
- in .beginArray ();
138
- int year = in .nextInt ();
139
- int month = in .nextInt ();
140
- int day = in .nextInt ();
141
- in .endArray ();
142
- return LocalDate .of (year , month , day );
143
- default :
144
- throw new JsonParseException (
145
- "Fineract's API normally always sends LocalDate as e.g. [2009,1,1].. or does it not?! (FINERACT-1220)" );
122
+ JsonToken token = in .peek ();
123
+
124
+ if (token == JsonToken .NULL ) {
125
+ in .nextNull ();
126
+ return null ;
127
+ }
128
+
129
+ if (token == JsonToken .STRING ) {
130
+ String dateString = in .nextString ();
131
+ if (dateString != null && !dateString .isEmpty ()) {
132
+ return LocalDate .parse (dateString );
133
+ }
134
+ return null ;
146
135
}
136
+
137
+ if (token == JsonToken .BEGIN_ARRAY ) {
138
+ in .beginArray ();
139
+ int year = in .nextInt ();
140
+ int month = in .nextInt ();
141
+ int day = in .nextInt ();
142
+ in .endArray ();
143
+ return LocalDate .of (year , month , day );
144
+ }
145
+
146
+ throw new JsonParseException (
147
+ "Fineract's API normally always sends LocalDate as e.g. [2009,1,1].. or does it not?! (FINERACT-1220)" );
147
148
}
148
149
}
149
150
@@ -172,20 +173,19 @@ public void write(JsonWriter out, java.sql.Date date) throws IOException {
172
173
173
174
@ Override
174
175
public java .sql .Date read (JsonReader in ) throws IOException {
175
- switch (in .peek ()) {
176
- case NULL :
177
- in .nextNull ();
178
- return null ;
179
- default :
180
- String date = in .nextString ();
181
- try {
182
- if (dateFormat != null ) {
183
- return new java .sql .Date (dateFormat .parse (date ).getTime ());
184
- }
185
- return new java .sql .Date (ISO8601Utils .parse (date , new ParsePosition (0 )).getTime ());
186
- } catch (ParseException e ) {
187
- throw new JsonParseException (e );
188
- }
176
+ JsonToken token = in .peek ();
177
+ if (token == JsonToken .NULL ) {
178
+ in .nextNull ();
179
+ return null ;
180
+ }
181
+ String date = in .nextString ();
182
+ try {
183
+ if (dateFormat != null ) {
184
+ return new java .sql .Date (dateFormat .parse (date ).getTime ());
185
+ }
186
+ return new java .sql .Date (ISO8601Utils .parse (date , new ParsePosition (0 )).getTime ());
187
+ } catch (ParseException e ) {
188
+ throw new JsonParseException (e );
189
189
}
190
190
}
191
191
}
@@ -226,7 +226,6 @@ public Date read(JsonReader in) throws IOException {
226
226
}
227
227
}
228
228
229
- @ NotNull
230
229
private Date parseDate (String date ) {
231
230
try {
232
231
if (null != dateFormat ) {
0 commit comments