1515
1616import java .util .Objects ;
1717import java .math .BigDecimal ;
18+ import java .util .UUID ;
1819
1920
2021
@@ -63,6 +64,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
6364 return null ; // this class only serializes 'ScalarAnyOf' and its subtypes
6465 }
6566 final TypeAdapter <JsonElement > elementAdapter = gson .getAdapter (JsonElement .class );
67+ final TypeAdapter <UUID > adapterUUID = gson .getDelegateAdapter (this , TypeToken .get (UUID .class ));
6668 final TypeAdapter <String > adapterString = gson .getDelegateAdapter (this , TypeToken .get (String .class ));
6769 final TypeAdapter <BigDecimal > adapterBigDecimal = gson .getDelegateAdapter (this , TypeToken .get (BigDecimal .class ));
6870 final TypeAdapter <Boolean > adapterBoolean = gson .getDelegateAdapter (this , TypeToken .get (Boolean .class ));
@@ -75,6 +77,12 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
7577 return ;
7678 }
7779
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+ }
7886 // check if the actual instance is of the type `String`
7987 if (value .getActualInstance () instanceof String ) {
8088 JsonPrimitive primitive = adapterString .toJsonTree ((String )value .getActualInstance ()).getAsJsonPrimitive ();
@@ -93,7 +101,7 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
93101 elementAdapter .write (out , primitive );
94102 return ;
95103 }
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 " );
97105 }
98106
99107 @ Override
@@ -104,6 +112,19 @@ public ScalarAnyOf read(JsonReader in) throws IOException {
104112 ArrayList <String > errorMessages = new ArrayList <>();
105113 TypeAdapter actualAdapter = elementAdapter ;
106114
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+ }
107128 // deserialize String
108129 try {
109130 // validate the JSON object to see if any exception is thrown
@@ -169,6 +190,7 @@ public ScalarAnyOf(Object o) {
169190 }
170191
171192 static {
193+ schemas .put ("UUID" , UUID .class );
172194 schemas .put ("String" , String .class );
173195 schemas .put ("BigDecimal" , BigDecimal .class );
174196 schemas .put ("Boolean" , Boolean .class );
@@ -182,12 +204,17 @@ public Map<String, Class<?>> getSchemas() {
182204 /**
183205 * Set the instance that matches the anyOf child schema, check
184206 * the instance parameter is valid against the anyOf child schemas:
185- * BigDecimal, Boolean, String
207+ * BigDecimal, Boolean, String, UUID
186208 *
187209 * It could be an instance of the 'anyOf' schemas.
188210 */
189211 @ Override
190212 public void setActualInstance (Object instance ) {
213+ if (instance instanceof UUID ) {
214+ super .setActualInstance (instance );
215+ return ;
216+ }
217+
191218 if (instance instanceof String ) {
192219 super .setActualInstance (instance );
193220 return ;
@@ -203,21 +230,32 @@ public void setActualInstance(Object instance) {
203230 return ;
204231 }
205232
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 " );
207234 }
208235
209236 /**
210237 * Get the actual instance, which can be the following:
211- * BigDecimal, Boolean, String
238+ * BigDecimal, Boolean, String, UUID
212239 *
213- * @return The actual instance (BigDecimal, Boolean, String)
240+ * @return The actual instance (BigDecimal, Boolean, String, UUID )
214241 */
215242 @ SuppressWarnings ("unchecked" )
216243 @ Override
217244 public Object getActualInstance () {
218245 return super .getActualInstance ();
219246 }
220247
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+
221259 /**
222260 * Get the actual instance of `String`. If the actual instance is not `String`,
223261 * the ClassCastException will be thrown.
@@ -260,6 +298,14 @@ public Boolean getBoolean() throws ClassCastException {
260298 public static void validateJsonElement (JsonElement jsonElement ) throws IOException {
261299 // validate anyOf schemas one by one
262300 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+ }
263309 // validate the json string with String
264310 try {
265311 if (!jsonElement .getAsJsonPrimitive ().isString ()) {
@@ -290,7 +336,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
290336 errorMessages .add (String .format ("Deserialization for Boolean failed with `%s`." , e .getMessage ()));
291337 // continue to the next one
292338 }
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 ()));
294340 }
295341
296342 /**
0 commit comments