You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-8Lines changed: 38 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@ JsonBeans is a lightweight library that makes it easy to serialize and deseriali
21
21
22
22
The `Json` class uses reflection to automatically serialize objects to JSON. For example, here are two classes (getters/setters and constructors omitted):
23
23
24
+
```java
24
25
publicclassPerson {
25
26
privateString name;
26
27
privateint age;
@@ -31,29 +32,36 @@ The `Json` class uses reflection to automatically serialize objects to JSON. For
That is compact, but hardly legible. The `prettyPrint` method can be used:
53
59
60
+
```java
54
61
Json json =newJson();
55
62
System.out.println(json.prettyPrint(person));
56
-
63
+
```
64
+
```json
57
65
{
58
66
"name": "Nate",
59
67
"age": 31,
@@ -70,13 +78,16 @@ That is compact, but hardly legible. The `prettyPrint` method can be used:
70
78
}
71
79
]
72
80
}
81
+
```
73
82
74
83
Note that the class for the `PhoneNumber` objects in the `ArrayList numbers` field appears in the JSON. This is required to recreate the object graph from the JSON because `ArrayList` can hold any type of object. Class names are only output when they are required for deserialization. If the field was `ArrayList<PhoneNumber> numbers` then class names would only appear when an item in the list extends `PhoneNumber`. If you know the concrete type or aren't using generics, you can avoid class names being written by telling the `Json` class the types:
@@ -114,14 +128,17 @@ When writing the class cannot be avoided, an alias can be given:
114
128
}
115
129
]
116
130
}
131
+
```
117
132
118
133
JsonBeans can write and read both JSON and a couple JSON-like formats. It supports "javascript", where the object property names are only quoted when needed. It also supports a "minimal" format, where both object property names and values are only quoted when needed.
@@ -136,23 +153,28 @@ JsonBeans can write and read both JSON and a couple JSON-like formats. It suppor
136
153
}
137
154
]
138
155
}
156
+
```
139
157
140
158
## Reading object graphs
141
159
142
160
The Json class uses reflection to automatically deserialize objects from JSON. Here is how to deserialize the JSON from the previous examples:
143
161
162
+
```java
144
163
Json json =newJson();
145
164
String text = json.toJson(person);
146
165
Person person2 = json.fromJson(Person.class, text);
166
+
```
147
167
148
168
The type passed to `fromJson` is the type of the root of the object graph. From this, JsonBeans can determine the types of all the fields and all other objects encountered, recursively. The "knownType" and "elementType" of the root can be passed to `toJson`. This is useful if the type of the root object is not known:
@@ -170,19 +192,23 @@ The type passed to `fromJson` is the type of the root of the object graph. From
170
192
}
171
193
]
172
194
}
195
+
```
173
196
174
197
To read the JSON as a DOM of maps, arrays, and values, the `JsonReader` class can be used:
175
198
199
+
```java
176
200
Json json =newJson();
177
201
String text = json.toJson(person, Object.class);
178
202
JsonValue root =newJsonReader().parse(text);
203
+
```
179
204
180
205
The `JsonValue` describes a JSON object, array, string, float, long, boolean, or null.
181
206
182
207
## Customizing serialization
183
208
184
209
Serialization can be customized by either having the class to be serialized implement the `Json.Serializable` interface, or by registering a `Json.Serializer` with the `Json` instance. This example writes the phone numbers as an object with a single field:
@@ -202,7 +228,8 @@ Serialization can be customized by either having the class to be serialized impl
202
228
String text = json.prettyPrint(person);
203
229
System.out.println(text);
204
230
Person person2 = json.fromJson(Person.class, text);
205
-
231
+
```
232
+
```json
206
233
{
207
234
"name": "Nate",
208
235
"age": 31
@@ -215,11 +242,13 @@ Serialization can be customized by either having the class to be serialized impl
215
242
}
216
243
]
217
244
}
245
+
```
218
246
219
247
In the `Json.Serializable` interface methods, the `Json` instance is given. It has many methods to read and write data to the JSON. When using `Json.Serializable`, the surrounding JSON object is handled automatically in the `write` method. This is why the `read` method always receives a `JsonMap`.
220
248
221
249
`Json.Serializer` provides more control over what is output, requiring `writeObjectStart` and `writeObjectEnd` to be called to achieve the same effect. A JSON array or a simple value could be output instead of an object. `Json.Serializer` also allows the object creation to be customized.
0 commit comments