-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #767 from microsoft/feature/serialization-helpers
feature/serialization helpers
- Loading branch information
Showing
12 changed files
with
709 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
components/abstractions/src/main/java/com/microsoft/kiota/Compatibility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
.../abstractions/src/main/java/com/microsoft/kiota/serialization/KiotaJsonSerialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.