Skip to content

Commit a24f7db

Browse files
authored
Update api.md
1 parent d47b07b commit a24f7db

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

api.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ And best of all, you can mix them up when parsing one document. Let's see some c
2020

2121
Given this document `{"a": {"b": {"c": "d"}}}`
2222

23+
Parse with Java bind-api + any-api
24+
```
25+
public class ABC {
26+
public Any a;
27+
}
28+
29+
Jsoniter iter = Jsoniter.parse("{'a': {'b': {'c': 'd'}}}".replace('\'', '"'));
30+
ABC abc = iter.read(ABC.class);
31+
System.out.println(abc.a.get("b", "c"));
32+
```
33+
2334
Parse with Go bind-api + any-api
2435

2536
```
@@ -33,13 +44,39 @@ iter.Read(&abc)
3344
fmt.Println(abc.a.Get("b", "c"))
3445
```
3546

47+
3648
Tranditionally, parse json return `Object` or `interface{}`. Then the parer's job is done, and developer's nightmare begins. Being able to mix bind-api and any-api, we can leave parts of the value uncertain, and deal with them later. `Any` has api to extract data out of deeply nested data structure very easily.
3749

3850
Here is another example, `[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`
3951

52+
Parse with Java iterator-api + bind-api
53+
54+
```
55+
public class User {
56+
public int userId;
57+
public String name;
58+
public String[] tags;
59+
}
60+
61+
Jsoniter iter = Jsoniter.parse("[123, {'name': 'taowen', 'tags': ['crazy', 'hacker']}]".replace('\'', '"'));
62+
iter.readArray();
63+
int userId = iter.readInt();
64+
iter.readArray();
65+
User user = iter.read(User.class);
66+
user.userId = userId;
67+
iter.readArray(); // end of array
68+
System.out.println(user);
69+
```
70+
4071
Parse with Go iterator-api + bind-api
4172

4273
```
74+
type User struct {
75+
userId int
76+
name string
77+
tags []string
78+
}
79+
4380
iter := ParseString(`[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`)
4481
user := User{}
4582
iter.ReadArray()

0 commit comments

Comments
 (0)