Understanding Primitive Values in GSON

Introduction

Working with primitive values in GSON is simple and straightforward.

Primitive values which are represented by primitive data types cannot be broken further.

Primitive data types examples include boolean, char, byte, short, int, long, float, and double.

In this blog we described how to get started with GSON.

Mostly GSON is used to serialize and deserialize custom objects in our projects.

GSON library can be used to serialize and deserialize primitive values as well.

To get started we will 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>

Serializing primitive values

To serialize primitive value to json we first initialise GSON object and call toJson() method on it.

Below program demonstrates how to serialize primitive using GSON library.

 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
package com.harishgowda.demo;

import com.google.gson.Gson;

public class App {

    public static void main(String[] args) {

        Gson gson = new Gson();
        int i = 100;
        System.out.println("int value: "+ gson.toJson(i));

        float f= 100.0f;
        System.out.println("float value: "+gson.toJson(f));

        long l= 100;
        System.out.println("long value: "+gson.toJson(l));

        short s = (short)100;
        System.out.println("short value: "+gson.toJson(s));

        double d= 100.0;
        System.out.println("double value: "+gson.toJson(d));

        boolean test= true;
        System.out.println("boolean value: "+gson.toJson(test));

        char c ='z';
        System.out.println("char value: "+gson.toJson(c));

        byte code = (byte) 0x00011110;
        System.out.println("byte value: "+gson.toJson(code));




    }
}
1
2
3
4
5
6
7
8
int value: 100
float value: 100.0
long value: 100
short value: 100
double value: 100.0
boolean value: true
char value: "z"
byte value: 16

Deserializing primitive values

To deserialize primitive value to json we first initialise GSON object and call fromJson() method on it.

We get json values as string and we convert them to respective data type by passing required datatype as the second argument to fromJson() method

Below program demonstrates how to deserialize primitive using GSON library.

 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
package com.harishgowda.demo;

import com.google.gson.Gson;

public class App {

    public static void main(String[] args) {

        Gson gson = new Gson();

        Integer deserint = gson.fromJson("200",Integer.class);

        System.out.println("deserint: "+deserint);

        Float deserfloat = gson.fromJson("200",Float.class);

        System.out.println("deserfloat: "+deserfloat);

        Double deserDouble = gson.fromJson("200",Double.class);

        System.out.println("deserDouble: "+deserDouble);

        Long deserLong = gson.fromJson("200",Long.class);

        System.out.println("deserLong: "+deserLong);

        Short deserShort = gson.fromJson("200",Short.class);

        System.out.println("deserShort: "+deserShort);

        Character deserChar = gson.fromJson("A",Character.class);

        System.out.println("deserChar: "+deserChar);


    }
}
1
2
3
4
5
6
deserint: 200
deserfloat: 200.0
deserDouble: 200.0
deserLong: 200
deserShort: 200
deserChar: A

Custom object having all primitive values

We can apply all we have learnt in above examples.

In below program we have a custom Employee object which contains all primitive values.

Given a populated employee object we convert to json string using toJson() method of GSON library.

We also take a dummy employee data string and convert to Employee object by calling fromJson() method of GSON library.

 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
78
79
80
81
82
83
package com.harishgowda.demo;

import com.google.gson.Gson;

public class App {

    public static void main(String[] args) {

        Gson gson = new Gson();

        Employee emp = new Employee(1,"Tim Doe",2000.5f,true);

        String empJson = gson.toJson(emp);

        System.out.println("Employee obj to Json String: "+empJson);

        String empData= "{\"empid\":201,\"empName\":\"Harry John\",\"salary\":5000.0,\"isMarried\":false}";

        Employee empUpdated = gson.fromJson(empData,Employee.class);

        System.out.println("Json String to Employee obj: "+empUpdated);

    }
}

class Employee {

    public int empid;
    public String empName;

    public float salary;

    public boolean isMarried;

    public Employee(int empid, String empName, float salary, boolean isMarried) {
        this.empid = empid;
        this.empName = empName;
        this.salary = salary;
        this.isMarried = isMarried;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    public boolean isMarried() {
        return isMarried;
    }

    public void setMarried(boolean married) {
        isMarried = married;
    }

    public int getEmpid() {
        return empid;
    }

    public void setEmpid(int empid) {
        this.empid = empid;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empid=" + empid +
                ", empName='" + empName + '\'' +
                ", salary=" + salary +
                ", isMarried=" + isMarried +
                '}';
    }
}
1
2
Employee obj to Json String: {"empid":1,"empName":"Tim Doe","salary":2000.5,"isMarried":true}
Json String to Employee obj: Employee{empid=201, empName='Harry John', salary=5000.0, isMarried=false}