Tuesday, 29 June 2021

Moshi: Customize serialization process using adapters

In this post, I am going to explain how to customize the serialization process using adapters.

 

Adapters provide greater flexibility in customization of serialization process.

 

Step 1: Define an adapter with @ToJson and @FromJson methods.

public class BoxAdapter {
    @ToJson
    int[] toJson(Box box) {
        return new int[] { box.getWidth(), box.getHeight() };
    }

    @FromJson
    Box fromJson(int[] dimensions) throws Exception {
        if (dimensions.length != 2) {
            throw new Exception("Expected 2 elements but was " + Arrays.toString(dimensions));
        }
        int width = dimensions[0];
        int height = dimensions[1];
        return new Box(width, height);
    }
}

 

Step 2: Get an instance of Moshi using the adapter defined in step 1.

Moshi moshi = new Moshi.Builder().add(new BoxAdapter()).build();

 

Step 3: Get JsonAdapter instance for Box class from the moshi object.

JsonAdapter<Box> boxJsonAdapter = moshi.adapter(Box.class);

Step 4: Serialize and deserialize the box data.

Box box = new Box(10, 15);
String json = boxJsonAdapter.toJson(box);
Box box1 = boxJsonAdapter.fromJson(json);

 

Find the below working application.

 

Box.java

package com.sample.app.model;

public class Box {
    private int width;
    private int height;

    public Box(int width, int height) {
        super();
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
    
    

}

 

BoxAdapter.java

package com.sample.app.adapter;

import java.util.Arrays;

import com.sample.app.model.Box;
import com.squareup.moshi.FromJson;
import com.squareup.moshi.ToJson;

public class BoxAdapter {
    @ToJson
    int[] toJson(Box box) {
        return new int[] { box.getWidth(), box.getHeight() };
    }

    @FromJson
    Box fromJson(int[] dimensions) throws Exception {
        if (dimensions.length != 2) {
            throw new Exception("Expected 2 elements but was " + Arrays.toString(dimensions));
        }
        int width = dimensions[0];
        int height = dimensions[1];
        return new Box(width, height);
    }
}

 

AdapterDemo.java

package com.sample.app;

import java.io.IOException;

import com.sample.app.adapter.BoxAdapter;
import com.sample.app.model.Box;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

public class AdapterDemo {

    public static void main(String args[]) throws IOException {
        Moshi moshi = new Moshi.Builder().add(new BoxAdapter()).build();

        JsonAdapter<Box> boxJsonAdapter = moshi.adapter(Box.class);

        Box box = new Box(10, 15);

        String json = boxJsonAdapter.toJson(box);
        System.out.println("serialized data : " + json);

        Box box1 = boxJsonAdapter.fromJson(json);

        System.out.println("\nBox details post deserialization");
        System.out.println("Width : " + box1.getWidth());
        System.out.println("Height : " + box1.getHeight());

    }

}

 

Output

json : [10,15]

Box details
Width : 10
Height : 15

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment