Skip to content

Commit 9a73d8b

Browse files
authored
Update api.md
1 parent a24f7db commit 9a73d8b

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

api.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: Json Iterator API
88

99
# API Choices
1010

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.
1212

1313
Jsoniter gives you three api style choices:
1414

@@ -264,13 +264,61 @@ It is not strictly schema free here. If the input is not string or int, but a ar
264264

265265
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.
266266

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(new TypeLiteral<ArrayList<Integer>>(){});
287+
```
288+
289+
## Bind callback
268290

269291
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.
270292

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+
```
272307

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('\'', '"'));
318+
SimpleObject myObject = iter.read(SimpleObject.class);
319+
assertEquals("100", myObject.field1);
320+
Jsoniter.clearDecoders();
321+
```
274322

275323

276324
# API List

0 commit comments

Comments
 (0)