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
+37Lines changed: 37 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,17 @@ And best of all, you can mix them up when parsing one document. Let's see some c
20
20
21
21
Given this document `{"a": {"b": {"c": "d"}}}`
22
22
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
+
23
34
Parse with Go bind-api + any-api
24
35
25
36
```
@@ -33,13 +44,39 @@ iter.Read(&abc)
33
44
fmt.Println(abc.a.Get("b", "c"))
34
45
```
35
46
47
+
36
48
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.
37
49
38
50
Here is another example, `[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`
39
51
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
+
40
71
Parse with Go iterator-api + bind-api
41
72
42
73
```
74
+
type User struct {
75
+
userId int
76
+
name string
77
+
tags []string
78
+
}
79
+
43
80
iter := ParseString(`[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`)
0 commit comments