-
Notifications
You must be signed in to change notification settings - Fork 2k
Custom JSON serializer
Azure SDKs for Java offers the capability to plug-in your own custom JSON serializer to allow for handling of specialized scenarios or when you don't want to use Jackson or GSON. Providing a custom JsonSerializer
requires a few interfaces/classes to be implemented along with registering your implementation with Java's service provider interface.
The following interfaces and abstract classes will need to be implemented in your custom HTTP client.
- JsonSerializer handles serializing objects to JSON and deserializing JSON to objects
-
JsonSerializerProvider handles creating instances of the
JsonSerializer
agnostic to the underlying implementation.
A specific version of Azure Core will need to be targeted when implementing a custom JsonSerializer
. This version should be based on which version of Azure Core is being used by the other Azure SDK client libraries your application is depending on, for example if you have a dependency on Azure Storage Blobs that uses Azure Core 1.9.0 you'll want your custom HTTP client to use that as it's dependency.
Once the custom JSON serializer has been implemented it needs to be registered with Java's service provider interface. In the module that contains the custom HTTP client the resource folder will need the following.
src
|
+--main
|
+--java
+--resources
|
+--META-INF.services
|
+--com.azure.core.util.serializer.JsonSerializerProvider
The only contents of the com.azure.core.util.serializer.JsonSerializerProvider
file should be the fully qualified name of the class that implements JsonSerializerProvider
. For example, in the azure-core-serializer-json-jackson implementation that is offered the implementing class is com.azure.core.serializer.json.jackson.JacksonJsonSerializerProvider
, so the only value in this file is com.azure.core.serializer.json.jackson.JacksonJsonSerializerProvider
.
This is a high-level example of a custom JSON serializer implementation built on top of com.cedarsoftware.json-io and using Azure Core 1.18.0. This example hasn't be verified for correctness or scalability but serves as a general example of how to roll your own JSON serializer.
public class JsonIoJsonSerializer implements JsonSerializer {
private final ClientLogger logger = new ClientLogger(JsonIoJsonSerializer.class);
@Override
public <T> T deserialize(InputStream stream, TypeReference<T> typeReference) {
if (stream == null) {
return null;
}
return (T)JsonReader.jsonToJava(stream, null);
}
@Override
public <T> Mono<T> deserializeAsync(InputStream stream, TypeReference<T> typeReference) {
return Mono.fromCallable(() -> deserialize(stream, typeReference));
}
@Override
public void serialize(OutputStream stream, Object value) {
JsonWriter jw = new JsonWriter(stream);
jw.write(value);
jw.close();
}
@Override
public Mono<Void> serializeAsync(OutputStream stream, Object value) {
return Mono.fromRunnable(() -> serialize(stream, value));
}
}
public final class JsonIoJsonSerializerProvider implements JsonSerializerProvider {
@Override
public JsonSerializer createInstance() {
return new JsonIoJsonSerializer();
}
}
jsonio.JsonIoJsonSerializerProvider
- Frequently Asked Questions
- Azure Identity Examples
- Configuration
- Performance Tuning
- Android Support
- Unit Testing
- Test Proxy Migration
- Azure Json Migration
- New Checkstyle and Spotbugs pattern migration
- Protocol Methods
- TypeSpec-Java Quickstart
- Getting Started Guidance
- Adding a Module
- Building
- Writing Performance Tests
- Working with AutoRest
- Deprecation
- BOM guidelines
- Release process
- Access helpers