A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/Azure/azure-sdk-for-java/wiki/Custom-JSON-serializer below:

Custom JSON serializer · Azure/azure-sdk-for-java Wiki · GitHub

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.

Interfaces and Abstract Classes

The following interfaces and abstract classes will need to be implemented in your custom HTTP client.

Target Version of Azure Core

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.

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 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));
    }
}
JsonIoJsonSerializerProvider
public final class JsonIoJsonSerializerProvider implements JsonSerializerProvider {
    @Override
    public JsonSerializer createInstance() {
        return new JsonIoJsonSerializer();
    }
}
com.azure.core.util.serializer.JsonSerializerProvider
jsonio.JsonIoJsonSerializerProvider

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4