Gson: Serializing Inner Classes

Serializing inner classes

Gson can serialize inner classes and static nested classes. The detailed example below demonstrates the following things.

  1. Serializing class containing static nested class
  2. Serializing class containing non static nested class (Inner class)
  3. De-serializing json to a class containing static and non static inner class
  4. Serializing static nested class (without the enclosing type)
  5. Serializing non static nested class (without the enclosing type)
  6. De-serializing json to a static nested class (without the enclosing type)
  7. De-serializing json to a non static nested class (without the enclosing type)

The Example

package com.studytrails.json.gson;

import java.lang.reflect.Modifier;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.studytrails.json.gson.AlbumsWithInnerClass.Dataset;
import com.studytrails.json.gson.AlbumsWithInnerClass.Dataset2;

public class SerializeInnerClassExample4 {
    public static void main(String[] args) {
        // create an Albums class with a static nested class and a non static
        // nested class
        AlbumsWithInnerClass albums = new AlbumsWithInnerClass();
        albums.setName("SerializeInnerClass");
        // create a dataset. we need the enclosing type since this is a non
        // static nested class (inner class)
        Dataset dataset = albums.new Dataset();
        dataset.setAlbum_id("1");
        dataset.setAlbum_name("albums1");
        // assign the datasets to albums
        albums.setDatasetsInner(new Dataset[] { dataset });

        // static inner class can be created without the enclosing type
        Dataset2 dataset2 = new Dataset2();
        dataset2.setAlbum_id("2");
        dataset2.setAlbum_name("albums2");
        albums.setDatasetsStatic(new Dataset2[] { dataset2 });

        // create the GsonBuilder
        GsonBuilder builder = new GsonBuilder();
        // we ignore Private fields
        builder.excludeFieldsWithModifiers(Modifier.PRIVATE);
        Gson gson = builder.create();

        // serialize the albums object
        String json = gson.toJson(albums);
        System.out.println(json);
        // prints
        // {"name":"SerializeInnerClass","datasetsInner":[{"album_name":"SerializeInnerClass_albums1","album_id":"1"}],
        // "datasetsStatic":[{"album_name":"albums2","album_id":"2"}]}

        // We read the json string now and recreate the AlbumsWithInnerClass class
        Gson gson3 = new Gson();
        AlbumsWithInnerClass parsedAlbums = gson3.fromJson(json, AlbumsWithInnerClass.class);
        System.out.println(parsedAlbums.datasetsInner[0].album_name);
        // prints SerializeInnerClass_albums1
        System.out.println(parsedAlbums.datasetsStatic[0].album_name);
        // prints albums2

        // now lets try and serialize only the object of inner class
        Gson gson2 = new Gson();
        String json2 = gson2.toJson(dataset);
        System.out.println(json2);
        // prints {"album_name":"SerializeInnerClass_albums1","album_id":"1"}

        // serialize nested static class
        String json3 = gson2.toJson(dataset2);
        System.out.println(json3);
        // prints {"album_name":"albums2","album_id":"2"}

        // let us now create the inner class from the json string
        Gson gson4 = new Gson();
        Dataset parsedDataset = gson4.fromJson(json2, Dataset.class);
        System.out.println(parsedDataset.getClass());
        // prints class com.studytrails.json.gson.AlbumsWithInnerClass$Dataset
        System.out.println(parsedDataset.album_name);
        // prints SerializeInnerClass_albums1

        // create nested static class from the json string
        Dataset2 parsedStaticNestedClass = gson4.fromJson(json3, Dataset2.class);
        System.out.println(parsedStaticNestedClass.getClass());
        // prints class com.studytrails.json.gson.AlbumsWithInnerClass$Dataset2
        System.out.println(parsedStaticNestedClass.album_name);
        // prints albums2

    }
}

The AlbumsWithInnerClass

package com.studytrails.json.gson;

public class AlbumsWithInnerClass {
    public String name;
    private String year;
    public Dataset[] datasetsInner;
    public Dataset2[] datasetsStatic;

    public void setDatasetsInner(Dataset[] datasetsInner) {
        this.datasetsInner = datasetsInner;
    }

    public void setDatasetsStatic(Dataset2[] datasetsStatic) {
        this.datasetsStatic = datasetsStatic;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setYear(String year) {
        this.year = year;
    }

    class Dataset {
        public String album_name;
        public String album_id;

        public void setAlbum_id(String album_id) {
            this.album_id = album_id;
        }

        public void setAlbum_name(String album_name) {
            this.album_name = name + "_" + album_name;
        }
    }

    static class Dataset2 {
        public String album_name;
        public String album_id;

        public void setAlbum_id(String album_id) {
            this.album_id = album_id;
        }

        public void setAlbum_name(String album_name) {
            this.album_name = album_name;
        }
    }
}

(Note: This article’s original links is here )

 Gson: Serializing and deserializing Java Generic Classes Gson: Custom Type Adapter 

Comments