1+ package com .hmkcode ;
2+
3+ import java .io .File ;
4+ import java .io .IOException ;
5+ import java .util .Iterator ;
6+ import java .util .LinkedList ;
7+ import java .util .List ;
8+ import java .util .Map ;
9+
10+ import com .fasterxml .jackson .core .JsonGenerationException ;
11+ import com .fasterxml .jackson .core .type .TypeReference ;
12+ import com .fasterxml .jackson .databind .JsonMappingException ;
13+ import com .fasterxml .jackson .databind .JsonNode ;
14+ import com .fasterxml .jackson .databind .ObjectMapper ;
15+ import com .fasterxml .jackson .databind .SerializationFeature ;
16+ import com .fasterxml .jackson .databind .node .ArrayNode ;
17+ import com .fasterxml .jackson .databind .node .ObjectNode ;
18+ import com .hmkcode .vo .Article ;
19+
20+
21+ public class App
22+ {
23+ public static void main ( String [] args )
24+ {
25+
26+ ObjectMapper mapper = new ObjectMapper ();
27+
28+ try {
29+
30+ //POJO to JSON
31+ mapper .writeValue (new File ("article.json" ), createArticle ());
32+ System .out .println ("json created!" );
33+
34+ //JSON to POJO
35+ Article article = mapper .readValue (new File ("article.json" ), Article .class );
36+
37+ //"Raw" Data Binding Example
38+ Map <String ,Object > articleMap = mapper .readValue (new File ("article.json" ), Map .class );
39+
40+ System .out .println (article );
41+
42+ System .out .println (articleMap );
43+
44+ //Data binding Collection<E>
45+ List <Article > articles = new LinkedList <Article >();
46+
47+ articles .add (createArticle ());
48+ articles .add (createArticle ());
49+
50+ mapper .writeValue (new File ("articles.json" ), articles );
51+
52+ //( 1 ) Collection<Map>
53+ List result = mapper .readValue (new File ("articles.json" ), List .class );
54+ System .out .println (result .get (0 ).getClass ());
55+ System .out .println (result );
56+
57+ //( 2 ) Collection<Artilce>
58+ result = mapper .readValue (new File ("articles.json" ), new TypeReference <List <Article >>() { });
59+ System .out .println (result .get (0 ).getClass ());
60+ System .out .println (result );
61+
62+ System .out .println ("---------------------------------------------------------" );
63+ //Tree
64+ ObjectNode objectRoot = (ObjectNode ) mapper .readTree (new File ("article.json" ));
65+ Iterator <String > fields = objectRoot .fieldNames ();
66+ String field = "" ;
67+ while (fields .hasNext ()){
68+ field = fields .next ();
69+ System .out .println ("field: " +field );
70+ }
71+ System .out .println ("---------------------------------------------------------" );
72+
73+ ArrayNode arrayRoot = (ArrayNode ) mapper .readTree (new File ("articles.json" ));
74+
75+ Iterator <JsonNode > elements = arrayRoot .elements ();
76+ JsonNode element ;
77+
78+ while (elements .hasNext ()){
79+ element = elements .next ();
80+ fields = element .fieldNames ();
81+ field = "" ;
82+ while (fields .hasNext ()){
83+ field = fields .next ();
84+ System .out .println ("field: " +field );
85+ }
86+
87+ }
88+
89+ } catch (JsonGenerationException e ) {
90+ e .printStackTrace ();
91+ } catch (JsonMappingException e ) {
92+ e .printStackTrace ();
93+ } catch (IOException e ) {
94+ e .printStackTrace ();
95+ }
96+ }
97+
98+ private static Article createArticle (){
99+
100+ Article article = new Article ();
101+
102+ article .setTitle ("Jackson - Java to JSON & JSON to Java" );
103+ article .setUrl ("http://hmkcode.com/jackson-java-json" );
104+ article .addCategory ("Java" );
105+ article .addTag ("Java" );
106+ article .addTag ("Jackson" );
107+ article .addTag ("JSON" );
108+
109+ return article ;
110+ }
111+ }
0 commit comments