|
28 | 28 | import com.google.gson.JsonObject; |
29 | 29 | import com.microsoft.graph.logger.ILogger; |
30 | 30 |
|
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Iterator; |
31 | 33 | import java.util.Map; |
32 | 34 |
|
33 | 35 | /** |
@@ -56,7 +58,7 @@ public DefaultSerializer(final ILogger logger) { |
56 | 58 | } |
57 | 59 |
|
58 | 60 | /** |
59 | | - * Deserialize an object from the input string. |
| 61 | + * Deserializes an object from the input string. |
60 | 62 | * |
61 | 63 | * @param inputString The string that stores the representation of the item. |
62 | 64 | * @param clazz The class of the item to be deserialized. |
@@ -101,28 +103,100 @@ public <T> String serializeObject(final T serializableObject) { |
101 | 103 | JsonElement outJsonTree = gson.toJsonTree(serializableObject); |
102 | 104 |
|
103 | 105 | if (serializableObject instanceof IJsonBackedObject) { |
104 | | - AdditionalDataManager additionalData = |
105 | | - ((IJsonBackedObject) serializableObject) |
106 | | - .additionalDataManager(); |
| 106 | + IJsonBackedObject serializableJsonObject = (IJsonBackedObject) serializableObject; |
| 107 | + |
| 108 | + AdditionalDataManager additionalData = serializableJsonObject.additionalDataManager(); |
| 109 | + |
| 110 | + // If the item is a valid Graph object, add its additional data |
107 | 111 | if (outJsonTree.isJsonObject()) { |
108 | 112 | JsonObject outJson = outJsonTree.getAsJsonObject(); |
109 | | - for (Map.Entry<String, JsonElement> entry : additionalData.entrySet()) { |
110 | | - if (!fieldIsOdataTransient(entry)) { |
111 | | - outJson.add( |
112 | | - entry.getKey(), |
113 | | - entry.getValue() |
114 | | - ); |
115 | | - } |
116 | | - } |
| 113 | + |
| 114 | + addAdditionalDataToJson(additionalData, outJson); |
| 115 | + outJson = getChildAdditionalData(serializableJsonObject, outJson); |
| 116 | + |
117 | 117 | outJsonTree = outJson; |
118 | 118 | } |
119 | 119 | } |
120 | 120 |
|
121 | 121 | return outJsonTree.toString(); |
122 | 122 | } |
| 123 | + |
| 124 | + /** |
| 125 | + * Recursively populates additional data for each child object |
| 126 | + * |
| 127 | + * @param serializableObject The child to get additional data for |
| 128 | + * @param outJson The serialized output JSON to add to |
| 129 | + * @return The serialized output JSON including the additional child data |
| 130 | + */ |
| 131 | + private JsonObject getChildAdditionalData(IJsonBackedObject serializableObject, JsonObject outJson) { |
| 132 | + // Use reflection to iterate through fields for eligible Graph children |
| 133 | + for (java.lang.reflect.Field field : serializableObject.getClass().getFields()) { |
| 134 | + try { |
| 135 | + Object fieldObject = field.get(serializableObject); |
| 136 | + |
| 137 | + // If the object is a HashMap, iterate through its children |
| 138 | + if (fieldObject instanceof HashMap) { |
| 139 | + HashMap<String, Object> serializableChildren = (HashMap<String, Object>) fieldObject; |
| 140 | + Iterator it = serializableChildren.entrySet().iterator(); |
| 141 | + |
| 142 | + while (it.hasNext()) { |
| 143 | + HashMap.Entry<String, Object> pair = (HashMap.Entry<String, Object>)it.next(); |
| 144 | + Object child = pair.getValue(); |
| 145 | + |
| 146 | + // If the item is a valid Graph object, add its additional data |
| 147 | + if (child instanceof IJsonBackedObject) { |
| 148 | + AdditionalDataManager childAdditionalData = ((IJsonBackedObject) child).additionalDataManager(); |
| 149 | + |
| 150 | + addAdditionalDataToJson( |
| 151 | + childAdditionalData, |
| 152 | + outJson.getAsJsonObject( |
| 153 | + field.getName()) |
| 154 | + .getAsJsonObject() |
| 155 | + .get(pair.getKey() |
| 156 | + .toString()) |
| 157 | + .getAsJsonObject()); |
| 158 | + |
| 159 | + // Serialize its children |
| 160 | + outJson = getChildAdditionalData((IJsonBackedObject)child, outJson); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + // If the object is a valid Graph object, add its additional data |
| 165 | + else if (fieldObject != null && fieldObject instanceof IJsonBackedObject) { |
| 166 | + IJsonBackedObject serializableChild = (IJsonBackedObject) fieldObject; |
| 167 | + AdditionalDataManager childAdditionalData = serializableChild.additionalDataManager(); |
| 168 | + |
| 169 | + addAdditionalDataToJson(childAdditionalData, outJson.get(field.getName()).getAsJsonObject()); |
| 170 | + |
| 171 | + // Serialize its children |
| 172 | + outJson = getChildAdditionalData(serializableChild, outJson); |
| 173 | + } |
| 174 | + } catch (IllegalArgumentException | IllegalAccessException e) { |
| 175 | + logger.logError("Unable to access child fields of " + serializableObject.getClass().getSimpleName(), e); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return outJson; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Add each non-transient additional data property to the given JSON node |
| 184 | + * @param additionalDataManager The additional data bag to iterate through |
| 185 | + * @param jsonNode The JSON node to add the additional data properties to |
| 186 | + */ |
| 187 | + private void addAdditionalDataToJson(AdditionalDataManager additionalDataManager, JsonObject jsonNode) { |
| 188 | + for (Map.Entry<String, JsonElement> entry : additionalDataManager.entrySet()) { |
| 189 | + if (!fieldIsOdataTransient(entry)) { |
| 190 | + jsonNode.add( |
| 191 | + entry.getKey(), |
| 192 | + entry.getValue() |
| 193 | + ); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
123 | 197 |
|
124 | 198 | private boolean fieldIsOdataTransient(Map.Entry<String, JsonElement> entry) { |
125 | | - return entry.getKey().startsWith("@"); |
| 199 | + return (entry.getKey().startsWith("@") && entry.getKey() != "@odata.type"); |
126 | 200 | } |
127 | 201 |
|
128 | 202 | /** |
|
0 commit comments