/
Case2CustomizingInstance.java
61 lines (48 loc) · 1.89 KB
/
Case2CustomizingInstance.java
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
package jug.ua.meetup;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import static org.junit.Assert.assertEquals;
/**
* Created by Olena_Syrota on 4/18/2015.
*
* sdiscussion:
* - create instance or use builder (SPI approach)
* - how to provide customization of Jsonb instance
*/
public class Case2CustomizingInstance {
//GSON
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
//JACKSON
//No builder available. ObjectMapper class is all in one.
private ObjectMapper jackson = new ObjectMapper()
.configure(SerializationFeature.INDENT_OUTPUT, true);
//GENSON
private Genson genson = new GensonBuilder().useIndentation(true).create();
// demonstrates that every mapper has own notion of "pretty"
@Test
public void indentedSerializationExample() throws Exception {
Book book = new Book("Super Book", "Super Author");
String expOrdered =
"{\n" +
" \"title\": \"Super Book\",\n" +
" \"author\": \"Super Author\"\n" +
"}";
String gsonStr = gson.toJson(book);
JSONAssert.assertEquals(expOrdered, gsonStr, true);
String jacksonStr = jackson.writeValueAsString(book);
JSONAssert.assertEquals(expOrdered, jacksonStr, true);
String expLexic =
"{\n" +
" \"author\":\"Super Author\",\n" +
" \"title\":\"Super Book\"\n" +
"}";
String gensonStr = genson.serialize(book);
JSONAssert.assertEquals(expLexic, gensonStr, true);
}
}