use of com.microsoft.graph.http.BaseCollectionResponse in project msgraph-sdk-java-core by microsoftgraph.
the class CollectionResponseDeserializer method deserialize.
/**
* Deserializes the JsonElement
*
* @param json the source CollectionResponse's Json
* @param typeOfT The type of the CollectionResponse to deserialize to
* @param logger the logger
* @param <T1> the entity type for the collection
* @throws JsonParseException the parse exception
* @return the deserialized CollectionResponse
*/
@SuppressWarnings("unchecked")
@Nullable
public static <T1> BaseCollectionResponse<T1> deserialize(@Nonnull final JsonElement json, @Nonnull final Type typeOfT, @Nonnull final ILogger logger) throws JsonParseException {
if (json == null || !json.isJsonObject() || !typeOfT.getClass().equals(Class.class)) {
return null;
}
serializer = new DefaultSerializer(logger);
final JsonObject jsonAsObject = json.getAsJsonObject();
final JsonArray sourceArray = jsonAsObject.get("value").getAsJsonArray();
final ArrayList<T1> list = new ArrayList<>(sourceArray.size());
/**
* eg: com.microsoft.graph.requests.AttachmentCollectionResponse
*/
/**
* eg: com.microsoft.graph.requests.DriveItemDeltaCollectionResponse
*/
/**
* eg: com.microsoft.graph.requests.DriveItemGetActivitiesByIntervalCollectionResponse
*/
final Class<?> responseClass = ((Class<?>) typeOfT);
/**
* eg: com.microsoft.graph.http.BaseCollectionResponse<com.microsoft.graph.models.DriveItem>
*/
final String genericResponseName = responseClass.getGenericSuperclass().toString();
final int indexOfGenericMarker = genericResponseName.indexOf('<');
/**
* eg: com.microsoft.graph.models.Attachment
*/
final String baseEntityClassCanonicalName = genericResponseName.substring(indexOfGenericMarker + 1, genericResponseName.length() - 1);
Class<?> baseEntityClass;
try {
baseEntityClass = Class.forName(baseEntityClassCanonicalName);
} catch (ClassNotFoundException ex) {
// it is possible we can't find the parent base class depending on the response class name, see examples
baseEntityClass = null;
logger.logDebug("could not find class" + baseEntityClassCanonicalName);
}
try {
for (JsonElement sourceElement : sourceArray) {
if (sourceElement.isJsonObject()) {
final JsonObject sourceObject = sourceElement.getAsJsonObject();
final T1 targetObject = (T1) serializer.deserializeObject(sourceObject, baseEntityClass);
((IJsonBackedObject) targetObject).setRawObject(serializer, sourceObject);
list.add(targetObject);
} else if (sourceElement.isJsonPrimitive()) {
final JsonPrimitive primitiveValue = sourceElement.getAsJsonPrimitive();
if (primitiveValue.isString())
list.add((T1) primitiveValue.getAsString());
else if (primitiveValue.isBoolean())
list.add((T1) Boolean.valueOf(primitiveValue.getAsBoolean()));
else if (primitiveValue.isNumber())
list.add((T1) Long.valueOf(primitiveValue.getAsLong()));
}
}
final BaseCollectionResponse<T1> response = (BaseCollectionResponse<T1>) responseClass.getConstructor().newInstance();
response.value = list;
final JsonElement potentialNextLink = jsonAsObject.get("@odata.nextLink");
if (potentialNextLink != null)
response.nextLink = potentialNextLink.getAsString();
response.setRawObject(serializer, jsonAsObject);
return response;
} catch (NoSuchMethodException | InstantiationException | InvocationTargetException ex) {
logger.logError("Could not instanciate type during deserialization", ex);
} catch (IllegalAccessException ex) {
logger.logError("Unable to set field value during deserialization", ex);
}
return null;
}
use of com.microsoft.graph.http.BaseCollectionResponse in project msgraph-sdk-java-core by microsoftgraph.
the class GsonFactory method getGsonInstance.
/**
* Creates an instance of GSON
*
* Serializing of null values can have side effects on the service behavior.
* Sending null values in a PATCH request might reset existing values on the service side.
* Sending null values in a POST request might prevent the service from assigning default values to the properties.
* It is not recommended to send null values to the service in general and this setting should only be used when serializing information for a local store.
*
* @param logger the logger
* @param serializeNulls the setting of whether or not to serialize the null values in the JSON object
* @return the new instance
*/
@Nonnull
public static Gson getGsonInstance(@Nonnull final ILogger logger, final boolean serializeNulls) {
Objects.requireNonNull(logger, "parameter logger cannot be null");
final JsonSerializer<OffsetDateTime> calendarJsonSerializer = (src, typeOfSrc, context) -> {
if (src == null) {
return null;
}
try {
return new JsonPrimitive(OffsetDateTimeSerializer.serialize(src));
} catch (final Exception e) {
logger.logError(PARSING_MESSAGE + src, e);
return null;
}
};
final JsonDeserializer<OffsetDateTime> calendarJsonDeserializer = (json, typeOfT, context) -> {
if (json == null) {
return null;
}
try {
return OffsetDateTimeSerializer.deserialize(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
};
final JsonSerializer<byte[]> byteArrayJsonSerializer = (src, typeOfSrc, context) -> {
if (src == null) {
return null;
}
try {
return new JsonPrimitive(ByteArraySerializer.serialize(src));
} catch (final Exception e) {
logger.logError(PARSING_MESSAGE + Arrays.toString(src), e);
return null;
}
};
final JsonDeserializer<byte[]> byteArrayJsonDeserializer = (json, typeOfT, context) -> {
if (json == null) {
return null;
}
try {
return ByteArraySerializer.deserialize(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
};
final JsonSerializer<DateOnly> dateJsonSerializer = (src, typeOfSrc, context) -> {
if (src == null) {
return null;
}
return new JsonPrimitive(src.toString());
};
final JsonDeserializer<DateOnly> dateJsonDeserializer = (json, typeOfT, context) -> {
if (json == null) {
return null;
}
try {
return DateOnly.parse(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
};
final EnumSetSerializer eSetSerializer = new EnumSetSerializer(logger);
final JsonSerializer<EnumSet<?>> enumSetJsonSerializer = (src, typeOfSrc, context) -> {
if (src == null || src.isEmpty()) {
return null;
}
return eSetSerializer.serialize(src);
};
final JsonDeserializer<EnumSet<?>> enumSetJsonDeserializer = (json, typeOfT, context) -> {
if (json == null) {
return null;
}
return eSetSerializer.deserialize(typeOfT, json.getAsString());
};
final JsonSerializer<Duration> durationJsonSerializer = (src, typeOfSrc, context) -> new JsonPrimitive(src.toString());
final JsonDeserializer<Duration> durationJsonDeserializer = (json, typeOfT, context) -> {
try {
return DatatypeFactory.newInstance().newDuration(json.getAsString());
} catch (Exception e) {
return null;
}
};
final JsonSerializer<BaseCollectionPage<?, ?>> collectionPageSerializer = (src, typeOfSrc, context) -> CollectionPageSerializer.serialize(src, logger);
final JsonDeserializer<BaseCollectionPage<?, ?>> collectionPageDeserializer = (json, typeOfT, context) -> CollectionPageSerializer.deserialize(json, typeOfT, logger);
final JsonDeserializer<BaseCollectionResponse<?>> collectionResponseDeserializer = (json, typeOfT, context) -> CollectionResponseDeserializer.deserialize(json, typeOfT, logger);
final JsonDeserializer<TimeOfDay> timeOfDayJsonDeserializer = (json, typeOfT, context) -> {
try {
return TimeOfDay.parse(json.getAsString());
} catch (Exception e) {
return null;
}
};
final JsonSerializer<TimeOfDay> timeOfDayJsonSerializer = (src, typeOfSrc, context) -> new JsonPrimitive(src.toString());
final JsonDeserializer<Boolean> booleanJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Boolean.class, logger);
final JsonDeserializer<String> stringJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, String.class, logger);
final JsonDeserializer<BigDecimal> bigDecimalJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, BigDecimal.class, logger);
final JsonDeserializer<Integer> integerJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Integer.class, logger);
final JsonDeserializer<Long> longJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Long.class, logger);
final JsonDeserializer<UUID> uuidJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, UUID.class, logger);
final JsonDeserializer<Float> floatJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Float.class, logger);
GsonBuilder builder = new GsonBuilder();
if (serializeNulls) {
builder.serializeNulls();
}
return builder.excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Boolean.class, booleanJsonDeserializer).registerTypeAdapter(String.class, stringJsonDeserializer).registerTypeAdapter(Float.class, floatJsonDeserializer).registerTypeAdapter(Integer.class, integerJsonDeserializer).registerTypeAdapter(BigDecimal.class, bigDecimalJsonDeserializer).registerTypeAdapter(UUID.class, uuidJsonDeserializer).registerTypeAdapter(Long.class, longJsonDeserializer).registerTypeAdapter(OffsetDateTime.class, calendarJsonSerializer).registerTypeAdapter(OffsetDateTime.class, calendarJsonDeserializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonSerializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonDeserializer).registerTypeAdapter(EnumSet.class, enumSetJsonSerializer).registerTypeAdapter(EnumSet.class, enumSetJsonDeserializer).registerTypeAdapter(Duration.class, durationJsonSerializer).registerTypeAdapter(Duration.class, durationJsonDeserializer).registerTypeHierarchyAdapter(BaseCollectionPage.class, collectionPageSerializer).registerTypeHierarchyAdapter(BaseCollectionPage.class, collectionPageDeserializer).registerTypeHierarchyAdapter(BaseCollectionResponse.class, collectionResponseDeserializer).registerTypeAdapter(TimeOfDay.class, timeOfDayJsonDeserializer).registerTypeAdapter(TimeOfDay.class, timeOfDayJsonSerializer).registerTypeAdapterFactory(new FallbackTypeAdapterFactory(logger)).create();
}
Aggregations