11/*
22 * BandwidthLib
33 *
4- * This file was automatically generated by APIMATIC v2 .0 ( https://apimatic.io ).
4+ * This file was automatically generated by APIMATIC v3 .0 ( https://www. apimatic.io ).
55 */
66
77package com .bandwidth ;
1111import com .bandwidth .http .request .MultipartWrapper ;
1212import com .fasterxml .jackson .annotation .JsonFormat ;
1313import com .fasterxml .jackson .annotation .JsonGetter ;
14- import com .fasterxml .jackson .annotation .JsonInclude ;
1514import com .fasterxml .jackson .core .JsonProcessingException ;
1615import com .fasterxml .jackson .core .type .TypeReference ;
1716import com .fasterxml .jackson .databind .DeserializationFeature ;
2625import java .lang .annotation .Annotation ;
2726import java .lang .reflect .InvocationTargetException ;
2827import java .lang .reflect .Method ;
28+ import java .lang .reflect .Modifier ;
2929import java .math .BigDecimal ;
3030import java .net .URLEncoder ;
3131import java .util .AbstractMap .SimpleEntry ;
@@ -52,7 +52,6 @@ public class ApiHelper {
5252 private static final long serialVersionUID = -174113593500315394L ;
5353 {
5454 configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
55- setSerializationInclusion (JsonInclude .Include .NON_NULL );
5655 configOverride (BigDecimal .class ).setFormat (
5756 JsonFormat .Value .forShape (JsonFormat .Shape .STRING ));
5857 }
@@ -136,7 +135,6 @@ public static String serialize(Object obj, final JsonSerializer serializer)
136135 }.writeValueAsString (obj );
137136 }
138137
139-
140138 /**
141139 * Json deserialization of the given Json string using a specified JsonDerializer.
142140 * @param json The Json string to deserialize.
@@ -215,6 +213,24 @@ public static <T extends Object> T deserialize(String json, TypeReference<T> typ
215213 return mapper .readValue (json , typeReference );
216214 }
217215
216+ /**
217+ * Json deserialization of the given Json string.
218+ * @param json The Json string to deserialize
219+ * @return The deserialized Json as an Object
220+ */
221+ public static Object deserializeAsObject (String json ) {
222+ if (isNullOrWhiteSpace (json )) {
223+ return null ;
224+ }
225+ try {
226+ return ApiHelper .deserialize (json , new TypeReference <Object >() {});
227+ } catch (IOException e ) {
228+ // Failed to deserialize when json is not representing a JSON object.
229+ // i.e. either its string or any primitive type.
230+ return json ;
231+ }
232+ }
233+
218234 /**
219235 * JSON Deserialization of the given json string.
220236 * @param <T> The type of the object to deserialize into
@@ -558,42 +574,52 @@ private static void objectToList(String objName, Object obj,
558574 } else {
559575 // Process objects
560576 // Invoke getter methods
561- Method [] methods = obj .getClass ().getMethods ();
562- for (Method method : methods ) {
563- // Is a getter?
564- if (method .getParameterTypes ().length != 0 || !method .getName ().startsWith ("get" )) {
565- continue ;
566- }
577+ Class <?> clazz = obj .getClass ();
578+ while (clazz != null ) {
579+ for (Method method : clazz .getDeclaredMethods ()) {
580+
581+ // Is a public/protected getter or internalGetter?
582+ if (method .getParameterTypes ().length != 0
583+ || Modifier .isPrivate (method .getModifiers ())
584+ || (!method .getName ().startsWith ("get" )
585+ && !method .getName ().startsWith ("internalGet" ))) {
586+ continue ;
587+ }
567588
568- // Get Json attribute name
569- Annotation getterAnnotation = method .getAnnotation (JsonGetter .class );
570- if (getterAnnotation == null ) {
571- continue ;
572- }
589+ // Get JsonGetter annotation
590+ Annotation getterAnnotation = method .getAnnotation (JsonGetter .class );
591+ if (getterAnnotation == null ) {
592+ continue ;
593+ }
573594
574- // Load key name
575- String attribName = ((JsonGetter ) getterAnnotation ).value ();
576- if ((objName != null ) && (!objName .isEmpty ())) {
577- attribName = String .format ("%s[%s]" , objName , attribName );
578- }
595+ // Load key name from getter attribute name
596+ String attribName = ((JsonGetter ) getterAnnotation ).value ();
597+ if ((objName != null ) && (!objName .isEmpty ())) {
598+ attribName = String .format ("%s[%s]" , objName , attribName );
599+ }
579600
580- try {
581- // Load key value pair
582- Object value = method .invoke (obj );
583- JsonSerialize serializerAnnotation = method .getAnnotation (JsonSerialize .class );
584- if (serializerAnnotation != null ) {
585- loadKeyValuePairForEncoding (attribName , value , objectList , processed ,
586- serializerAnnotation );
587- } else {
588- loadKeyValuePairForEncoding (attribName , value , objectList , processed );
601+ try {
602+ // Load value by invoking getter method
603+ method .setAccessible (true );
604+ Object value = method .invoke (obj );
605+ JsonSerialize serializerAnnotation = method
606+ .getAnnotation (JsonSerialize .class );
607+ // Load key value pair into objectList
608+ if (serializerAnnotation != null ) {
609+ loadKeyValuePairForEncoding (attribName , value , objectList , processed ,
610+ serializerAnnotation );
611+ } else {
612+ loadKeyValuePairForEncoding (attribName , value , objectList , processed );
613+ }
614+ } catch (IllegalAccessException | IllegalArgumentException
615+ | InvocationTargetException e ) {
616+ // This block only calls getter methods.
617+ // These getters don't throw any exception except invocationTargetException.
618+ // The getters are public so there is no chance of an IllegalAccessException
619+ // Steps we've followed ensure that the object has the specified method.
589620 }
590- } catch (IllegalAccessException | IllegalArgumentException
591- | InvocationTargetException e ) {
592- // This block only calls getter methods.
593- // These getters don't throw any exception except invocationTargetException.
594- // The getters are public so there is no chance of an IllegalAccessException
595- // Steps we've followed ensure that the object has the specified method.
596621 }
622+ clazz = clazz .getSuperclass ();
597623 }
598624 }
599625 }
0 commit comments