Skip to content

Commit 4b65094

Browse files
committed
seperate codegen access
1 parent 9d41a09 commit 4b65094

3 files changed

Lines changed: 39 additions & 44 deletions

File tree

src/main/java/com/jsoniter/Codegen.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ private static void genField(StringBuilder lines, Field field, String cacheKey)
329329
return;
330330
}
331331
getDecoder(fieldCacheKey, fieldType); // put decoder into cache
332-
append(lines, String.format("obj.%s = (%s)iter.read(\"%s\");",
332+
append(lines, String.format("obj.%s = (%s)com.jsoniter.CodegenAccess.read(\"%s\", iter);",
333333
field.getName(), fieldTypeName, fieldCacheKey));
334334
return;
335335
}
@@ -343,18 +343,10 @@ private static String genReadOp(Type type) {
343343
if (nativeRead != null) {
344344
return nativeRead;
345345
}
346-
return String.format("(%s)iter.read(\"%s\", %s.class)",
347-
clazz.getCanonicalName(), TypeLiteral.generateCacheKey(clazz), clazz.getCanonicalName());
348346
}
349-
if (type instanceof ParameterizedType) {
350-
ParameterizedType pType = (ParameterizedType) type;
351-
Class clazz = (Class) pType.getRawType();
352-
Type[] args = pType.getActualTypeArguments();
353-
String cacheKey = TypeLiteral.generateCacheKey(type);
354-
getDecoder(cacheKey, clazz, args); // set the decoder to cache
355-
return String.format("(%s)iter.read(\"%s\")", clazz.getCanonicalName(), cacheKey);
356-
}
357-
throw new IllegalArgumentException("unsupported type: " + type);
347+
String cacheKey = TypeLiteral.generateCacheKey(type);
348+
getDecoder(cacheKey, type); // set the decoder to cache
349+
return String.format("com.jsoniter.CodegenAccess.read(\"%s\", iter)", cacheKey);
358350
}
359351

360352
private static String genArray(Class clazz) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jsoniter;
2+
3+
import java.io.IOException;
4+
5+
// only uesd by generated code to access decoder
6+
public class CodegenAccess {
7+
8+
public static final boolean readBoolean(String cacheKey, Jsoniter iter) throws IOException {
9+
return Codegen.getBooleanDecoder(cacheKey).decodeBoolean(iter);
10+
}
11+
12+
public static final short readShort(String cacheKey, Jsoniter iter) throws IOException {
13+
return Codegen.getShortDecoder(cacheKey).decodeShort(iter);
14+
}
15+
16+
public static final int readInt(String cacheKey, Jsoniter iter) throws IOException {
17+
return Codegen.getIntDecoder(cacheKey).decodeInt(iter);
18+
}
19+
20+
public static final long readLong(String cacheKey, Jsoniter iter) throws IOException {
21+
return Codegen.getLongDecoder(cacheKey).decodeLong(iter);
22+
}
23+
24+
public static final float readFloat(String cacheKey, Jsoniter iter) throws IOException {
25+
return Codegen.getFloatDecoder(cacheKey).decodeFloat(iter);
26+
}
27+
28+
public static final double readDouble(String cacheKey, Jsoniter iter) throws IOException {
29+
return Codegen.getDoubleDecoder(cacheKey).decodeDouble(iter);
30+
}
31+
32+
public static final <T> T read(String cacheKey, Jsoniter iter) throws IOException {
33+
return (T) Codegen.getDecoder(cacheKey, null).decode(iter);
34+
}
35+
}

src/main/java/com/jsoniter/Jsoniter.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ public final boolean readNull() throws IOException {
163163
return false;
164164
}
165165

166-
public final boolean readBoolean(String cacheKey) throws IOException {
167-
return Codegen.getBooleanDecoder(cacheKey).decodeBoolean(this);
168-
}
169-
170166
public final boolean readBoolean() throws IOException {
171167
byte c = readByte();
172168
switch (c) {
@@ -181,10 +177,6 @@ public final boolean readBoolean() throws IOException {
181177
}
182178
}
183179

184-
public final short readShort(String cacheKey) throws IOException {
185-
return Codegen.getShortDecoder(cacheKey).decodeShort(this);
186-
}
187-
188180
public final short readShort() throws IOException {
189181
int v = readInt();
190182
if (Short.MIN_VALUE <= v && v <= Short.MAX_VALUE) {
@@ -194,10 +186,6 @@ public final short readShort() throws IOException {
194186
}
195187
}
196188

197-
public final int readInt(String cacheKey) throws IOException {
198-
return Codegen.getIntDecoder(cacheKey).decodeInt(this);
199-
}
200-
201189
public final int readInt() throws IOException {
202190
byte c = readByte();
203191
if (c == '-') {
@@ -231,10 +219,6 @@ public final int readUnsignedInt() throws IOException {
231219
return result;
232220
}
233221

234-
public final long readLong(String cacheKey) throws IOException {
235-
return Codegen.getLongDecoder(cacheKey).decodeLong(this);
236-
}
237-
238222
public final long readLong() throws IOException {
239223
byte c = readByte();
240224
if (c == '-') {
@@ -740,18 +724,10 @@ final String readNumber() throws IOException {
740724
return new String(reusableChars, 0, j);
741725
}
742726

743-
public final float readFloat(String cacheKey) throws IOException {
744-
return Codegen.getFloatDecoder(cacheKey).decodeFloat(this);
745-
}
746-
747727
public final float readFloat() throws IOException {
748728
return Float.valueOf(readNumber());
749729
}
750730

751-
public final double readDouble(String cacheKey) throws IOException {
752-
return Codegen.getDoubleDecoder(cacheKey).decodeDouble(this);
753-
}
754-
755731
public final double readDouble() throws IOException {
756732
return Double.valueOf(readNumber());
757733
}
@@ -805,10 +781,6 @@ public final <T> T read(TypeLiteral<T> typeLiteral) throws IOException {
805781
return (T) Codegen.getDecoder(typeLiteral.cacheKey, type).decode(this);
806782
}
807783

808-
public final <T> T read(String cacheKey) throws IOException {
809-
return (T) Codegen.getDecoder(cacheKey, null).decode(this);
810-
}
811-
812784
public final void skip() throws IOException {
813785
byte c = readByte();
814786
switch (c) {
@@ -1029,10 +1001,6 @@ public static void registerFieldDecoder(TypeLiteral typeLiteral, String field, D
10291001
Codegen.addNewDecoder(field + "@" + typeLiteral.cacheKey, decoder);
10301002
}
10311003

1032-
public static void clearDecoders() {
1033-
Codegen.cache.clear();
1034-
}
1035-
10361004
public static void registerExtension(Extension extension) {
10371005
Codegen.registerExtension(extension);
10381006
}

0 commit comments

Comments
 (0)