How to serialize and deserialize arrays using GSON?

Using GSON to serialize and deserialize arrays is simple and easy!

Let’s get started!

If you want to learn how to get started with GSON, please check this blog out.

To use GSON in your java project,you must add below maven dependency!

1
2
3
4
5
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.8.5</version>
</dependency>

Integer Arrays

We can serialize and deserialize integer arrays using GSON library!

First we need to create GSON object as shown below

1
Gson gson = new GsonBuilder().setPrettyPrinting().create();

Below code snippet will show to serialize and deserialize integer arrays.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.harishgowda.demo;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Arrays;

public class GsonArrayDemo {

    public static void main(String[] args) {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        int[] nums = {10,20,30};

        System.out.println("Integer array to json: "+gson.toJson(nums));

        String intData= "[100,200,300]";

        int[] receivedNums= gson.fromJson(intData,int[].class);

        System.out.println("Json to Integer Array: " +Arrays.toString(receivedNums));

    }
}

Output:

1
2
3
4
5
6
Integer array to json: [
  10,
  20,
  30
]
Json to Integer Array: [100, 200, 300]

String Arrays

Similarly we can also serialize and deserialize string arrays using GSON library!

Below code snippet will show to serialize and deserialize string arrays.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.harishgowda.demo;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Arrays;

public class GsonArrayDemo {

    public static void main(String[] args) {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        String[] names = {"Jim","Tim","Jane"};

        System.out.println("String array to json: "+gson.toJson(names));

        String restData= "[\"Harish\",\"Menaka\",\"Shanvi\"]";

        String[] receivedNames= gson.fromJson(restData,String[].class);

        System.out.println("Json to String Array: " +Arrays.toString(receivedNames));

    }
}

Output:

1
2
3
4
5
6
String array to json: [
  "Jim",
  "Tim",
  "Jane"
]
Json to String Array: [Harish, Menaka, Shanvi]

Multidimensional arrays

Below program shows how to convert multidimentional array to json string and from json string to multidimentional array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.harishgowda.demo;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Arrays;

public class GsonArrayDemo {

    public static void main(String[] args) {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        String[][] items = {{"tomato","potato","onions"},{"mango","apple","grapes"}};

        System.out.println("Multidimensional array to json: "+gson.toJson(items));

        String restData= " [\n" +
                "  [\n" +
                "    \"tomato\",\n" +
                "    \"potato\",\n" +
                "    \"onions\"\n" +
                "  ],\n" +
                "  [\n" +
                "    \"mango\",\n" +
                "    \"apple\",\n" +
                "    \"grapes\"\n" +
                "  ]\n" +
                "]";

        String[][] receivedItems= gson.fromJson(restData,String[][].class);

        System.out.println("Json to Multidimensional Array: " +Arrays.deepToString(receivedItems));

    }
}

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Multidimensional array to json: [
  [
    "tomato",
    "potato",
    "onions"
  ],
  [
    "mango",
    "apple",
    "grapes"
  ]
]
Json to Multidimensional Array: [[tomato, potato, onions], [mango, apple, grapes]]

Serialize and deserialize array of objects

We will create array of Product which contains id and name.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Product {
    int productId;

    String productName;

    public Product(int productId, String productName) {
        this.productId = productId;
        this.productName = productName;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @Override
    public String toString() {
        return "Product{" +
                "productId=" + productId +
                ", productName='" + productName + '\'' +
                '}';
    }
}

First we will convert array of products to json string using toJson() method by passing array of products as argument.

To convert from json string to array of products we will use fromJson() method. First argument is the json string and second argument is Product[].class.

Complete program is shown as below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.harishgowda.demo;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Arrays;

public class GsonArrayDemo {

    public static void main(String[] args) {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        Product[] products = new Product[2];

        Product product1 = new Product(1,"Mobile");
        Product product2 = new Product(2,"Fan");

        products[0]=product1;
        products[1]=product2;

        System.out.println("Array of objects to json string: "+gson.toJson(products));

        String productData= "[\n" +
                "  {\n" +
                "    \"productId\": 1,\n" +
                "    \"productName\": \"Mobile\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"productId\": 2,\n" +
                "    \"productName\": \"Fan\"\n" +
                "  }\n" +
                "]";
        Product[] items = gson.fromJson(productData, Product[].class);

        System.out.println("Json string to Array of objects: "+Arrays.toString(items));




    }
}

class Product {
    int productId;

    String productName;

    public Product(int productId, String productName) {
        this.productId = productId;
        this.productName = productName;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @Override
    public String toString() {
        return "Product{" +
                "productId=" + productId +
                ", productName='" + productName + '\'' +
                '}';
    }
}

Output is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Array of objects to json string: [
  {
    "productId": 1,
    "productName": "Mobile"
  },
  {
    "productId": 2,
    "productName": "Fan"
  }
]
Json string to Array of objects: [Product{productId=1, productName='Mobile'}, Product{productId=2, productName='Fan'}]

So we have learnt how to serialize and deserialize all types of array objects using GSON in this blog.