forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyAny.java
More file actions
71 lines (58 loc) · 1.7 KB
/
LazyAny.java
File metadata and controls
71 lines (58 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.jsoniter.any;
import com.jsoniter.spi.JsonException;
import com.jsoniter.JsonIterator;
import com.jsoniter.ValueType;
import com.jsoniter.output.JsonStream;
import com.jsoniter.spi.TypeLiteral;
import java.io.IOException;
abstract class LazyAny extends Any {
protected final byte[] data;
protected final int head;
protected final int tail;
public LazyAny(byte[] data, int head, int tail) {
this.data = data;
this.head = head;
this.tail = tail;
}
public abstract ValueType valueType();
public final <T> T bindTo(T obj) {
try {
return parse().read(obj);
} catch (IOException e) {
throw new JsonException(e);
}
}
public final <T> T bindTo(TypeLiteral<T> typeLiteral, T obj) {
try {
return parse().read(typeLiteral, obj);
} catch (IOException e) {
throw new JsonException(e);
}
}
public final <T> T as(Class<T> clazz) {
try {
return parse().read(clazz);
} catch (IOException e) {
throw new JsonException(e);
}
}
public final <T> T as(TypeLiteral<T> typeLiteral) {
try {
return parse().read(typeLiteral);
} catch (IOException e) {
throw new JsonException(e);
}
}
public String toString() {
return new String(data, head, tail - head);
}
public final JsonIterator parse() {
JsonIterator iter = JsonIterator.tlsIter.get();
iter.reset(data, head, tail);
return iter;
}
@Override
public void writeTo(JsonStream stream) throws IOException {
stream.write(data, head, tail - head);
}
}