Skip to content

Commit deb6a9b

Browse files
author
Igor Polevoy
committed
#499 Add JsonHelper class
1 parent 3fe65c3 commit deb6a9b

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

javalite-common/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,18 @@
8585
</exclusion>
8686
</exclusions>
8787
</dependency>
88+
89+
<dependency>
90+
<groupId>org.codehaus.jackson</groupId>
91+
<artifactId>jackson-core-asl</artifactId>
92+
<version>1.9.13</version>
93+
</dependency>
94+
95+
<dependency>
96+
<groupId>org.codehaus.jackson</groupId>
97+
<artifactId>jackson-mapper-asl</artifactId>
98+
<version>1.9.13</version>
99+
</dependency>
100+
88101
</dependencies>
89102
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.javalite.common;
2+
3+
import org.codehaus.jackson.map.ObjectMapper;
4+
import org.codehaus.jackson.map.SerializationConfig;
5+
6+
import java.io.IOException;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* Convenience class to convert JSON strings to and from objects.
12+
*
13+
* @author Igor Polevoy on 5/26/16.
14+
*/
15+
16+
public class JsonHelper {
17+
private static final ObjectMapper mapper = new ObjectMapper();
18+
static {
19+
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
20+
}
21+
22+
23+
/**
24+
* Convert a JSON map to a Java Map
25+
*
26+
* @param json JSON map
27+
* @return Java Map.
28+
*/
29+
public static Map toMap(String json) {
30+
try {
31+
return mapper.readValue(json, Map.class);
32+
} catch (IOException e) {
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
37+
/**
38+
* Convert JSON Array to Java array of maps.
39+
*
40+
* @param json JSON array
41+
* @return Java array.
42+
*/
43+
public static Map[] toMaps(String json) {
44+
try {
45+
return mapper.readValue(json, Map[].class);
46+
} catch (IOException e) {
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
/**
52+
* Convert Java object to a JSON string.
53+
*
54+
* @param val Java object
55+
* @return JSON string.
56+
*/
57+
public static String toJsonString(Object val) {
58+
try {
59+
return mapper.writeValueAsString(val);
60+
} catch (Exception e) {
61+
throw new RuntimeException(e);
62+
}
63+
}
64+
65+
/**
66+
* Convert JSON array tp Java List
67+
*
68+
* @param json JSON array string.
69+
* @return Java List instance.
70+
*/
71+
public static List toList(String json) {
72+
try {
73+
return mapper.readValue(json, List.class);
74+
} catch (IOException e) {
75+
throw new RuntimeException(e);
76+
}
77+
}
78+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.javalite.common;
2+
3+
import org.junit.Test;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import static org.javalite.test.jspec.JSpec.$;
9+
import static org.javalite.test.jspec.JSpec.a;
10+
11+
/**
12+
* @author Igor Polevoy on 5/26/16.
13+
*/
14+
public class JsonHelperSpec {
15+
16+
@Test
17+
public void shouldConvertObject2JSON(){
18+
class Person {
19+
String firstName, lastName;
20+
public Person(String firstName, String lastName) {
21+
this.firstName = firstName;
22+
this.lastName = lastName;
23+
}
24+
25+
public String getFirstName() {
26+
return firstName;
27+
}
28+
29+
public String getLastName() {
30+
return lastName;
31+
}
32+
33+
public void setFirstName(String firstName) {
34+
this.firstName = firstName;
35+
}
36+
37+
public void setLastName(String lastName) {
38+
this.lastName = lastName;
39+
}
40+
}
41+
a(JsonHelper.toJsonString(new Person("John", "Smith"))).shouldBeEqual("{\"firstName\":\"John\",\"lastName\":\"Smith\"}");
42+
}
43+
44+
@Test
45+
public void shouldConvertArray2List(){
46+
List l = JsonHelper.toList("[1, 2]");
47+
$(l.size()).shouldBeEqual(2);
48+
$(l.get(0)).shouldBeEqual(1);
49+
$(l.get(1)).shouldBeEqual(2);
50+
}
51+
52+
@Test
53+
public void shouldConvertMap2Map(){
54+
Map m = JsonHelper.toMap("{ \"name\" : \"John\", \"age\": 22 }");
55+
$(m.size()).shouldBeEqual(2);
56+
$(m.get("name")).shouldBeEqual("John");
57+
$(m.get("age")).shouldBeEqual(22);
58+
}
59+
60+
@Test
61+
public void shouldConvertMaps2Maps(){
62+
Map[] maps = JsonHelper.toMaps("[{ \"name\" : \"John\", \"age\": 22 },{ \"name\" : \"Samantha\", \"age\": 21 }]");
63+
$(maps.length).shouldBeEqual(2);
64+
$(maps[0].get("name")).shouldBeEqual("John");
65+
$(maps[0].get("age")).shouldBeEqual(22);
66+
$(maps[1].get("name")).shouldBeEqual("Samantha");
67+
$(maps[1].get("age")).shouldBeEqual(21);
68+
}
69+
}

0 commit comments

Comments
 (0)