15
15
16
16
import java .util .Objects ;
17
17
import java .math .BigDecimal ;
18
+ import java .util .UUID ;
18
19
19
20
20
21
@@ -63,6 +64,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
63
64
return null ; // this class only serializes 'ScalarAnyOf' and its subtypes
64
65
}
65
66
final TypeAdapter <JsonElement > elementAdapter = gson .getAdapter (JsonElement .class );
67
+ final TypeAdapter <UUID > adapterUUID = gson .getDelegateAdapter (this , TypeToken .get (UUID .class ));
66
68
final TypeAdapter <String > adapterString = gson .getDelegateAdapter (this , TypeToken .get (String .class ));
67
69
final TypeAdapter <BigDecimal > adapterBigDecimal = gson .getDelegateAdapter (this , TypeToken .get (BigDecimal .class ));
68
70
final TypeAdapter <Boolean > adapterBoolean = gson .getDelegateAdapter (this , TypeToken .get (Boolean .class ));
@@ -75,6 +77,12 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
75
77
return ;
76
78
}
77
79
80
+ // check if the actual instance is of the type `UUID`
81
+ if (value .getActualInstance () instanceof UUID ) {
82
+ JsonElement element = adapterUUID .toJsonTree ((UUID )value .getActualInstance ());
83
+ elementAdapter .write (out , element );
84
+ return ;
85
+ }
78
86
// check if the actual instance is of the type `String`
79
87
if (value .getActualInstance () instanceof String ) {
80
88
JsonPrimitive primitive = adapterString .toJsonTree ((String )value .getActualInstance ()).getAsJsonPrimitive ();
@@ -93,7 +101,7 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
93
101
elementAdapter .write (out , primitive );
94
102
return ;
95
103
}
96
- throw new IOException ("Failed to serialize as the type doesn't match anyOf schemas: BigDecimal, Boolean, String" );
104
+ throw new IOException ("Failed to serialize as the type doesn't match anyOf schemas: BigDecimal, Boolean, String, UUID " );
97
105
}
98
106
99
107
@ Override
@@ -104,6 +112,19 @@ public ScalarAnyOf read(JsonReader in) throws IOException {
104
112
ArrayList <String > errorMessages = new ArrayList <>();
105
113
TypeAdapter actualAdapter = elementAdapter ;
106
114
115
+ // deserialize UUID
116
+ try {
117
+ // validate the JSON object to see if any exception is thrown
118
+ UUID .fromString (jsonElement .getAsString ());
119
+ actualAdapter = adapterUUID ;
120
+ ScalarAnyOf ret = new ScalarAnyOf ();
121
+ ret .setActualInstance (actualAdapter .fromJsonTree (jsonElement ));
122
+ return ret ;
123
+ } catch (Exception e ) {
124
+ // deserialization failed, continue
125
+ errorMessages .add (String .format ("Deserialization for UUID failed with `%s`." , e .getMessage ()));
126
+ log .log (Level .FINER , "Input data does not match schema 'UUID'" , e );
127
+ }
107
128
// deserialize String
108
129
try {
109
130
// validate the JSON object to see if any exception is thrown
@@ -169,6 +190,7 @@ public ScalarAnyOf(Object o) {
169
190
}
170
191
171
192
static {
193
+ schemas .put ("UUID" , UUID .class );
172
194
schemas .put ("String" , String .class );
173
195
schemas .put ("BigDecimal" , BigDecimal .class );
174
196
schemas .put ("Boolean" , Boolean .class );
@@ -182,12 +204,17 @@ public Map<String, Class<?>> getSchemas() {
182
204
/**
183
205
* Set the instance that matches the anyOf child schema, check
184
206
* the instance parameter is valid against the anyOf child schemas:
185
- * BigDecimal, Boolean, String
207
+ * BigDecimal, Boolean, String, UUID
186
208
*
187
209
* It could be an instance of the 'anyOf' schemas.
188
210
*/
189
211
@ Override
190
212
public void setActualInstance (Object instance ) {
213
+ if (instance instanceof UUID ) {
214
+ super .setActualInstance (instance );
215
+ return ;
216
+ }
217
+
191
218
if (instance instanceof String ) {
192
219
super .setActualInstance (instance );
193
220
return ;
@@ -203,21 +230,32 @@ public void setActualInstance(Object instance) {
203
230
return ;
204
231
}
205
232
206
- throw new RuntimeException ("Invalid instance type. Must be BigDecimal, Boolean, String" );
233
+ throw new RuntimeException ("Invalid instance type. Must be BigDecimal, Boolean, String, UUID " );
207
234
}
208
235
209
236
/**
210
237
* Get the actual instance, which can be the following:
211
- * BigDecimal, Boolean, String
238
+ * BigDecimal, Boolean, String, UUID
212
239
*
213
- * @return The actual instance (BigDecimal, Boolean, String)
240
+ * @return The actual instance (BigDecimal, Boolean, String, UUID )
214
241
*/
215
242
@ SuppressWarnings ("unchecked" )
216
243
@ Override
217
244
public Object getActualInstance () {
218
245
return super .getActualInstance ();
219
246
}
220
247
248
+ /**
249
+ * Get the actual instance of `UUID`. If the actual instance is not `UUID`,
250
+ * the ClassCastException will be thrown.
251
+ *
252
+ * @return The actual instance of `UUID`
253
+ * @throws ClassCastException if the instance is not `UUID`
254
+ */
255
+ public UUID getUUID () throws ClassCastException {
256
+ return (UUID )super .getActualInstance ();
257
+ }
258
+
221
259
/**
222
260
* Get the actual instance of `String`. If the actual instance is not `String`,
223
261
* the ClassCastException will be thrown.
@@ -260,6 +298,14 @@ public Boolean getBoolean() throws ClassCastException {
260
298
public static void validateJsonElement (JsonElement jsonElement ) throws IOException {
261
299
// validate anyOf schemas one by one
262
300
ArrayList <String > errorMessages = new ArrayList <>();
301
+ // validate the json string with UUID
302
+ try {
303
+ UUID .fromString (jsonElement .getAsString ());
304
+ return ;
305
+ } catch (Exception e ) {
306
+ errorMessages .add (String .format ("Deserialization for UUID failed with `%s`." , e .getMessage ()));
307
+ // continue to the next one
308
+ }
263
309
// validate the json string with String
264
310
try {
265
311
if (!jsonElement .getAsJsonPrimitive ().isString ()) {
@@ -290,7 +336,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
290
336
errorMessages .add (String .format ("Deserialization for Boolean failed with `%s`." , e .getMessage ()));
291
337
// continue to the next one
292
338
}
293
- throw new IOException (String .format ("The JSON string is invalid for ScalarAnyOf with anyOf schemas: BigDecimal, Boolean, String. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s" , errorMessages , jsonElement .toString ()));
339
+ throw new IOException (String .format ("The JSON string is invalid for ScalarAnyOf with anyOf schemas: BigDecimal, Boolean, String, UUID . no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s" , errorMessages , jsonElement .toString ()));
294
340
}
295
341
296
342
/**
0 commit comments