forked from elasticio/java-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.java
More file actions
84 lines (67 loc) · 1.81 KB
/
Copy pathJSON.java
File metadata and controls
84 lines (67 loc) · 1.81 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
72
73
74
75
76
77
78
79
80
81
82
83
84
package io.elastic.api;
import javax.json.*;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.concurrent.Callable;
/**
* JSON utilities.
*/
public final class JSON {
private JSON() {
}
/**
* Parses a String into a {@link JsonObject}.
*
* @param input string to parse
* @return JsonObject
*/
public static JsonObject parseObject(String input) {
final JsonReader reader = createReader(input);
if (reader == null) {
return null;
}
try {
return reader.readObject();
} finally {
reader.close();
}
}
/**
* Parses a String into a {@link JsonArray}.
*
* @param input string to parse
* @return JsonArray
*/
public static JsonArray parseArray(String input) {
final JsonReader reader = createReader(input);
if (reader == null) {
return null;
}
try {
return reader.readArray();
} finally {
reader.close();
}
}
private static <T> JsonReader createReader(final String input) {
if (input == null) {
return null;
}
final JsonReader reader = Json.createReader(
new ByteArrayInputStream(input.getBytes()));
return reader;
}
/**
* Writes a {@link JsonObject} into a String and returns it.
*
* @param object object to stringify
* @return String representation of the object
*/
public static String stringify(final JsonObject object) {
final StringWriter writer = new StringWriter();
final JsonWriter jsonWriter = Json.createWriter(writer);
jsonWriter.writeObject(object);
jsonWriter.close();
return writer.toString();
}
}