Skip to content

Custom JSON serializer

Alan Zimmer edited this page Oct 21, 2020 · 2 revisions

Introduction

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.

Implementation Overview

Interfaces and Abstract Classes

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.

Target Version of Azure Core

A specific version of Azure Core will need to be targeted when implementing a custom HTTP client. 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.

Service Provider Interface (SPI)

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 Netty 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.

Example

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.9.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.

JsonIoJsonSerializer

JsonIoJsonSerializerProvider

com.azure.core.util.serializer.JsonSerializerProvider

jsonio.JsonIoJsonSerializerProvider
Clone this wiki locally