Skip to content

Commit

Permalink
Merge pull request #957 from andreaTP/rem-reflect-kiota-ser
Browse files Browse the repository at this point in the history
[chore] Remove reflection usage from KiotaSerialization
  • Loading branch information
baywet authored Jan 18, 2024
2 parents 73dbb8e + 87dda24 commit a28ee7e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 134 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

### Changed

- Removed methods using reflection from `KiotaSerialization`

## [0.12.1] - 2024-01-10

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,54 +113,4 @@ private KiotaJsonSerialization() {}
@Nonnull final InputStream stream, @Nonnull final ParsableFactory<T> parsableFactory) {
return KiotaSerialization.deserializeCollection(CONTENT_TYPE, stream, parsableFactory);
}

/**
* Deserializes the given stream to a model object
* @param <T> the type of the value to deserialize
* @param stream the stream to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
*/
@Nonnull public static <T extends Parsable> T deserialize(
@Nonnull final InputStream stream, @Nonnull final Class<T> typeClass) {
return KiotaSerialization.deserialize(CONTENT_TYPE, stream, typeClass);
}

/**
* Deserializes the given string to a model object
* @param <T> the type of the value to deserialize
* @param value the string to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull public static <T extends Parsable> T deserialize(
@Nonnull final String value, @Nonnull final Class<T> typeClass) throws IOException {
return KiotaSerialization.deserialize(CONTENT_TYPE, value, typeClass);
}

/**
* Deserializes the given stream to a collection of model objects
* @param <T> the type of the value to deserialize
* @param stream the stream to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
*/
@Nonnull public static <T extends Parsable> List<T> deserializeCollection(
@Nonnull final InputStream stream, @Nonnull final Class<T> typeClass) {
return KiotaSerialization.deserializeCollection(CONTENT_TYPE, stream, typeClass);
}

/**
* Deserializes the given string to a collection of model objects
* @param <T> the type of the value to deserialize
* @param value the string to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull public static <T extends Parsable> List<T> deserializeCollection(
@Nonnull final String value, @Nonnull final Class<T> typeClass) throws IOException {
return KiotaSerialization.deserializeCollection(CONTENT_TYPE, value, typeClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -183,86 +182,4 @@ private static InputStream getInputStreamFromString(@Nonnull final String value)
final ParseNode parseNode = getRootParseNode(contentType, stream, parsableFactory);
return parseNode.getCollectionOfObjectValues(parsableFactory);
}

/**
* Deserializes the given stream to a model object
* @param <T> the type of the value to deserialize
* @param contentType the content type to use for deserialization
* @param stream the stream to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
*/
@Nonnull public static <T extends Parsable> T deserialize(
@Nonnull final String contentType,
@Nonnull final InputStream stream,
@Nonnull final Class<T> typeClass) {
return deserialize(contentType, stream, getFactoryMethodFromType(typeClass));
}

/**
* Deserializes the given string to a model object
* @param <T> the type of the value to deserialize
* @param contentType the content type to use for deserialization
* @param value the string to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull public static <T extends Parsable> T deserialize(
@Nonnull final String contentType,
@Nonnull final String value,
@Nonnull final Class<T> typeClass)
throws IOException {
return deserialize(contentType, value, getFactoryMethodFromType(typeClass));
}

/**
* Deserializes the given stream to a collection of model objects
* @param <T> the type of the value to deserialize
* @param contentType the content type to use for deserialization
* @param stream the stream to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
*/
@Nonnull public static <T extends Parsable> List<T> deserializeCollection(
@Nonnull final String contentType,
@Nonnull final InputStream stream,
@Nonnull final Class<T> typeClass) {
return deserializeCollection(contentType, stream, getFactoryMethodFromType(typeClass));
}

/**
* Deserializes the given string to a collection of model objects
* @param <T> the type of the value to deserialize
* @param contentType the content type to use for deserialization
* @param value the string to deserialize
* @param typeClass the class of the model object
* @return the deserialized value
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull public static <T extends Parsable> List<T> deserializeCollection(
@Nonnull final String contentType,
@Nonnull final String value,
@Nonnull final Class<T> typeClass)
throws IOException {
return deserializeCollection(contentType, value, getFactoryMethodFromType(typeClass));
}

@SuppressWarnings("unchecked")
@Nonnull private static <T extends Parsable> ParsableFactory<T> getFactoryMethodFromType(
@Nonnull final Class<T> type) {
Objects.requireNonNull(type);
try {
final Method method = type.getMethod("createFromDiscriminatorValue", ParseNode.class);
return node -> {
try {
return (T) method.invoke(null, node);
} catch (final Exception e) {
throw new RuntimeException(e);
}
};
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ void deserializesObjectWithReflection() throws IOException {
_jsonContentType, mockParseNodeFactory);

final var result =
KiotaSerialization.deserialize(_jsonContentType, strValue, TestEntity.class);
KiotaSerialization.deserialize(
_jsonContentType, strValue, TestEntity::createFromDiscriminatorValue);
assertEquals("123", result.getId());
}

Expand Down

0 comments on commit a28ee7e

Please sign in to comment.