forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonPrettyPrintEx.java
More file actions
32 lines (23 loc) · 849 Bytes
/
JsonPrettyPrintEx.java
File metadata and controls
32 lines (23 loc) · 849 Bytes
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
package com.zetcode;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import java.io.StringWriter;
import java.time.LocalDate;
import java.util.HashMap;
public class JsonPrettyPrintEx {
public static void main(String[] args) {
var born = LocalDate.of(1992, 3, 2).toString();
var json = Json.createObjectBuilder()
.add("name", "John Doe")
.add("occupation", "gardener")
.add("born", born).build();
var config = new HashMap<String, Boolean>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
var jwf = Json.createWriterFactory(config);
var sw = new StringWriter();
try (var jsonWriter = jwf.createWriter(sw)) {
jsonWriter.writeObject(json);
System.out.println(sw.toString());
}
}
}