Total Pageviews

2016/09/03

[jackson] How to generate json from java bean and vice versa

Problem
If I would like to do conversation with Java Bean to JSON and JSON to Java Bean, any library can be used?

How-To
Add dependency to your pom.xml
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

Create an User bean for test:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package albert.practice.json;

import java.io.Serializable;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private String id;
    private String login;
    private String mail;
}

Test code is as bellows:
 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
84
85
86
87
package albert.practice.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JacksonTest {

    public static void main(String[] args) {
        JacksonTest test = new JacksonTest();
        String jsonString = test.convertOjbectToJson();

        List<User> users = test.convertJsonToObject(jsonString);
        for (User user : users) {
           log.debug("user = " + user.toString());
        }
    }

    public List<User> convertJsonToObject(String jsonString) {
        ObjectMapper mapper = new ObjectMapper();
        List<User> users = new ArrayList<User>();
        try {
            users = mapper.readValue(jsonString, new TypeReference<List<User>>() {
        });
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
        return users;
    }

    public String convertOjbectToJson() {
        List<User> users = createUsers();
        String jsonString = "";

        ObjectMapper mapper = new ObjectMapper();

        try {
            jsonString = mapper.writeValueAsString(users);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.debug("jsonString = " + jsonString);
        return jsonString;
    }

    private List<User> createUsers() {
        User albert = new User();
        albert.setId("1");
        albert.setLogin("albert");
        albert.setMail("albert@gmail.com");

        User mandy = new User();
        mandy.setId("2");
        mandy.setLogin("mandy");
        mandy.setMail("mandy@gmail.com");

        User verio = new User();
        verio.setId("3");
        verio.setLogin("verio");
        verio.setMail("verio@gmail.com");

        List<User> users = new ArrayList<User>();
        users.add(albert);
        users.add(mandy);
        users.add(verio);

        return users;
    }

}

print log:
1
2
3
4
2016-06-01 15:35:39.630 [main] DEBUG albert.practice.json.JacksonTest - jsonString = [{"id":"1","login":"albert","mail":"albert@gmail.com"},{"id":"2","login":"mandy","mail":"mandy@gmail.com"},{"id":"3","login":"verio","mail":"verio@gmail.com"}]
2016-06-01 15:35:39.670 [main] DEBUG albert.practice.json.JacksonTest - user = User(id=1, login=albert, mail=albert@gmail.com)
2016-06-01 15:35:39.670 [main] DEBUG albert.practice.json.JacksonTest - user = User(id=2, login=mandy, mail=mandy@gmail.com)
2016-06-01 15:35:39.670 [main] DEBUG albert.practice.json.JacksonTest - user = User(id=3, login=verio, mail=verio@gmail.com)

Reference
[1] http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
[2] https://stackoverflow.com/questions/28821715/java-lang-classcastexception-java-util-linkedhashmap-cannot-be-cast-to-com-test

No comments: