|
| 1 | +package com.microsoft.graph.serializer; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.TypeAdapter; |
| 6 | +import com.google.gson.internal.Streams; |
| 7 | +import com.google.gson.reflect.TypeToken; |
| 8 | +import com.google.gson.stream.JsonReader; |
| 9 | +import com.google.gson.stream.JsonWriter; |
| 10 | +import com.microsoft.graph.logger.ILogger; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.Objects; |
| 14 | +import javax.annotation.Nonnull; |
| 15 | + |
| 16 | +/** |
| 17 | + * This adapter is responsible for deserialization of IJsonBackedObjects where service |
| 18 | + * returns one of several derived types of a base object, which is defined using the |
| 19 | + * odata.type parameter. If odata.type parameter is not found, the Gson default |
| 20 | + * (delegated) type adapter is used. |
| 21 | + */ |
| 22 | +class ODataTypeParametrizedIJsonBackedTypedAdapter extends TypeAdapter<IJsonBackedObject> { |
| 23 | + |
| 24 | + private final FallbackTypeAdapterFactory fallbackTypeAdapterFactory; |
| 25 | + private final Gson gson; |
| 26 | + private final TypeAdapter<IJsonBackedObject> delegatedAdapter; |
| 27 | + private final TypeToken<IJsonBackedObject> type; |
| 28 | + private final DerivedClassIdentifier derivedClassIdentifier; |
| 29 | + |
| 30 | + public ODataTypeParametrizedIJsonBackedTypedAdapter(FallbackTypeAdapterFactory fallbackTypeAdapterFactory, @Nonnull Gson gson, |
| 31 | + @Nonnull TypeAdapter<IJsonBackedObject> delegatedAdapter, @Nonnull final TypeToken<IJsonBackedObject> type, @Nonnull final ILogger logger) |
| 32 | + { |
| 33 | + super(); |
| 34 | + this.fallbackTypeAdapterFactory = fallbackTypeAdapterFactory; |
| 35 | + this.gson = Objects.requireNonNull(gson, "parameter gson cannot be null"); |
| 36 | + this.delegatedAdapter = Objects.requireNonNull(delegatedAdapter, "object delegated adapted cannot be null"); |
| 37 | + this.type = Objects.requireNonNull(type, "object type cannot be null"); |
| 38 | + this.derivedClassIdentifier = new DerivedClassIdentifier(logger); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void write(JsonWriter out, IJsonBackedObject value) |
| 43 | + throws IOException |
| 44 | + { |
| 45 | + this.delegatedAdapter.write(out, value); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public IJsonBackedObject read(JsonReader in) { |
| 50 | + JsonElement jsonElement = Streams.parse(in); |
| 51 | + |
| 52 | + if (jsonElement.isJsonObject()) { |
| 53 | + final Class<?> derivedClass = derivedClassIdentifier.identify(jsonElement.getAsJsonObject(), type.getRawType()); |
| 54 | + |
| 55 | + if (derivedClass != null) { |
| 56 | + final TypeAdapter<?> subTypeAdapter = gson.getDelegateAdapter(fallbackTypeAdapterFactory, TypeToken.get(derivedClass)); |
| 57 | + return (IJsonBackedObject) subTypeAdapter.fromJsonTree(jsonElement); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return delegatedAdapter.fromJsonTree(jsonElement); |
| 62 | + } |
| 63 | +} |
0 commit comments