Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Remove reflection usage from KiotaSerialization #957

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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