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: api.md
+52-4Lines changed: 52 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ title: Json Iterator API
8
8
9
9
# API Choices
10
10
11
-
One thing does not fit all. Jsoniter always put developr friendlyness as top priority. No matter how many times faster you claim you can be, what most developer need is a json parser just get the job done. JSON being a weak typed data exchange format, when being parsed in language like Java or Go, it is very often we need to deal with type mismatch or uncertain data structure. Existing solution is not only slow to parse, but put too much work on the shoulder of developer. The motivation to reinvent this wheel is not performance, but to make the parsing as easy as it can be. Benchmarking is just a way to get your attention, sadly.
11
+
One thing does not fit all. Jsoniter always put developr friendliness as top priority. No matter how many times faster you claim you can be, what most developer need is a json parser just get the job done. JSON being a weak typed data exchange format, when being parsed in language like Java or Go, it is very often we need to deal with type mismatch or uncertain data structure. Existing solution is not only slow to parse, but put too much work on the shoulder of developer. The motivation to reinvent this wheel is not performance, but to make the parsing as easy as it can be. Benchmarking is just a way to get your attention, sadly.
12
12
13
13
Jsoniter gives you three api style choices:
14
14
@@ -264,13 +264,61 @@ It is not strictly schema free here. If the input is not string or int, but a ar
264
264
265
265
No matter how convenient the iterator api is, iterator is still just iterator. 99% of time, we want to bind the JSON input into object, then process it. Binding JSON to structured object gives the schema info to the parser. With structure, not only easier to underderstand, but also makes the parsing much much faster.
266
266
267
-
# Integration
267
+
The api is simple. In Java, you provide the class
268
+
269
+
```java
270
+
Jsoniter iter =Jsoniter.parse("[1,2,3]");
271
+
int[] val = iter.read(int[].class);
272
+
```
273
+
274
+
In Go, you provide pointer to your object
275
+
276
+
```go
277
+
iter:=ParseString(`[1,2,3]`)
278
+
slice:= []int{}
279
+
iter.Read(&slice)
280
+
```
281
+
282
+
If you want to use generics in Java, use this syntax:
283
+
284
+
```java
285
+
Jsoniter iter =Jsoniter.parse("[1,2,3]");
286
+
List<Integer> val = iter.read(newTypeLiteral<ArrayList<Integer>>(){});
287
+
```
288
+
289
+
## Bind callback
268
290
269
291
The downside of binding is there is always exception. We want the field to be string, but some input put the field as int. Being able to customize the binding is crucial. The answer is callback. Add callback hook in the binding process, then we can use iterator api to take back the control.
270
292
271
-
The downside of iterator is, it is tedious. If we want to read a lot of fields, writting switch case for all of them is not fun at all. It would be nice to mix bind api when we are driving the iterator. And jsoniter allows us to do that.
293
+
Register type decoder to parse element of specific type to use your callback
294
+
295
+
```
296
+
Jsoniter.registerTypeDecoder(Date.class, new Decoder() {
297
+
@Override
298
+
public Object decode(Type type, Jsoniter iter) throws IOException {
299
+
return new Date(iter.readLong());
300
+
}
301
+
});
302
+
Jsoniter iter = Jsoniter.parse("1481365190000");
303
+
Date date = iter.read(Date.class);
304
+
assertEquals(1481365190000L, date.getTime());
305
+
Jsoniter.clearDecoders();
306
+
```
272
307
273
-
In jsoniter, iterator is not some underlying implementation hidden away from bind api. Iterator api and bind api are at the same level, and they complement each other. There is no other json parser designed in the same way, as far as I know. Hope you enjoy the freedom as I do. Here is a quick example.
308
+
Register field decoder to special handle the fields chosen
309
+
310
+
```
311
+
Jsoniter.registerFieldDecoder(SimpleObject.class, "field1", new Decoder(){
312
+
@Override
313
+
public Object decode(Type type, Jsoniter iter) throws IOException {
314
+
return Integer.toString(iter.readInt());
315
+
}
316
+
});
317
+
Jsoniter iter = Jsoniter.parse("{'field1': 100}".replace('\'', '"'));
0 commit comments