public final class BinaryData extends Object
BinaryData can be created from an InputStream, a Flux of ByteBuffer, a String, an Object, or a byte array.
Immutable data
BinaryData copies data on construction making it immutable. Various APIs are provided to get data out of
BinaryData, they all start with the 'to' prefix, for example BinaryData.toBytes().
Create an instance from a byte array
final byte[] data = "Some Data".getBytes(StandardCharsets.UTF_8); BinaryData binaryData = BinaryData.fromBytes(data); System.out.println(new String(binaryData.toBytes(), StandardCharsets.UTF_8));
Create an instance from a String
final String data = "Some Data"; // Following will use default character set as StandardCharsets.UTF_8 BinaryData binaryData = BinaryData.fromString(data); System.out.println(binaryData.toString());
Create an instance from an InputStream
final ByteArrayInputStream inputStream = new ByteArrayInputStream("Some Data".getBytes(StandardCharsets.UTF_8));
BinaryData binaryData = BinaryData.fromStream(inputStream);
System.out.println(binaryData.toString());
Create an instance from an Object
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
BinaryData binaryData = BinaryData.fromObject(data);
System.out.println(binaryData.toString());
ObjectSerializer,
JsonSerializer,
More about serialization| Modifier and Type | Method and Description |
|---|---|
static BinaryData |
fromBytes(byte[] data)
Creates an instance of
BinaryData from the given byte array. |
static Mono<BinaryData> |
fromFlux(Flux<ByteBuffer> data)
|
static BinaryData |
fromObject(Object data)
|
static BinaryData |
fromObject(Object data,
ObjectSerializer serializer)
|
static Mono<BinaryData> |
fromObjectAsync(Object data)
|
static Mono<BinaryData> |
fromObjectAsync(Object data,
ObjectSerializer serializer)
|
static BinaryData |
fromStream(InputStream inputStream)
Creates an instance of
BinaryData from the given InputStream. |
static Mono<BinaryData> |
fromStreamAsync(InputStream inputStream)
Creates an instance of
BinaryData from the given InputStream. |
static BinaryData |
fromString(String data)
Creates an instance of
BinaryData from the given String. |
ByteBuffer |
toByteBuffer()
Returns a read-only
ByteBuffer representation of this BinaryData. |
byte[] |
toBytes()
Returns a byte array representation of this
BinaryData. |
<T> T |
toObject(Class<T> clazz)
Returns an
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer. |
<T> T |
toObject(Class<T> clazz,
ObjectSerializer serializer)
Returns an
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer. |
<T> T |
toObject(TypeReference<T> typeReference)
Returns an
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer. |
<T> T |
toObject(TypeReference<T> typeReference,
ObjectSerializer serializer)
Returns an
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer. |
<T> Mono<T> |
toObjectAsync(Class<T> clazz)
Returns an
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer. |
<T> Mono<T> |
toObjectAsync(Class<T> clazz,
ObjectSerializer serializer)
Returns an
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer. |
<T> Mono<T> |
toObjectAsync(TypeReference<T> typeReference)
Returns an
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer. |
<T> Mono<T> |
toObjectAsync(TypeReference<T> typeReference,
ObjectSerializer serializer)
Returns an
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer. |
InputStream |
toStream()
Returns an
InputStream representation of this BinaryData. |
String |
toString()
Returns a
String representation of this BinaryData by converting its data using the UTF-8
character set. |
public static BinaryData fromStream(InputStream inputStream)
BinaryData from the given InputStream.
If inputStream is null or empty an empty BinaryData is returned.
NOTE: The InputStream is not closed by this function.
Create an instance from an InputStream
final ByteArrayInputStream inputStream = new ByteArrayInputStream("Some Data".getBytes(StandardCharsets.UTF_8));
BinaryData binaryData = BinaryData.fromStream(inputStream);
System.out.println(binaryData.toString());
inputStream - The InputStream that BinaryData will represent.BinaryData representing the InputStream.UncheckedIOException - If any error happens while reading the InputStream.public static Mono<BinaryData> fromStreamAsync(InputStream inputStream)
BinaryData from the given InputStream.
If inputStream is null or empty an empty BinaryData is returned.
NOTE: The InputStream is not closed by this function.
Create an instance from an InputStream
final ByteArrayInputStream inputStream = new ByteArrayInputStream("Some Data".getBytes(StandardCharsets.UTF_8));
Mono<BinaryData> binaryDataMono = BinaryData.fromStreamAsync(inputStream);
Disposable subscriber = binaryDataMono
.map(binaryData -> {
System.out.println(binaryData.toString());
return true;
})
.subscribe();
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
inputStream - The InputStream that BinaryData will represent.Mono of BinaryData representing the InputStream.UncheckedIOException - If any error happens while reading the InputStream.public static Mono<BinaryData> fromFlux(Flux<ByteBuffer> data)
BinaryData from the given Flux of ByteBuffer.
If the data is null an empty BinaryData will be returned.
Note: This will collect all bytes from the ByteBuffers resulting in hasRemaining to return false.
Create an instance from a Flux of ByteBuffer
final byte[] data = "Some Data".getBytes(StandardCharsets.UTF_8);
final Flux<ByteBuffer> dataFlux = Flux.just(ByteBuffer.wrap(data));
Mono<BinaryData> binaryDataMono = BinaryData.fromFlux(dataFlux);
Disposable subscriber = binaryDataMono
.map(binaryData -> {
System.out.println(binaryData.toString());
return true;
})
.subscribe();
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
data - The Flux of ByteBuffer that BinaryData will represent.Mono of BinaryData representing the Flux of ByteBuffer.public static BinaryData fromString(String data)
BinaryData from the given String.
The String is converted into bytes using String.getBytes(Charset) passing StandardCharsets.UTF_8.
If the data is null or a zero length string an empty BinaryData will be returned.
Create an instance from a String
final String data = "Some Data"; // Following will use default character set as StandardCharsets.UTF_8 BinaryData binaryData = BinaryData.fromString(data); System.out.println(binaryData.toString());
data - The String that BinaryData will represent.BinaryData representing the String.public static BinaryData fromBytes(byte[] data)
BinaryData from the given byte array.
If the byte array is null or zero length an empty BinaryData will be returned.
Create an instance from a byte array
final byte[] data = "Some Data".getBytes(StandardCharsets.UTF_8); BinaryData binaryData = BinaryData.fromBytes(data); System.out.println(new String(binaryData.toBytes(), StandardCharsets.UTF_8));
data - The byte array that BinaryData will represent.BinaryData representing the byte array.public static BinaryData fromObject(Object data)
BinaryData by serializing the Object using the default JsonSerializer.
If data is null an empty BinaryData will be returned.
Note: This method first looks for a JsonSerializerProvider implementation on the classpath. If no
implementation is found, a default Jackson-based implementation will be used to serialize the object.
Creating an instance from an Object
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
BinaryData binaryData = BinaryData.fromObject(data);
System.out.println(binaryData.toString());
data - The object that will be JSON serialized that BinaryData will represent.BinaryData representing the JSON serialized object.JsonSerializerpublic static Mono<BinaryData> fromObjectAsync(Object data)
BinaryData by serializing the Object using the default JsonSerializer.
If data is null an empty BinaryData will be returned.
Note: This method first looks for a JsonSerializerProvider implementation on the classpath. If no
implementation is found, a default Jackson-based implementation will be used to serialize the object.
Creating an instance from an Object
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
Disposable subscriber = BinaryData.fromObjectAsync(data)
.subscribe(binaryData -> System.out.println(binaryData.toString()));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
data - The object that will be JSON serialized that BinaryData will represent.Mono of BinaryData representing the JSON serialized object.JsonSerializerpublic static BinaryData fromObject(Object data, ObjectSerializer serializer)
BinaryData by serializing the Object using the passed ObjectSerializer.
If data is null an empty BinaryData will be returned.
The passed ObjectSerializer can either be one of the implementations offered by the Azure SDKs or your
own implementation.
Azure SDK implementations
Create an instance from an Object
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
BinaryData binaryData = BinaryData.fromObject(data, serializer);
System.out.println(binaryData.toString());
data - The object that will be serialized that BinaryData will represent.serializer - The ObjectSerializer used to serialize object.BinaryData representing the serialized object.NullPointerException - If serializer is null and data is not null.ObjectSerializer,
JsonSerializer,
More about serializationpublic static Mono<BinaryData> fromObjectAsync(Object data, ObjectSerializer serializer)
BinaryData by serializing the Object using the passed ObjectSerializer.
If data is null an empty BinaryData will be returned.
The passed ObjectSerializer can either be one of the implementations offered by the Azure SDKs or your
own implementation.
Azure SDK implementations
Create an instance from an Object
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
Disposable subscriber = BinaryData.fromObjectAsync(data, serializer)
.subscribe(binaryData -> System.out.println(binaryData.toString()));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
data - The object that will be serialized that BinaryData will represent.serializer - The ObjectSerializer used to serialize object.Mono of BinaryData representing the serialized object.NullPointerException - If serializer is null and data is not null.ObjectSerializer,
JsonSerializer,
More about serializationpublic byte[] toBytes()
BinaryData.BinaryData.public String toString()
String representation of this BinaryData by converting its data using the UTF-8
character set.toString in class ObjectString representing this BinaryData.public <T> T toObject(Class<T> clazz)
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer.
The type, represented by Class, should be a non-generic class, for generic classes use BinaryData.toObject(TypeReference).
Note: This method first looks for a JsonSerializerProvider implementation on the classpath. If no
implementation is found, a default Jackson-based implementation will be used to deserialize the object.
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Ensure your classpath have the Serializer to serialize the object which implement implement
// com.azure.core.util.serializer.JsonSerializer interface.
// Or use Azure provided libraries for this.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
BinaryData binaryData = BinaryData.fromObject(data);
Person person = binaryData.toObject(Person.class);
System.out.println(person.getName());
T - Type of the deserialized Object.clazz - The Class representing the Object's type.Object representing the JSON deserialized BinaryData.NullPointerException - If clazz is null.JsonSerializerpublic <T> T toObject(TypeReference<T> typeReference)
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer.
The type, represented by TypeReference, can either be a generic or non-generic type. If the type is
generic create a sub-type of TypeReference, if the type is non-generic use TypeReference.createInstance(Class).
Note: This method first looks for a JsonSerializerProvider implementation on the classpath. If no
implementation is found, a default Jackson-based implementation will be used to deserialize the object.
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Ensure your classpath have the Serializer to serialize the object which implement implement
// com.azure.core.util.serializer.JsonSerializer interface.
// Or use Azure provided libraries for this.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
BinaryData binaryData = BinaryData.fromObject(data);
Person person = binaryData.toObject(TypeReference.createInstance(Person.class));
System.out.println(person.getName());
Get a generic Object from the BinaryData
final Person person1 = new Person().setName("John");
final Person person2 = new Person().setName("Jack");
List<Person> personList = new ArrayList<>();
personList.add(person1);
personList.add(person2);
// Ensure your classpath have the Serializer to serialize the object which implement implement
// com.azure.core.util.serializer.JsonSerializer interface.
// Or use Azure provided libraries for this.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
BinaryData binaryData = BinaryData.fromObject(personList);
List<Person> persons = binaryData.toObject(new TypeReference<List<Person>>() { });
persons.forEach(person -> System.out.println(person.getName()));
T - Type of the deserialized Object.typeReference - The TypeReference representing the Object's type.Object representing the JSON deserialized BinaryData.NullPointerException - If typeReference is null.JsonSerializerpublic <T> T toObject(Class<T> clazz, ObjectSerializer serializer)
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer.
The type, represented by Class, should be a non-generic class, for generic classes use BinaryData.toObject(TypeReference, ObjectSerializer).
The passed ObjectSerializer can either be one of the implementations offered by the Azure SDKs or your
own implementation.
Azure SDK implementations
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
BinaryData binaryData = BinaryData.fromObject(data, serializer);
Person person = binaryData.toObject(Person.class, serializer);
System.out.println("Name : " + person.getName());
T - Type of the deserialized Object.clazz - The Class representing the Object's type.serializer - The ObjectSerializer used to deserialize object.Object representing the deserialized BinaryData.NullPointerException - If clazz or serializer is null.ObjectSerializer,
JsonSerializer,
More about serializationpublic <T> T toObject(TypeReference<T> typeReference, ObjectSerializer serializer)
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer.
The type, represented by TypeReference, can either be a generic or non-generic type. If the type is
generic create a sub-type of TypeReference, if the type is non-generic use TypeReference.createInstance(Class).
The passed ObjectSerializer can either be one of the implementations offered by the Azure SDKs or your
own implementation.
Azure SDK implementations
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
BinaryData binaryData = BinaryData.fromObject(data, serializer);
Person person = binaryData.toObject(TypeReference.createInstance(Person.class), serializer);
System.out.println("Name : " + person.getName());
Get a generic Object from the BinaryData
final Person person1 = new Person().setName("John");
final Person person2 = new Person().setName("Jack");
List<Person> personList = new ArrayList<>();
personList.add(person1);
personList.add(person2);
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
BinaryData binaryData = BinaryData.fromObject(personList, serializer);
// Retains the type of the list when deserializing
List<Person> persons = binaryData.toObject(new TypeReference<List<Person>>() { }, serializer);
persons.forEach(person -> System.out.println("Name : " + person.getName()));
T - Type of the deserialized Object.typeReference - The TypeReference representing the Object's type.serializer - The ObjectSerializer used to deserialize object.Object representing the deserialized BinaryData.NullPointerException - If typeReference or serializer is null.ObjectSerializer,
JsonSerializer,
More about serializationpublic <T> Mono<T> toObjectAsync(Class<T> clazz)
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer.
The type, represented by Class, should be a non-generic class, for generic classes use BinaryData.toObject(TypeReference).
Note: This method first looks for a JsonSerializerProvider implementation on the classpath. If no
implementation is found, a default Jackson-based implementation will be used to deserialize the object.
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Ensure your classpath have the Serializer to serialize the object which implement implement
// com.azure.core.util.serializer.JsonSerializer interface.
// Or use Azure provided libraries for this.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
BinaryData binaryData = BinaryData.fromObject(data);
Disposable subscriber = binaryData.toObjectAsync(Person.class)
.subscribe(person -> System.out.println(person.getName()));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
T - Type of the deserialized Object.clazz - The Class representing the Object's type.Mono of Object representing the JSON deserialized BinaryData.NullPointerException - If clazz is null.JsonSerializerpublic <T> Mono<T> toObjectAsync(TypeReference<T> typeReference)
Object representation of this BinaryData by deserializing its data using the default
JsonSerializer.
The type, represented by TypeReference, can either be a generic or non-generic type. If the type is
generic create a sub-type of TypeReference, if the type is non-generic use TypeReference.createInstance(Class).
Note: This method first looks for a JsonSerializerProvider implementation on the classpath. If no
implementation is found, a default Jackson-based implementation will be used to deserialize the object.
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Ensure your classpath have the Serializer to serialize the object which implement implement
// com.azure.core.util.serializer.JsonSerializer interface.
// Or use Azure provided libraries for this.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
BinaryData binaryData = BinaryData.fromObject(data);
Disposable subscriber = binaryData.toObjectAsync(TypeReference.createInstance(Person.class))
.subscribe(person -> System.out.println(person.getName()));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
Get a generic Object from the BinaryData
final Person person1 = new Person().setName("John");
final Person person2 = new Person().setName("Jack");
List<Person> personList = new ArrayList<>();
personList.add(person1);
personList.add(person2);
BinaryData binaryData = BinaryData.fromObject(personList);
Disposable subscriber = binaryData.toObjectAsync(new TypeReference<List<Person>>() { })
.subscribe(persons -> persons.forEach(person -> System.out.println(person.getName())));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
T - Type of the deserialized Object.typeReference - The TypeReference representing the Object's type.Mono of Object representing the JSON deserialized BinaryData.NullPointerException - If typeReference is null.JsonSerializerpublic <T> Mono<T> toObjectAsync(Class<T> clazz, ObjectSerializer serializer)
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer.
The type, represented by Class, should be a non-generic class, for generic classes use BinaryData.toObject(TypeReference, ObjectSerializer).
The passed ObjectSerializer can either be one of the implementations offered by the Azure SDKs or your
own implementation.
Azure SDK implementations
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
BinaryData binaryData = BinaryData.fromObject(data, serializer);
Disposable subscriber = binaryData.toObjectAsync(Person.class, serializer)
.subscribe(person -> System.out.println(person.getName()));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
T - Type of the deserialized Object.clazz - The Class representing the Object's type.serializer - The ObjectSerializer used to deserialize object.Mono of Object representing the deserialized BinaryData.NullPointerException - If clazz or serializer is null.ObjectSerializer,
JsonSerializer,
More about serializationpublic <T> Mono<T> toObjectAsync(TypeReference<T> typeReference, ObjectSerializer serializer)
Object representation of this BinaryData by deserializing its data using the passed
ObjectSerializer.
The type, represented by TypeReference, can either be a generic or non-generic type. If the type is
generic create a sub-type of TypeReference, if the type is non-generic use TypeReference.createInstance(Class).
The passed ObjectSerializer can either be one of the implementations offered by the Azure SDKs or your
own implementation.
Azure SDK implementations
Get a non-generic Object from the BinaryData
class Person {
{@literal @}JsonProperty
private String name;
{@literal @}JsonSetter
public Person setName(String name) {
this.name = name;
return this;
}
{@literal @}JsonGetter
public String getName() {
return name;
}
}
final Person data = new Person().setName("John");
// Provide your custom serializer or use Azure provided serializers.
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-jackson or
// https://mvnrepository.com/artifact/com.azure/azure-core-serializer-json-gson
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
BinaryData binaryData = BinaryData.fromObject(data, serializer);
Disposable subscriber = binaryData
.toObjectAsync(TypeReference.createInstance(Person.class), serializer)
.subscribe(person -> System.out.println(person.getName()));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
Get a generic Object from the BinaryData
final Person person1 = new Person().setName("John");
final Person person2 = new Person().setName("Jack");
List<Person> personList = new ArrayList<>();
personList.add(person1);
personList.add(person2);
final ObjectSerializer serializer =
new MyJsonSerializer(); // Replace this with your Serializer
BinaryData binaryData = BinaryData.fromObject(personList, serializer);
Disposable subscriber = binaryData
.toObjectAsync(new TypeReference<List<Person>>() { }, serializer) // retains the generic type information
.subscribe(persons -> persons.forEach(person -> System.out.println(person.getName())));
// So that your program wait for above subscribe to complete.
TimeUnit.SECONDS.sleep(5);
subscriber.dispose();
T - Type of the deserialized Object.typeReference - The TypeReference representing the Object's type.serializer - The ObjectSerializer used to deserialize object.Mono of Object representing the deserialized BinaryData.NullPointerException - If typeReference or serializer is null.ObjectSerializer,
JsonSerializer,
More about serializationpublic InputStream toStream()
InputStream representation of this BinaryData.
Get an InputStream from the BinaryData
final byte[] data = "Some Data".getBytes(StandardCharsets.UTF_8); BinaryData binaryData = BinaryData.fromStream(new ByteArrayInputStream(data)); final byte[] bytes = new byte[data.length]; binaryData.toStream().read(bytes, 0, data.length); System.out.println(new String(bytes));
InputStream representing the BinaryData.public ByteBuffer toByteBuffer()
ByteBuffer representation of this BinaryData.
Attempting to mutate the returned ByteBuffer will throw a ReadOnlyBufferException.
Get a read-only ByteBuffer from the BinaryData
final byte[] data = "Some Data".getBytes(StandardCharsets.UTF_8); BinaryData binaryData = BinaryData.fromBytes(data); final byte[] bytes = new byte[data.length]; binaryData.toByteBuffer().get(bytes, 0, data.length); System.out.println(new String(bytes));
ByteBuffer representing the BinaryData.Copyright © 2021 Microsoft Corporation. All rights reserved.