forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenAccess.java
More file actions
157 lines (140 loc) · 5.24 KB
/
CodegenAccess.java
File metadata and controls
157 lines (140 loc) · 5.24 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.jsoniter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// only uesd by generated code to access decoder
public class CodegenAccess {
public static byte nextToken(Jsoniter iter) throws IOException {
return iter.nextToken();
}
public static final boolean readBoolean(String cacheKey, Jsoniter iter) throws IOException {
return Codegen.getBooleanDecoder(cacheKey).decodeBoolean(iter);
}
public static final short readShort(String cacheKey, Jsoniter iter) throws IOException {
return Codegen.getShortDecoder(cacheKey).decodeShort(iter);
}
public static final int readInt(String cacheKey, Jsoniter iter) throws IOException {
return Codegen.getIntDecoder(cacheKey).decodeInt(iter);
}
public static final long readLong(String cacheKey, Jsoniter iter) throws IOException {
return Codegen.getLongDecoder(cacheKey).decodeLong(iter);
}
public static final float readFloat(String cacheKey, Jsoniter iter) throws IOException {
return Codegen.getFloatDecoder(cacheKey).decodeFloat(iter);
}
public static final double readDouble(String cacheKey, Jsoniter iter) throws IOException {
return Codegen.getDoubleDecoder(cacheKey).decodeDouble(iter);
}
public static final <T> T read(String cacheKey, Jsoniter iter) throws IOException {
return (T) Codegen.getDecoder(cacheKey, null).decode(iter);
}
public static boolean readArrayStart(Jsoniter iter) throws IOException {
byte c = iter.nextToken();
if (c != '[') {
throw iter.reportError("readArrayStart", "expect [ or n");
}
c = iter.nextToken();
if (c == ']') {
return false;
}
iter.unreadByte();
return true;
}
public static boolean readObjectStart(Jsoniter iter) throws IOException {
byte c = iter.nextToken();
if (c != '{') {
throw iter.reportError("readObjectStart", "expect { or n, found: " + (char)c);
}
c = iter.nextToken();
if (c == '}') {
return false;
}
iter.unreadByte();
return true;
}
public static void reportIncompleteObject(Jsoniter iter) {
throw iter.reportError("genObject", "expect }");
}
public static void reportIncompleteArray(Jsoniter iter) {
throw iter.reportError("genArray", "expect ]");
}
public static final int readObjectFieldAsHash(Jsoniter iter) throws IOException {
if (iter.nextToken() != '"') {
throw iter.reportError("readObjectFieldAsHash", "expect \"");
}
long hash = 0x811c9dc5;
for (; ; ) {
byte c = 0;
int i = iter.head;
for ( ;i < iter.tail; i++) {
c = iter.buf[i];
if (c == '"') {
break;
}
hash ^= c;
hash *= 0x1000193;
}
if (c == '"') {
iter.head = i + 1;
if (iter.nextToken() != ':') {
throw iter.reportError("readObjectFieldAsHash", "expect :");
}
return (int) hash;
}
if (!iter.loadMore()) {
throw iter.reportError("readObjectFieldAsHash", "unmatched quote");
}
}
}
public static final Slice readObjectFieldAsSlice(Jsoniter iter) throws IOException {
if (iter.nextToken() != '"') {
throw iter.reportError("readObjectFieldAsSlice", "expect \"");
}
Slice field = StringReader.readSlice(iter);
boolean notCopied = field != null;
if (skipWhitespacesWithoutLoadMore(iter)) {
if (notCopied) {
byte[] newBuf = new byte[field.len];
System.arraycopy(field.data, field.head, newBuf, 0, field.len);
field.data = newBuf;
field.head = 0;
field.len = newBuf.length;
}
if (!iter.loadMore()) {
throw iter.reportError("readObjectFieldAsSlice", "expect : after object field");
}
}
if (iter.buf[iter.head] != ':') {
throw iter.reportError("readObjectFieldAsSlice", "expect : after object field");
}
iter.head++;
if (skipWhitespacesWithoutLoadMore(iter)) {
if (notCopied) {
byte[] newBuf = new byte[field.len];
System.arraycopy(field.data, field.head, newBuf, 0, field.len);
field.data = newBuf;
field.head = 0;
field.len = newBuf.length;
}
if (!iter.loadMore()) {
throw iter.reportError("readObjectFieldAsSlice", "expect : after object field");
}
}
return field;
}
private final static boolean skipWhitespacesWithoutLoadMore(Jsoniter iter) throws IOException {
for (int i = iter.head; i < iter.tail; i++) {
byte c = iter.buf[i];
switch (c) {
case ' ':
case '\n':
case '\t':
case '\r':
continue;
}
iter.head = i;
return false;
}
return true;
}
}