forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (59 loc) · 2.92 KB
/
Main.java
File metadata and controls
71 lines (59 loc) · 2.92 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
package modern.challenge;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
Jsonb jsonb = JsonbBuilder.create();
Path pathArray = Paths.get("melons_array.json");
Path pathMap = Paths.get("melons_map.json");
Path pathRaw = Paths.get("melons_raw.json");
System.out.println("Read 'melons_array.json' as array of melons");
Melon[] melonsArray = jsonb.fromJson(
Files.newBufferedReader(pathArray, StandardCharsets.UTF_8), Melon[].class);
System.out.println("Melon array content: " + Arrays.toString(melonsArray));
System.out.println("Read 'melons_array.json' as List of melons");
List<Melon> melonsList = jsonb.fromJson(Files.newBufferedReader(
pathArray, StandardCharsets.UTF_8), ArrayList.class);
// or, as Type: new ArrayList<Melon>() {}.getClass().getGenericSuperclass()
System.out.println("Melon list content: " + melonsList);
System.out.println("Read 'melons_map.json' as Map of melons");
Map<String, Melon> melonsMap = jsonb.fromJson(Files.newBufferedReader(
pathMap, StandardCharsets.UTF_8), HashMap.class);
// or, as Type: new HashMap<String, Melon>() {}.getClass().getGenericSuperclass()
System.out.println("Melon list content: " + melonsMap);
System.out.println("Read 'melons_raw.json' line by line into a map");
Map<String, String> stringMap = new HashMap<>();
try (BufferedReader br = Files.newBufferedReader(pathRaw, StandardCharsets.UTF_8)) {
String line;
while ((line = br.readLine()) != null) {
stringMap = jsonb.fromJson(line, HashMap.class);
System.out.println("Current map is: " + stringMap);
}
}
System.out.println("Read 'melons_raw.json' line by line into a Melon");
try (BufferedReader br = Files.newBufferedReader(pathRaw, StandardCharsets.UTF_8)) {
String line;
while ((line = br.readLine()) != null) {
Melon melon = jsonb.fromJson(line, Melon.class);
System.out.println("Current melon is: " + melon);
}
}
System.out.println("Writing an Object to a JSON file ('melons_output.json') ...");
Path path = Paths.get("melons_output.json");
jsonb.toJson(melonsMap, Files.newBufferedWriter(path, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.WRITE));
}
}