Skip to content

Commit 75224ea

Browse files
authored
Update api.md
1 parent 3673d3c commit 75224ea

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

api.md

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -289,46 +289,35 @@ List<Integer> val = iter.read(new TypeLiteral<ArrayList<Integer>>(){});
289289

290290
The type literal instance might be reused globally, which is thread-safe.
291291

292-
## Bind callback
293-
294-
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.
295-
296-
Register type decoder to parse element of specific type to use your callback
297-
298-
```java
299-
JsonIterator.registerTypeDecoder(Date.class, new Decoder() {
300-
@Override
301-
public Object decode(JsonIterator iter) throws IOException {
302-
return new Date(iter.readLong());
303-
}
304-
});
305-
JsonIterator iter = JsonIterator.parse("1481365190000");
306-
Date date = iter.read(Date.class);
307-
assertEquals(1481365190000L, date.getTime());
308-
JsonIterator.clearDecoders();
309-
```
292+
## Bind to existing object
310293

311-
Register field decoder to special handle the fields chosen
294+
Java version also support bind to existing object
312295

313296
```java
314-
public class CustomizedObject {
315-
public String field2;
297+
public static class TestObj1 {
316298
public String field1;
299+
public String field2;
317300
}
318301

319-
JsonIterator.registerFieldDecoder(CustomizedObject.class, "field1", new Decoder(){
302+
TestObj1 testObj = new TestObj1();
303+
testObj.field2 = "world";
304+
JsonIterator iter = JsonIterator.parse("{ 'field1' : 'hello' }".replace('\'', '"'));
305+
iter.read(testObj);
320306

321-
@Override
322-
public Object decode(JsonIterator iter) throws IOException {
323-
return Integer.toString(iter.readInt());
324-
}
325-
});
326-
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
327-
CustomizedObject myObject = iter.read(CustomizedObject.class);
328-
assertEquals("100", myObject.field1);
307+
System.out.println(testObj.field1); // "hello"
308+
System.out.println(testObj.field2); // "world"
329309
```
330310

331-
same api available in go version.
311+
Object and map will be reused as it is. Array will not be reused. Collection will be reused, but with elements cleared.
312+
313+
## Bind callback
314+
315+
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.
316+
317+
The callback itself is too complicated here, please checkout extension guides:
318+
319+
* Java bind-api extension guide: http://jsoniter.com/java-extension.html
320+
* Go bind-api extension guide: http://jsoniter.com/go-extension.html
332321

333322
# Any API
334323

0 commit comments

Comments
 (0)