|
22 | 22 |
|
23 | 23 | package com.microsoft.graph.serializer; |
24 | 24 |
|
25 | | -import java.io.IOException; |
26 | | -import java.util.HashMap; |
27 | | -import java.util.Map; |
28 | | -import java.util.Objects; |
29 | | - |
30 | 25 | import com.google.common.base.CaseFormat; |
31 | 26 | import com.google.gson.Gson; |
| 27 | +import com.google.gson.JsonElement; |
32 | 28 | import com.google.gson.TypeAdapter; |
33 | 29 | import com.google.gson.TypeAdapterFactory; |
| 30 | +import com.google.gson.internal.Streams; |
34 | 31 | import com.google.gson.reflect.TypeToken; |
35 | 32 | import com.google.gson.stream.JsonReader; |
36 | 33 | import com.google.gson.stream.JsonToken; |
37 | 34 | import com.google.gson.stream.JsonWriter; |
| 35 | +import com.microsoft.graph.http.BaseCollectionPage; |
| 36 | +import com.microsoft.graph.http.BaseCollectionResponse; |
38 | 37 | import com.microsoft.graph.logger.ILogger; |
39 | 38 |
|
40 | | -import javax.annotation.Nullable; |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.Objects; |
41 | 43 | import javax.annotation.Nonnull; |
| 44 | +import javax.annotation.Nullable; |
42 | 45 |
|
43 | 46 | /** |
44 | 47 | * Handles serialization/deserialization for special types (especially of |
@@ -89,16 +92,70 @@ public FallbackTypeAdapterFactory(@Nonnull final ILogger logger) { |
89 | 92 | public <T> TypeAdapter<T> create(@Nonnull final Gson gson, @Nonnull final TypeToken<T> type) { |
90 | 93 | Objects.requireNonNull(type, "parameter type cannot be null"); |
91 | 94 | final Class<T> rawType = (Class<T>) type.getRawType(); |
| 95 | + |
92 | 96 | if (rawType.isEnum()) { |
93 | 97 | return new EnumTypeAdapter<T>(rawType, logger); |
94 | 98 | } else if (rawType == Void.class) { |
95 | 99 | return (TypeAdapter<T>) voidAdapter; |
| 100 | + } else if (IJsonBackedObject.class.isAssignableFrom(type.getRawType())) { |
| 101 | + |
| 102 | + // Avoid overriding custom IJsonBackedObject type adapters defined in GsonFactory |
| 103 | + if (BaseCollectionResponse.class.isAssignableFrom(rawType) || BaseCollectionPage.class.isAssignableFrom(rawType)) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + final TypeAdapter<?> delegatedAdapter = gson.getDelegateAdapter(this, type); |
| 108 | + return (TypeAdapter<T>) new ODataTypeParametrizedIJsonBackedObjectAdapter(gson, delegatedAdapter, type); |
96 | 109 | } |
97 | 110 | else { |
98 | 111 | return null; |
99 | 112 | } |
100 | 113 | } |
101 | 114 |
|
| 115 | + /** |
| 116 | + * This adapter is responsible for deserialization of IJsonBackedObjects where service |
| 117 | + * returns one of several derived types of a base object, which is defined using the |
| 118 | + * odata.type parameter. If odata.type parameter is not found, the Gson default |
| 119 | + * (delegated) type adapter is used. |
| 120 | + */ |
| 121 | + private class ODataTypeParametrizedIJsonBackedObjectAdapter extends TypeAdapter<IJsonBackedObject> { |
| 122 | + |
| 123 | + private final Gson gson; |
| 124 | + private final TypeAdapter<?> delegatedAdapter; |
| 125 | + private final TypeToken<?> type; |
| 126 | + |
| 127 | + public ODataTypeParametrizedIJsonBackedObjectAdapter(@Nonnull Gson gson, @Nonnull TypeAdapter<?> delegatedAdapter, @Nonnull final TypeToken<?> type) { |
| 128 | + super(); |
| 129 | + this.gson = gson; |
| 130 | + this.delegatedAdapter = delegatedAdapter; |
| 131 | + this.type = type; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void write(JsonWriter out, IJsonBackedObject value) |
| 136 | + throws IOException |
| 137 | + { |
| 138 | + ((TypeAdapter<IJsonBackedObject>)this.delegatedAdapter).write(out, value); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public IJsonBackedObject read(JsonReader in) { |
| 143 | + JsonElement jsonElement = Streams.parse(in); |
| 144 | + |
| 145 | + if (jsonElement.isJsonObject()) { |
| 146 | + final DerivedClassIdentifier derivedClassIdentifier = new DerivedClassIdentifier(logger); |
| 147 | + final Class<?> derivedClass = derivedClassIdentifier.getDerivedClass(jsonElement.getAsJsonObject(), type.getRawType()); |
| 148 | + |
| 149 | + if (derivedClass != null) { |
| 150 | + final TypeAdapter<?> subTypeAdapter = gson.getDelegateAdapter(FallbackTypeAdapterFactory.this, TypeToken.get(derivedClass)); |
| 151 | + return (IJsonBackedObject) subTypeAdapter.fromJsonTree(jsonElement); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return (IJsonBackedObject) delegatedAdapter.fromJsonTree(jsonElement); |
| 156 | + } |
| 157 | + } |
| 158 | + |
102 | 159 | private static final class EnumTypeAdapter<T> extends TypeAdapter<T> { |
103 | 160 |
|
104 | 161 | private final Map<String, T> enumValues; |
|
0 commit comments