Skip to content

Commit

Permalink
Merge pull request #767 from microsoft/feature/serialization-helpers
Browse files Browse the repository at this point in the history
feature/serialization helpers
  • Loading branch information
baywet authored Nov 2, 2023
2 parents abd2209 + 0e760db commit 7490835
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [0.8.0] - 2023-10-31

### Added

- Added a default implementation of `BasicAccessAuthenticationProvider`
- Added helper methods to serialize kiota models. [microsoft/kiota#3406](https://github.com/microsoft/kiota/issues/3406)

## [0.7.8] - 2023-10-13

Expand Down
12 changes: 12 additions & 0 deletions components/abstractions/spotBugsExcludeFilter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
<Bug pattern="RV_EXCEPTION_NOT_THROWN"/>
<Class name="com.microsoft.kiota.authentication.ApiKeyAuthenticationProviderTest" />
</Match>
<Match>
<Bug pattern="RV_EXCEPTION_NOT_THROWN"/>
<Class name="com.microsoft.kiota.serialization.DeserializationHelpersTest" />
</Match>
<Match>
<Bug pattern="RV_EXCEPTION_NOT_THROWN"/>
<Class name="com.microsoft.kiota.serialization.SerializationHelpersTest" />
</Match>
<Match>
<Bug pattern="EI_EXPOSE_REP" />
<Class name="com.microsoft.kiota.serialization.mocks.TestEntity" />
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.microsoft.kiota;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import jakarta.annotation.Nonnull;
/**
* Compatibility methods for android
*/
public class Compatibility {
private Compatibility() {
}
/**
* INTERNAL METHOD, DO NOT USE DIRECTLY
* Reads all bytes from the given input stream
* @param inputStream the input stream to read from
* @return the bytes read from the stream
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull
public static byte[] readAllBytes(@Nonnull final InputStream inputStream) throws IOException {
// InputStream.readAllBytes() is only available to Android API level 33+
final int bufLen = 1024;
byte[] buf = new byte[bufLen];
int readLen;
try(final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
outputStream.write(buf, 0, readLen);
return outputStream.toByteArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.microsoft.kiota;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
Expand Down Expand Up @@ -131,7 +128,7 @@ public void serialize(@Nonnull final SerializationWriter writer) {
try (final InputStream partContent = partWriter.getSerializedContent()) {
if (partContent.markSupported())
partContent.reset();
writer.writeByteArrayValue("", readAllBytes(partContent));
writer.writeByteArrayValue("", Compatibility.readAllBytes(partContent));
}
}
} else if (objectValue instanceof String) {
Expand All @@ -140,7 +137,7 @@ public void serialize(@Nonnull final SerializationWriter writer) {
final InputStream inputStream = (InputStream)objectValue;
if (inputStream.markSupported())
inputStream.reset();
writer.writeByteArrayValue("", readAllBytes(inputStream));
writer.writeByteArrayValue("", Compatibility.readAllBytes(inputStream));
} else if (objectValue instanceof byte[]) {
writer.writeByteArrayValue("", (byte[])objectValue);
} else {
Expand All @@ -153,16 +150,5 @@ public void serialize(@Nonnull final SerializationWriter writer) {
writer.writeStringValue("", "");
writer.writeStringValue("", "--" + boundary + "--");
}
@Nonnull
private byte[] readAllBytes(@Nonnull final InputStream inputStream) throws IOException {
// InputStream.readAllBytes() is only available to Android API level 33+
final int bufLen = 1024;
byte[] buf = new byte[bufLen];
int readLen;
try(final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
outputStream.write(buf, 0, readLen);
return outputStream.toByteArray();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.microsoft.kiota.serialization;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import jakarta.annotation.Nonnull;

/**
* Helper methods for JSON serialization of kiota models
*/
public class KiotaJsonSerialization {
private static final String CONTENT_TYPE = "application/json";
private KiotaJsonSerialization() {}
/**
* Serializes the given value to a stream
* @param <T> the type of the value to serialize
* @param value the value to serialize
* @return the serialized value as a stream
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull
public static <T extends Parsable> InputStream serializeAsStream(@Nonnull final T value) throws IOException {
return KiotaSerialization.serializeAsStream(CONTENT_TYPE, value);
}
/**
* Serializes the given value to a string
* @param <T> the type of the value to serialize
* @param value the value to serialize
* @return the serialized value as a string
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull
public static <T extends Parsable> String serializeAsString(@Nonnull final T value) throws IOException {
return KiotaSerialization.serializeAsString(CONTENT_TYPE, value);
}
/**
* Serializes the given value to a stream
* @param <T> the type of the value to serialize
* @param values the values to serialize
* @return the serialized value as a stream
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull
public static <T extends Parsable> InputStream serializeAsStream(@Nonnull final Iterable<T> values) throws IOException {
return KiotaSerialization.serializeAsStream(CONTENT_TYPE, values);
}
/**
* Serializes the given value to a string
* @param <T> the type of the value to serialize
* @param values the values to serialize
* @return the serialized value as a string
* @throws IOException when the stream cannot be closed or read.
*/
@Nonnull
public static <T extends Parsable> String serializeAsString(@Nonnull final Iterable<T> values) throws IOException {
return KiotaSerialization.serializeAsString(CONTENT_TYPE, values);
}
/**
* Deserializes the given stream to a model object
* @param <T> the type of the value to deserialize
* @param stream the stream to deserialize
* @param parsableFactory the factory to use for creating the model object
* @return the deserialized value
*/
@Nonnull
public static <T extends Parsable> T deserialize(@Nonnull final InputStream stream, @Nonnull final ParsableFactory<T> parsableFactory) {
return KiotaSerialization.deserialize(CONTENT_TYPE, stream, parsableFactory);
}
/**
* Deserializes the given string to a model object
* @param <T> the type of the value to deserialize
* @param value the string to deserialize
* @param parsableFactory the factory to use for creating 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 ParsableFactory<T> parsableFactory) throws IOException {
return KiotaSerialization.deserialize(CONTENT_TYPE, value, parsableFactory);
}
/**
* 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 parsableFactory the factory to use for creating 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 ParsableFactory<T> parsableFactory) throws IOException {
return KiotaSerialization.deserializeCollection(CONTENT_TYPE, value, parsableFactory);
}
/**
* 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 parsableFactory the factory to use for creating the model object
* @return the deserialized value
*/
@Nonnull
public static <T extends Parsable> List<T> deserializeCollection(@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);
}
}
Loading

0 comments on commit 7490835

Please sign in to comment.