Package com.azure.core.util.serializer
Class JacksonAdapter
- java.lang.Object
-
- com.azure.core.util.serializer.JacksonAdapter
-
- All Implemented Interfaces:
SerializerAdapter
public class JacksonAdapter extends Object implements SerializerAdapter
Implementation ofSerializerAdapter
for Jackson.
-
-
Constructor Summary
Constructors Constructor Description JacksonAdapter()
Creates a new JacksonAdapter instance with default mapper settings.JacksonAdapter(BiConsumer<ObjectMapper,ObjectMapper> configureSerialization)
Creates a new JacksonAdapter instance with Azure Core mapper settings and applies additional configuration throughconfigureSerialization
callback.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static SerializerAdapter
createDefaultSerializerAdapter()
maintain singleton instance of the default serializer adapter.<T> T
deserialize(byte[] bytes, Type type, SerializerEncoding encoding)
Deserializes a byte array into an object.<T> T
deserialize(HttpHeaders headers, Type deserializedHeadersType)
Deserialize the provided headers returned from a REST API to an entity instance declared as the model to hold 'Matching' headers.<T> T
deserialize(InputStream inputStream, Type type, SerializerEncoding encoding)
Deserializes a stream into an object.<T> T
deserialize(String value, Type type, SerializerEncoding encoding)
Deserializes a string into an object.String
serialize(Object object, SerializerEncoding encoding)
Serializes an object into a string.void
serialize(Object object, SerializerEncoding encoding, OutputStream outputStream)
Serializes an object and writes its output into anOutputStream
.String
serializeList(List<?> list, CollectionFormat format)
Serializes a list into a string with the delimiter specified with the Swagger collection format joining each individual serialized items in the list.ObjectMapper
serializer()
Deprecated.deprecated to avoid directObjectMapper
usage in favor of using more resilient and debuggableJacksonAdapter
APIs.String
serializeRaw(Object object)
Serializes an object into a raw string, leading and trailing quotes will be trimmed.byte[]
serializeToBytes(Object object, SerializerEncoding encoding)
Serializes an object into a byte array.protected ObjectMapper
simpleMapper()
Deprecated.deprecated, useJacksonAdapter(BiConsumer<ObjectMapper, ObjectMapper>)
constructor to configure modules.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.azure.core.util.serializer.SerializerAdapter
serializeIterable
-
-
-
-
Constructor Detail
-
JacksonAdapter
public JacksonAdapter()
Creates a new JacksonAdapter instance with default mapper settings.
-
JacksonAdapter
public JacksonAdapter(BiConsumer<ObjectMapper,ObjectMapper> configureSerialization)
Creates a new JacksonAdapter instance with Azure Core mapper settings and applies additional configuration throughconfigureSerialization
callback.configureSerialization
callback provides outer and inner instances ofObjectMapper
. Both of them are pre-configured for Azure serialization needs, but only outer mapper capable of flattening and populating additionalProperties. Outer mapper is used byJacksonAdapter
for all serialization needs. Register modules on the outer instance to add custom (de)serializers similar tonew JacksonAdapter((outer, inner) -> outer.registerModule(new MyModule()))
Use inner mapper for chaining serialization logic in your (de)serializers.- Parameters:
configureSerialization
- Applies additional configuration to outer mapper using inner mapper for module chaining.
-
-
Method Detail
-
simpleMapper
@Deprecated protected ObjectMapper simpleMapper()
Deprecated.deprecated, useJacksonAdapter(BiConsumer<ObjectMapper, ObjectMapper>)
constructor to configure modules.Gets a static instance ofObjectMapper
that doesn't handle flattening.- Returns:
- an instance of
ObjectMapper
.
-
createDefaultSerializerAdapter
public static SerializerAdapter createDefaultSerializerAdapter()
maintain singleton instance of the default serializer adapter.- Returns:
- the default serializer
-
serializer
@Deprecated public ObjectMapper serializer()
Deprecated.deprecated to avoid directObjectMapper
usage in favor of using more resilient and debuggableJacksonAdapter
APIs.- Returns:
- the original serializer type.
-
serialize
public String serialize(Object object, SerializerEncoding encoding) throws IOException
Description copied from interface:SerializerAdapter
Serializes an object into a string.- Specified by:
serialize
in interfaceSerializerAdapter
- Parameters:
object
- The object to serialize.encoding
- The serialization encoding.- Returns:
- The object serialized as a string using the specified encoding. If the object is null null is returned.
- Throws:
IOException
- If an IO exception was thrown during serialization.
-
serializeToBytes
public byte[] serializeToBytes(Object object, SerializerEncoding encoding) throws IOException
Description copied from interface:SerializerAdapter
Serializes an object into a byte array.- Specified by:
serializeToBytes
in interfaceSerializerAdapter
- Parameters:
object
- The object to serialize.encoding
- The serialization encoding.- Returns:
- The object serialized as a byte array.
- Throws:
IOException
- If an IO exception was thrown during serialization.
-
serialize
public void serialize(Object object, SerializerEncoding encoding, OutputStream outputStream) throws IOException
Description copied from interface:SerializerAdapter
Serializes an object and writes its output into anOutputStream
.- Specified by:
serialize
in interfaceSerializerAdapter
- Parameters:
object
- The object to serialize.encoding
- The serialization encoding.outputStream
- TheOutputStream
where the serialized object will be written.- Throws:
IOException
- If an IO exception was thrown during serialization.
-
serializeRaw
public String serializeRaw(Object object)
Description copied from interface:SerializerAdapter
Serializes an object into a raw string, leading and trailing quotes will be trimmed.- Specified by:
serializeRaw
in interfaceSerializerAdapter
- Parameters:
object
- The object to serialize.- Returns:
- The object serialized as a string. If the object is null null is returned.
-
serializeList
public String serializeList(List<?> list, CollectionFormat format)
Description copied from interface:SerializerAdapter
Serializes a list into a string with the delimiter specified with the Swagger collection format joining each individual serialized items in the list.- Specified by:
serializeList
in interfaceSerializerAdapter
- Parameters:
list
- The list to serialize.format
- The collection joining format.- Returns:
- The list serialized as a joined string.
-
deserialize
public <T> T deserialize(String value, Type type, SerializerEncoding encoding) throws IOException
Description copied from interface:SerializerAdapter
Deserializes a string into an object.- Specified by:
deserialize
in interfaceSerializerAdapter
- Type Parameters:
T
- The type of the deserialized object.- Parameters:
value
- The string to deserialize.type
- The type of the deserialized object.encoding
- The deserialization encoding.- Returns:
- The string deserialized into an object.
- Throws:
IOException
- If an IO exception was thrown during deserialization.
-
deserialize
public <T> T deserialize(byte[] bytes, Type type, SerializerEncoding encoding) throws IOException
Description copied from interface:SerializerAdapter
Deserializes a byte array into an object.- Specified by:
deserialize
in interfaceSerializerAdapter
- Type Parameters:
T
- The type of the deserialized object.- Parameters:
bytes
- The byte array to deserialize.type
- The type of the deserialized object.encoding
- The deserialization encoding.- Returns:
- The string deserialized into an object.
- Throws:
IOException
- If an IO exception was thrown during serialization.
-
deserialize
public <T> T deserialize(InputStream inputStream, Type type, SerializerEncoding encoding) throws IOException
Description copied from interface:SerializerAdapter
Deserializes a stream into an object.- Specified by:
deserialize
in interfaceSerializerAdapter
- Type Parameters:
T
- The type of the deserialized object.- Parameters:
inputStream
- TheInputStream
to deserialize.type
- The type of the deserialized object.encoding
- The deserialization encoding.- Returns:
- The stream deserialized into an object.
- Throws:
IOException
- If an IO exception was thrown during serialization.
-
deserialize
public <T> T deserialize(HttpHeaders headers, Type deserializedHeadersType) throws IOException
Description copied from interface:SerializerAdapter
Deserialize the provided headers returned from a REST API to an entity instance declared as the model to hold 'Matching' headers.'Matching' headers are the REST API returned headers those with:
- header names same as name of a properties in the entity.
- header names start with value of
HeaderCollection
annotation applied to the properties in the entity.
ResponseBase
returned by java proxy method corresponding to the REST API. e.g.Mono<RestResponseBase<FooMetadataHeaders, Void>> getMetadata(args);
class FooMetadataHeaders { String name; {@literal @}HeaderCollection("header-collection-prefix-") Map<String,String> headerCollection; }
in the case of above example, this method produces an instance of FooMetadataHeaders from providedheaders
.- Specified by:
deserialize
in interfaceSerializerAdapter
- Type Parameters:
T
- the type of the deserialized object- Parameters:
headers
- the REST API returned headersdeserializedHeadersType
- the type to deserialize- Returns:
- instance of header entity type created based on provided
headers
, if header entity model does not not exists then return null - Throws:
IOException
- If an I/O error occurs
-
-