0
# JSON Processing
1
2
JSON-P and JSON-B APIs for parsing, generating, and binding JSON data with streaming and object mapping support.
3
4
## JSON Processing (JSON-P)
5
6
```java { .api }
7
public final class Json {
8
public static JsonReader createReader(InputStream in);
9
public static JsonReader createReader(Reader reader);
10
public static JsonWriter createWriter(OutputStream out);
11
public static JsonWriter createWriter(Writer writer);
12
public static JsonObjectBuilder createObjectBuilder();
13
public static JsonArrayBuilder createArrayBuilder();
14
public static JsonParser createParser(InputStream in);
15
public static JsonParser createParser(Reader reader);
16
public static JsonGenerator createGenerator(OutputStream out);
17
public static JsonGenerator createGenerator(Writer writer);
18
}
19
20
public interface JsonReader extends Closeable {
21
JsonStructure read();
22
JsonObject readObject();
23
JsonArray readArray();
24
void close();
25
}
26
27
public interface JsonWriter extends Closeable {
28
void writeArray(JsonArray array);
29
void writeObject(JsonObject object);
30
void write(JsonStructure value);
31
void close();
32
}
33
```
34
35
## JSON Binding (JSON-B)
36
37
```java { .api }
38
public interface Jsonb extends AutoCloseable {
39
<T> String toJson(T object);
40
<T> String toJson(T object, Type runtimeType);
41
<T> void toJson(T object, OutputStream out);
42
<T> void toJson(T object, Writer writer);
43
<T> T fromJson(String str, Class<T> type);
44
<T> T fromJson(String str, Type runtimeType);
45
<T> T fromJson(Reader reader, Class<T> type);
46
<T> T fromJson(Reader reader, Type runtimeType);
47
<T> T fromJson(InputStream stream, Class<T> type);
48
<T> T fromJson(InputStream stream, Type runtimeType);
49
void close() throws Exception;
50
}
51
52
public final class JsonbBuilder {
53
public static JsonbBuilder create();
54
public JsonbBuilder withConfig(JsonbConfig config);
55
public JsonbBuilder withProvider(JsonbProvider provider);
56
public Jsonb build();
57
}
58
```
59
60
## Usage Example
61
62
```java
63
// JSON-P
64
JsonObject jsonObject = Json.createObjectBuilder()
65
.add("name", "John Doe")
66
.add("age", 30)
67
.add("active", true)
68
.build();
69
70
// JSON-B
71
Jsonb jsonb = JsonbBuilder.create();
72
User user = new User("John", "john@example.com");
73
String json = jsonb.toJson(user);
74
User deserializedUser = jsonb.fromJson(json, User.class);
75
```