-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonWriterExample.java
More file actions
48 lines (43 loc) · 1.37 KB
/
JsonWriterExample.java
File metadata and controls
48 lines (43 loc) · 1.37 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
package org.javaee7.chapter09;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonWriter;
import javax.json.JsonObject;
/**
*
* @author Juneau
*/
@ManagedBean(name="jsonWriter")
public class JsonWriterExample {
@Inject
JsonController jsonController;
public void writeJson() {
try {
JsonObject jsonObject = jsonController.buildJobsJson();
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(writer);
jsonWriter.writeObject(jsonObject);
jsonWriter.close();
writer.close();
// Write file
FileWriter fstream = new FileWriter("Jobs.json");
BufferedWriter out = new BufferedWriter(fstream);
out.write(writer.toString());
out.close();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "JSON Built",
"JSON Built"));
} catch (IOException ex) {
System.out.println(ex);
}
}
}