forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultSerializer.java
More file actions
311 lines (273 loc) · 13.9 KB
/
Copy pathDefaultSerializer.java
File metadata and controls
311 lines (273 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// ------------------------------------------------------------------------------
// Copyright (c) 2017 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ------------------------------------------------------------------------------
package com.microsoft.graph.serializer;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CaseFormat;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.graph.logger.ILogger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* The default serializer implementation for the SDK
*/
public class DefaultSerializer implements ISerializer {
/**
* The instance of the internal serializer
*/
private final Gson gson;
/**
* The logger
*/
private final ILogger logger;
/**
* Creates a DefaultSerializer
*
* @param logger the logger
*/
public DefaultSerializer(final ILogger logger) {
this.logger = logger;
this.gson = GsonFactory.getGsonInstance(logger);
}
/**
* Deserializes an object from the input string
*
* @param inputString the string that stores the representation of the item
* @param clazz the class of the item to be deserialized
* @param <T> the type of the item to be deserialized
* @return the deserialized item from the input string
*/
@Override
public <T> T deserializeObject(final String inputString, final Class<T> clazz) {
return deserializeObject(inputString, clazz, null);
}
@SuppressWarnings("unchecked")
@Override
public <T> T deserializeObject(final String inputString, final Class<T> clazz, Map<String, java.util.List<String>> responseHeaders) {
final T jsonObject = gson.fromJson(inputString, clazz);
// Populate the JSON-backed fields for any annotations that are not in the object model
if (jsonObject instanceof IJsonBackedObject) {
logger.logDebug("Deserializing type " + clazz.getSimpleName());
final JsonObject rawObject = gson.fromJson(inputString, JsonObject.class);
// If there is a derived class, try to get it and deserialize to it
Class<?> derivedClass = this.getDerivedClass(rawObject, clazz);
final T jo;
if (derivedClass != null) {
jo = (T) gson.fromJson(inputString, derivedClass);
} else {
jo = jsonObject;
}
final IJsonBackedObject jsonBackedObject = (IJsonBackedObject) jo;
jsonBackedObject.setRawObject(this, rawObject);
if (responseHeaders != null) {
JsonElement convertedHeaders = gson.toJsonTree(responseHeaders);
jsonBackedObject.additionalDataManager().put("graphResponseHeaders", convertedHeaders);
}
jsonBackedObject.additionalDataManager().setAdditionalData(rawObject);
setChildAdditionalData(jsonBackedObject,rawObject);
return jo;
} else {
logger.logDebug("Deserializing a non-IJsonBackedObject type " + clazz.getSimpleName());
return jsonObject;
}
}
/**
* Recursively sets additional data for each child object
*
* @param serializedObject the parent object whose children will be iterated to set additional data
* @param rawJson the raw json
*/
@SuppressWarnings("unchecked")
private void setChildAdditionalData(IJsonBackedObject serializedObject, JsonObject rawJson) {
// Use reflection to iterate through fields for eligible Graph children
for (java.lang.reflect.Field field : serializedObject.getClass().getFields()) {
try {
Object fieldObject = field.get(serializedObject);
// If the object is a HashMap, iterate through its children
if (fieldObject instanceof HashMap) {
@SuppressWarnings("unchecked")
HashMap<String, Object> serializableChildren = (HashMap<String, Object>) fieldObject;
Iterator<Entry<String, Object>> it = serializableChildren.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> pair = (Map.Entry<String, Object>)it.next();
Object child = pair.getValue();
// If the item is a valid Graph object, set its additional data
if (child instanceof IJsonBackedObject) {
AdditionalDataManager childAdditionalDataManager = ((IJsonBackedObject) child).additionalDataManager();
if(rawJson != null && field != null && rawJson.get(field.getName()) != null && rawJson.get(field.getName()).isJsonObject()
&& rawJson.get(field.getName()).getAsJsonObject().get(pair.getKey()).isJsonObject()) {
childAdditionalDataManager.setAdditionalData(rawJson.get(field.getName()).getAsJsonObject().get(pair.getKey()).getAsJsonObject());
setChildAdditionalData((IJsonBackedObject) child,rawJson.get(field.getName()).getAsJsonObject().get(pair.getKey()).getAsJsonObject());
}
}
}
}
// If the object is a valid Graph object, set its additional data
else if (fieldObject != null && fieldObject instanceof IJsonBackedObject) {
IJsonBackedObject serializedChild = (IJsonBackedObject) fieldObject;
AdditionalDataManager childAdditionalDataManager = serializedChild.additionalDataManager();
if(rawJson != null && field != null && rawJson.get(field.getName()) != null && rawJson.get(field.getName()).isJsonObject()) {
childAdditionalDataManager.setAdditionalData(rawJson.get(field.getName()).getAsJsonObject());
setChildAdditionalData((IJsonBackedObject) fieldObject,rawJson.get(field.getName()).getAsJsonObject());
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
logger.logError("Unable to access child fields of " + serializedObject.getClass().getSimpleName(), e);
}
}
}
/**
* Serializes an object into a string
*
* @param serializableObject the object to convert into a string
* @param <T> the type of the item to be serialized
* @return the string representation of that item
*/
@Override
public <T> String serializeObject(final T serializableObject) {
logger.logDebug("Serializing type " + serializableObject.getClass().getSimpleName());
JsonElement outJsonTree = gson.toJsonTree(serializableObject);
if (serializableObject instanceof IJsonBackedObject) {
IJsonBackedObject serializableJsonObject = (IJsonBackedObject) serializableObject;
AdditionalDataManager additionalData = serializableJsonObject.additionalDataManager();
// If the item is a valid Graph object, add its additional data
if (outJsonTree.isJsonObject()) {
JsonObject outJson = outJsonTree.getAsJsonObject();
addAdditionalDataToJson(additionalData, outJson);
outJson = getChildAdditionalData(serializableJsonObject, outJson);
outJsonTree = outJson;
}
}
return outJsonTree.toString();
}
/**
* Recursively populates additional data for each child object
*
* @param serializableObject the child to get additional data for
* @param outJson the serialized output JSON to add to
* @return the serialized output JSON including the additional child data
*/
@SuppressWarnings("unchecked")
private JsonObject getChildAdditionalData(IJsonBackedObject serializableObject, JsonObject outJson) {
// Use reflection to iterate through fields for eligible Graph children
for (java.lang.reflect.Field field : serializableObject.getClass().getFields()) {
try {
Object fieldObject = field.get(serializableObject);
// If the object is a HashMap, iterate through its children
if (fieldObject instanceof HashMap) {
HashMap<String, Object> serializableChildren = (HashMap<String, Object>) fieldObject;
Iterator<Entry<String, Object>> it = serializableChildren.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> pair = (Map.Entry<String, Object>)it.next();
Object child = pair.getValue();
// If the item is a valid Graph object, add its additional data
if (child instanceof IJsonBackedObject) {
AdditionalDataManager childAdditionalData = ((IJsonBackedObject) child).additionalDataManager();
addAdditionalDataToJson(
childAdditionalData,
outJson.getAsJsonObject(
field.getName())
.getAsJsonObject()
.get(pair.getKey()
.toString())
.getAsJsonObject());
// Serialize its children
outJson = getChildAdditionalData((IJsonBackedObject)child, outJson);
}
}
}
// If the object is a valid Graph object, add its additional data
else if (fieldObject != null && fieldObject instanceof IJsonBackedObject) {
IJsonBackedObject serializableChild = (IJsonBackedObject) fieldObject;
AdditionalDataManager childAdditionalData = serializableChild.additionalDataManager();
if(outJson != null && field != null && outJson.get(field.getName()) != null && outJson.get(field.getName()).isJsonObject()) {
addAdditionalDataToJson(childAdditionalData, outJson.get(field.getName()).getAsJsonObject());
}
// Serialize its children
outJson = getChildAdditionalData(serializableChild, outJson);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
logger.logError("Unable to access child fields of " + serializableObject.getClass().getSimpleName(), e);
}
}
return outJson;
}
/**
* Add each non-transient additional data property to the given JSON node
*
* @param additionalDataManager the additional data bag to iterate through
* @param jsonNode the JSON node to add the additional data properties to
*/
private void addAdditionalDataToJson(AdditionalDataManager additionalDataManager, JsonObject jsonNode) {
for (Map.Entry<String, JsonElement> entry : additionalDataManager.entrySet()) {
if (!fieldIsOdataTransient(entry)) {
jsonNode.add(
entry.getKey(),
entry.getValue()
);
}
}
}
private boolean fieldIsOdataTransient(Map.Entry<String, JsonElement> entry) {
return (entry.getKey().startsWith("@") && entry.getKey() != "@odata.type");
}
/**
* Get the derived class for the given JSON object
* This covers scenarios in which the service may return one of several derived types
* of a base object, which it defines using the odata.type parameter
*
* @param jsonObject the raw JSON object of the response
* @param parentClass the parent class the derived class should inherit from
* @return the derived class if found, or null if not applicable
*/
private Class<?> getDerivedClass(JsonObject jsonObject, Class<?> parentClass) {
//Identify the odata.type information if provided
if (jsonObject.get("@odata.type") != null) {
String odataType = jsonObject.get("@odata.type").getAsString();
String derivedType = odataType.substring(odataType.lastIndexOf('.') + 1); //Remove microsoft.graph prefix
derivedType = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, derivedType);
derivedType = "com.microsoft.graph.models.extensions." + derivedType; //Add full package path
try {
Class<?> derivedClass = Class.forName(derivedType);
//Check that the derived class inherits from the given parent class
if (parentClass.isAssignableFrom(derivedClass)) {
return derivedClass;
}
return null;
} catch (ClassNotFoundException e) {
logger.logDebug("Unable to find a corresponding class for derived type " + derivedType + ". Falling back to parent class.");
//If we cannot determine the derived type to cast to, return null
//This may happen if the API and the SDK are out of sync
return null;
}
}
//If there is no defined OData type, return null
return null;
}
@VisibleForTesting
public ILogger getLogger() {
return logger;
}
}