2222
2323package com .microsoft .graph .serializer ;
2424
25- import com .google .common .base .CaseFormat ;
2625import com .google .gson .Gson ;
2726import com .google .gson .JsonArray ;
2827import com .google .gson .JsonElement ;
2928import com .google .gson .JsonObject ;
30-
3129import com .microsoft .graph .logger .ILogger ;
3230
3331import java .io .IOException ;
3836import java .util .Iterator ;
3937import java .util .List ;
4038import java .util .Map ;
41- import java .util .Objects ;
4239import java .util .Map .Entry ;
43-
40+ import java . util . Objects ;
4441import javax .annotation .Nonnull ;
4542import javax .annotation .Nullable ;
4643
4744/**
4845 * The default serializer implementation for the SDK
4946 */
5047public class DefaultSerializer implements ISerializer {
51- private static final String graphResponseHeadersKey = "graphResponseHeaders" ;
48+
49+ private static final String GRAPH_RESPONSE_HEADERS_KEY = "graphResponseHeaders" ;
50+
51+ /**
52+ * The logger
53+ */
54+ private final ILogger logger ;
5255
5356 /**
5457 * The instance of the internal serializer
5558 */
5659 private final Gson gson ;
5760
58- /**
59- * The logger
60- */
61- private final ILogger logger ;
6261
6362 /**
6463 * Creates a DefaultSerializer
@@ -104,16 +103,7 @@ public <T> T deserializeObject(@Nonnull final JsonElement rawElement, @Nonnull f
104103 if (jsonObject instanceof IJsonBackedObject ) {
105104 logger .logDebug ("Deserializing type " + clazz .getSimpleName ());
106105 final JsonObject rawObject = rawElement .isJsonObject () ? rawElement .getAsJsonObject () : null ;
107-
108- // If there is a derived class, try to get it and deserialize to it
109- T jo = jsonObject ;
110- if (rawElement .isJsonObject ()) {
111- final Class <?> derivedClass = this .getDerivedClass (rawObject , clazz );
112- if (derivedClass != null )
113- jo = (T ) gson .fromJson (rawElement , derivedClass );
114- }
115-
116- final IJsonBackedObject jsonBackedObject = (IJsonBackedObject ) jo ;
106+ final IJsonBackedObject jsonBackedObject = (IJsonBackedObject ) jsonObject ;
117107
118108 if (rawElement .isJsonObject ()) {
119109 jsonBackedObject .setRawObject (this , rawObject );
@@ -123,9 +113,9 @@ public <T> T deserializeObject(@Nonnull final JsonElement rawElement, @Nonnull f
123113
124114 if (responseHeaders != null ) {
125115 JsonElement convertedHeaders = gson .toJsonTree (responseHeaders );
126- jsonBackedObject .additionalDataManager ().put (graphResponseHeadersKey , convertedHeaders );
116+ jsonBackedObject .additionalDataManager ().put (GRAPH_RESPONSE_HEADERS_KEY , convertedHeaders );
127117 }
128- return jo ;
118+ return jsonObject ;
129119 } else {
130120 logger .logDebug ("Deserializing a non-IJsonBackedObject type " + clazz .getSimpleName ());
131121 return jsonObject ;
@@ -304,52 +294,12 @@ private void addAdditionalDataFromJsonObjectToJson (final Object item, final Jso
304294 */
305295 private void addAdditionalDataFromManagerToJson (AdditionalDataManager additionalDataManager , JsonObject jsonNode ) {
306296 for (Map .Entry <String , JsonElement > entry : additionalDataManager .entrySet ()) {
307- if (!entry .getKey ().equals (graphResponseHeadersKey )) {
297+ if (!entry .getKey ().equals (GRAPH_RESPONSE_HEADERS_KEY )) {
308298 jsonNode .add (entry .getKey (), entry .getValue ());
309299 }
310300 }
311301 }
312302
313- private final static String ODATA_TYPE_KEY = "@odata.type" ;
314- /**
315- * Get the derived class for the given JSON object
316- * This covers scenarios in which the service may return one of several derived types
317- * of a base object, which it defines using the odata.type parameter
318- *
319- * @param jsonObject the raw JSON object of the response
320- * @param parentClass the parent class the derived class should inherit from
321- * @return the derived class if found, or null if not applicable
322- */
323- @ Nullable
324- public Class <?> getDerivedClass (@ Nonnull final JsonObject jsonObject , @ Nullable final Class <?> parentClass ) {
325- Objects .requireNonNull (jsonObject , "parameter jsonObject cannot be null" );
326- //Identify the odata.type information if provided
327- if (jsonObject .get (ODATA_TYPE_KEY ) != null ) {
328- /** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */
329- final String odataType = jsonObject .get (ODATA_TYPE_KEY ).getAsString ();
330- final int lastDotIndex = odataType .lastIndexOf ("." );
331- final String derivedType = (odataType .substring (0 , lastDotIndex ) +
332- ".models." +
333- CaseFormat .LOWER_CAMEL .to (CaseFormat .UPPER_CAMEL ,
334- odataType .substring (lastDotIndex + 1 )))
335- .replace ("#" , "com." );
336- try {
337- Class <?> derivedClass = Class .forName (derivedType );
338- //Check that the derived class inherits from the given parent class
339- if (parentClass == null || parentClass .isAssignableFrom (derivedClass )) {
340- return derivedClass ;
341- }
342- return null ;
343- } catch (ClassNotFoundException e ) {
344- logger .logDebug ("Unable to find a corresponding class for derived type " + derivedType + ". Falling back to parent class." );
345- //If we cannot determine the derived type to cast to, return null
346- //This may happen if the API and the SDK are out of sync
347- return null ;
348- }
349- }
350- //If there is no defined OData type, return null
351- return null ;
352- }
353303
354304 /**
355305 * Gets the logger in use
0 commit comments