-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathApp.java
More file actions
111 lines (85 loc) · 3.12 KB
/
App.java
File metadata and controls
111 lines (85 loc) · 3.12 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.hmkcode;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hmkcode.vo.Article;
public class App
{
public static void main( String[] args )
{
ObjectMapper mapper = new ObjectMapper();
try {
//POJO to JSON
mapper.writeValue(new File("article.json"), createArticle());
System.out.println("json created!");
//JSON to POJO
Article article = mapper.readValue(new File("article.json"), Article.class);
//"Raw" Data Binding Example
Map<String,Object> articleMap = mapper.readValue(new File("article.json"), Map.class);
System.out.println(article);
System.out.println(articleMap);
//Data binding Collection<E>
List<Article> articles = new LinkedList<Article>();
articles.add(createArticle());
articles.add(createArticle());
mapper.writeValue(new File("articles.json"), articles);
//( 1 ) Collection<Map>
List result = mapper.readValue(new File("articles.json"), List.class);
System.out.println(result.get(0).getClass());
System.out.println(result);
//( 2 ) Collection<Artilce>
result = mapper.readValue(new File("articles.json"), new TypeReference<List<Article>>() { });
System.out.println(result.get(0).getClass());
System.out.println(result);
System.out.println("---------------------------------------------------------");
//Tree
ObjectNode objectRoot = (ObjectNode) mapper.readTree(new File("article.json"));
Iterator<String> fields = objectRoot.fieldNames();
String field = "";
while(fields.hasNext()){
field = fields.next();
System.out.println("field: "+field);
}
System.out.println("---------------------------------------------------------");
ArrayNode arrayRoot = (ArrayNode) mapper.readTree(new File("articles.json"));
Iterator<JsonNode> elements = arrayRoot.elements();
JsonNode element;
while(elements.hasNext()){
element = elements.next();
fields = element.fieldNames();
field = "";
while(fields.hasNext()){
field = fields.next();
System.out.println("field: "+field);
}
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static Article createArticle(){
Article article = new Article();
article.setTitle("Jackson - Java to JSON & JSON to Java");
article.setUrl("http://hmkcode.com/jackson-java-json");
article.addCategory("Java");
article.addTag("Java");
article.addTag("Jackson");
article.addTag("JSON");
return article;
}
}