Skip to content

Commit a9d8aa4

Browse files
committed
updated examples
1 parent 3c228f0 commit a9d8aa4

File tree

1 file changed

+200
-169
lines changed

1 file changed

+200
-169
lines changed

Java-JsonPojo-Example/src/main/java/com/howtoprogram/jsonpojo/JsonPojoUtils.java

Lines changed: 200 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -4,182 +4,213 @@
44
import java.io.IOException;
55
import java.util.ArrayList;
66
import java.util.Arrays;
7+
import java.util.HashMap;
78
import java.util.List;
9+
import java.util.Map;
810

911
import com.fasterxml.jackson.core.JsonProcessingException;
1012
import com.fasterxml.jackson.core.type.TypeReference;
1113
import com.fasterxml.jackson.databind.ObjectMapper;
1214

1315
public class JsonPojoUtils {
1416

15-
public static void main(String[] args) {
16-
// JSON to object
17-
toObject();
18-
toList();
19-
toArray();
20-
21-
// Object to JSON
22-
toJSON();
23-
collectionToJSON();
24-
// Working with file
25-
serializeToFile();
26-
readFromFile();
27-
28-
}
29-
30-
/**
31-
* Converts a JSON string to a {@link Book} object
32-
*/
33-
public static void toObject() {
34-
35-
String json = "{\"id\":2,\"name\":\"Thinking in Java\",\"author\":\"Bruce Eckel\"}";
36-
// create Jackson ObjectMapper
37-
ObjectMapper mapper = new ObjectMapper();
38-
try {
39-
// Converts JSON string to Java object
40-
Book book = mapper.readValue(json, Book.class);
41-
42-
System.out.println(book);
43-
44-
} catch (JsonProcessingException e) {
45-
e.printStackTrace();
46-
} catch (IOException e) {
47-
e.printStackTrace();
48-
}
49-
50-
}
51-
52-
/**
53-
* Converts a JSON string to a list of {@link Book}
54-
*/
55-
public static void toList() {
56-
57-
String json =
58-
"[{\"id\":1,\"name\":\"Java How To Program\",\"author\":\"Paul Deitel\"}, {\"id\":2,\"name\":\"Thinking in Java\",\"author\":\"Bruce Eckel\"}]";
59-
// create Jackson ObjectMapper
60-
ObjectMapper mapper = new ObjectMapper();
61-
try {
62-
// Converts JSON string to Java object
63-
List<Book> books = mapper.readValue(json, new TypeReference<List<Book>>() {});
64-
System.out.println(books);
65-
// Or converted from the array
66-
books = Arrays.asList(mapper.readValue(json, Book[].class));
67-
} catch (JsonProcessingException e) {
68-
e.printStackTrace();
69-
} catch (IOException e) {
70-
e.printStackTrace();
71-
}
72-
73-
}
74-
75-
/**
76-
* Converts a JSON string to an array of {@link Book}
77-
*/
78-
public static void toArray() {
79-
80-
String json =
81-
"[{\"id\":1,\"name\":\"Java How To Program\",\"author\":\"Paul Deitel\"}, {\"id\":2,\"name\":\"Thinking in Java\",\"author\":\"Bruce Eckel\"}]";
82-
// create Jackson ObjectMapper
83-
ObjectMapper mapper = new ObjectMapper();
84-
try {
85-
// Converts JSON string to an array
86-
Book[] booksArray = mapper.readValue(json, Book[].class);
87-
88-
} catch (Exception e) {
89-
e.printStackTrace();
90-
}
91-
92-
}
93-
94-
/**
95-
* Converts a lit of {@link Book} to JSON string.
96-
*/
97-
public static void toJSON() {
98-
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
99-
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
100-
101-
// Converts an Java object to JSON string
102-
ObjectMapper mapper = new ObjectMapper();
103-
try {
104-
String json = mapper.writeValueAsString(book1);
105-
System.out.println(json);
106-
} catch (JsonProcessingException e) {
107-
e.printStackTrace();
108-
}
109-
List<Book> books = new ArrayList<Book>();
110-
books.add(book1);
111-
books.add(book2);
112-
try {
113-
String json = mapper.writeValueAsString(books);
114-
System.out.println(json);
115-
} catch (JsonProcessingException e) {
116-
e.printStackTrace();
117-
}
118-
}
119-
120-
/**
121-
* Converts a list of {@link Book} to JSON
122-
*/
123-
public static void collectionToJSON() {
124-
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
125-
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
126-
List<Book> books = new ArrayList<Book>();
127-
books.add(book1);
128-
books.add(book2);
129-
130-
// Converts an Java object to JSON string
131-
ObjectMapper mapper = new ObjectMapper();
132-
133-
try {
134-
// Converts the list to JSON
135-
String json = mapper.writeValueAsString(books);
136-
System.out.println(json);
137-
} catch (JsonProcessingException e) {
138-
e.printStackTrace();
139-
}
140-
}
141-
142-
/**
143-
* Serializes a list of {@link Book} to the file: d:/books.json
144-
*/
145-
public static void serializeToFile() {
146-
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
147-
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
148-
149-
// Converts an Java object to JSON string
150-
ObjectMapper mapper = new ObjectMapper();
151-
try {
152-
String json = mapper.writeValueAsString(book1);
153-
System.out.println(json);
154-
} catch (JsonProcessingException e) {
155-
e.printStackTrace();
156-
}
157-
List<Book> books = new ArrayList<Book>();
158-
books.add(book1);
159-
books.add(book2);
160-
try {
161-
// Serialize to file
162-
mapper.writeValue(new File("d:/books.json"), books);
163-
} catch (Exception e) {
164-
e.printStackTrace();
165-
}
166-
}
167-
168-
/**
169-
* Read JSON content from the file: d:/books.json and convert to an array. Assume that the
170-
* d:/books.json was exist.
171-
*/
172-
public static void readFromFile() {
173-
ObjectMapper mapper = new ObjectMapper();
174-
try {
175-
Book[] books = mapper.readValue(new File("d:/books.json"), Book[].class);
176-
System.out.println(books);
177-
} catch (Exception e) {
178-
e.printStackTrace();
179-
}
180-
181-
}
17+
public static void main(String[] args) {
18+
// JSON to object
19+
toObject();
20+
toList();
21+
toArray();
22+
23+
// Object to JSON
24+
toJSON();
25+
collectionToJSON();
26+
// Working with file
27+
serializeToFile();
28+
readFromFile();
29+
// collectionToJSONPrettyPrint();
30+
mapToJson();
31+
}
32+
33+
/**
34+
* Converts a JSON string to a {@link Book} object
35+
*/
36+
public static void toObject() {
37+
38+
String json = "{\"id\":2,\"name\":\"Thinking in Java\",\"author\":\"Bruce Eckel\"}";
39+
// create Jackson ObjectMapper
40+
ObjectMapper mapper = new ObjectMapper();
41+
try {
42+
// Converts JSON string to Java object
43+
Book book = mapper.readValue(json, Book.class);
44+
45+
System.out.println(book);
46+
47+
} catch (JsonProcessingException e) {
48+
e.printStackTrace();
49+
} catch (IOException e) {
50+
e.printStackTrace();
51+
}
52+
53+
}
54+
55+
/**
56+
* Converts a JSON string to a list of {@link Book}
57+
*/
58+
public static void toList() {
59+
60+
String json = "[{\"id\":1,\"name\":\"Java How To Program\",\"author\":\"Paul Deitel\"}, "
61+
+ "{\"id\":2,\"name\":\"Thinking in Java\",\"author\":\"Bruce Eckel\"}]";
62+
63+
try {
64+
ObjectMapper mapper = new ObjectMapper();
65+
List<Book> books = mapper.readValue(json, new TypeReference<List<Book>>() {
66+
});
67+
System.out.println(books);
68+
// Or converted from the array
69+
books = Arrays.asList(mapper.readValue(json, Book[].class));
70+
} catch (JsonProcessingException e) {
71+
e.printStackTrace();
72+
} catch (IOException e) {
73+
e.printStackTrace();
74+
}
75+
76+
}
77+
78+
/**
79+
* Converts a JSON string to an array of {@link Book}
80+
*/
81+
public static void toArray() {
82+
83+
String json = "[{\"id\":1,\"name\":\"Java How To Program\",\"author\":\"Paul Deitel\"}, "
84+
+ "{\"id\":2,\"name\":\"Thinking in Java\",\"author\":\"Bruce Eckel\"}]";
85+
try {
86+
ObjectMapper mapper = new ObjectMapper();
87+
Book[] booksArray = mapper.readValue(json, Book[].class);
88+
89+
} catch (Exception e) {
90+
e.printStackTrace();
91+
}
92+
93+
}
94+
95+
/**
96+
* Converts a lit of {@link Book} to JSON string.
97+
*/
98+
public static void toJSON() {
99+
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
100+
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
101+
102+
List<Book> books = new ArrayList<Book>();
103+
books.add(book1);
104+
books.add(book2);
105+
try {
106+
ObjectMapper mapper = new ObjectMapper();
107+
String json = mapper.writeValueAsString(books);
108+
System.out.println(json);
109+
} catch (JsonProcessingException e) {
110+
e.printStackTrace();
111+
}
112+
}
113+
114+
/**
115+
* Converts a list of {@link Book} to JSON
116+
*/
117+
public static void mapToJson() {
118+
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
119+
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
120+
Map<Long, Book> bookMap = new HashMap<Long, Book>();
121+
bookMap.put(book1.getId(), book1);
122+
bookMap.put(book2.getId(), book2);
123+
124+
try {
125+
// Converts the list to JSON
126+
ObjectMapper mapper = new ObjectMapper();
127+
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bookMap);
128+
// json = mapper.writeValueAsString(bookMap);
129+
System.out.println(json);
130+
} catch (JsonProcessingException e) {
131+
e.printStackTrace();
132+
}
133+
}
134+
135+
/**
136+
* Converts a list of {@link Book} to JSON
137+
*/
138+
public static void collectionToJSON() {
139+
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
140+
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
141+
List<Book> books = new ArrayList<Book>();
142+
books.add(book1);
143+
books.add(book2);
144+
145+
try {
146+
// Converts the list to JSON
147+
ObjectMapper mapper = new ObjectMapper();
148+
String json = mapper.writeValueAsString(books);
149+
System.out.println(json);
150+
} catch (JsonProcessingException e) {
151+
e.printStackTrace();
152+
}
153+
}
154+
155+
/**
156+
* Converts a list of {@link Book} to JSON
157+
*/
158+
public static void collectionToJSONPrettyPrint() {
159+
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
160+
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
161+
List<Book> books = new ArrayList<Book>();
162+
books.add(book1);
163+
books.add(book2);
164+
165+
try {
166+
// Converts the list to JSON
167+
ObjectMapper mapper = new ObjectMapper();
168+
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(books);
169+
System.out.println(json);
170+
} catch (JsonProcessingException e) {
171+
e.printStackTrace();
172+
}
173+
}
174+
175+
/**
176+
* Serializes a list of {@link Book} to the file: d:/books.json
177+
*/
178+
public static void serializeToFile() {
179+
Book book1 = new Book(1l, "Java How To Program", "Paul Deitel");
180+
Book book2 = new Book(2l, "Thinking in Java", "Bruce Eckel");
181+
182+
// Converts an Java object to JSON string
183+
ObjectMapper mapper = new ObjectMapper();
184+
try {
185+
String json = mapper.writeValueAsString(book1);
186+
System.out.println(json);
187+
} catch (JsonProcessingException e) {
188+
e.printStackTrace();
189+
}
190+
List<Book> books = new ArrayList<Book>();
191+
books.add(book1);
192+
books.add(book2);
193+
try {
194+
// Serialize to file
195+
mapper.writeValue(new File("d:/books.json"), books);
196+
} catch (Exception e) {
197+
e.printStackTrace();
198+
}
199+
}
200+
201+
/**
202+
* Read JSON content from the file: d:/books.json and convert to an array.
203+
* Assume that the d:/books.json was exist.
204+
*/
205+
public static void readFromFile() {
206+
ObjectMapper mapper = new ObjectMapper();
207+
try {
208+
Book[] books = mapper.readValue(new File("d:/books.json"), Book[].class);
209+
System.out.println(books);
210+
} catch (Exception e) {
211+
e.printStackTrace();
212+
}
213+
214+
}
182215

183216
}
184-
185-

0 commit comments

Comments
 (0)