下面列出了怎么用com.alibaba.fastjson.parser.JSONLexerBase的API类实例代码及写法,或者点击链接到github查看源代码。
protected Enum scanEnum(JSONLexerBase lexer, char[] name_chars, ObjectDeserializer fieldValueDeserilizer) {
EnumDeserializer enumDeserializer = null;
if (fieldValueDeserilizer instanceof EnumDeserializer) {
enumDeserializer = (EnumDeserializer) fieldValueDeserilizer;
}
if (enumDeserializer == null) {
lexer.matchStat = JSONLexer.NOT_MATCH;
return null;
}
long enumNameHashCode = lexer.scanFieldSymbol(name_chars);
if (lexer.matchStat > 0) {
return enumDeserializer.getEnumByHashCode(enumNameHashCode);
} else {
return null;
}
}
private void _deserialize_endCheck(Context context, MethodVisitor mw, Label reset_) {
mw.visitIntInsn(ILOAD, context.var("matchedCount"));
mw.visitJumpInsn(IFLE, reset_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "token", "()I");
mw.visitLdcInsn(JSONToken.RBRACE);
mw.visitJumpInsn(IF_ICMPNE, reset_);
// mw.visitLabel(nextToken_);
_quickNextTokenComma(context, mw);
}
private void _quickNextToken(Context context, MethodVisitor mw, int token) {
Label quickElse_ = new Label(), quickEnd_ = new Label();
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
if (token == JSONToken.LBRACE) {
mw.visitVarInsn(BIPUSH, '{');
} else if (token == JSONToken.LBRACKET) {
mw.visitVarInsn(BIPUSH, '[');
} else {
throw new IllegalStateException();
}
mw.visitJumpInsn(IF_ICMPNE, quickElse_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
mw.visitInsn(POP);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitLdcInsn(token);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
mw.visitJumpInsn(GOTO, quickEnd_);
mw.visitLabel(quickElse_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitLdcInsn(token);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "nextToken", "(I)V");
mw.visitLabel(quickEnd_);
}
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
if (lexer.seekArrayToItem(index)
&& context.eval)
{
context.object = parser.parse();
}
}
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
if (lexer.seekArrayToItem(index)
&& context.eval)
{
context.object = parser.parse();
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected static void parseArray(Collection collection, //
ObjectDeserializer deser, //
DefaultJSONParser parser, //
Type type, //
Object fieldName) {
final JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
int token = lexer.token();
if (token == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
token = lexer.token();
return;
}
if (token != JSONToken.LBRACKET) {
parser.throwException(token);
}
char ch = lexer.getCurrent();
if (ch == '[') {
lexer.next();
lexer.setToken(JSONToken.LBRACKET);
} else {
lexer.nextToken(JSONToken.LBRACKET);
}
if (lexer.token() == JSONToken.RBRACKET) {
lexer.nextToken();
return;
}
int index = 0;
for (;;) {
Object item = deser.deserialze(parser, type, index);
collection.add(item);
index++;
if (lexer.token() == JSONToken.COMMA) {
ch = lexer.getCurrent();
if (ch == '[') {
lexer.next();
lexer.setToken(JSONToken.LBRACKET);
} else {
lexer.nextToken(JSONToken.LBRACKET);
}
} else {
break;
}
}
token = lexer.token();
if (token != JSONToken.RBRACKET) {
parser.throwException(token);
}
ch = lexer.getCurrent();
if (ch == ',') {
lexer.next();
lexer.setToken(JSONToken.COMMA);
} else {
lexer.nextToken(JSONToken.COMMA);
}
// parser.accept(JSONToken.RBRACKET, JSONToken.COMMA);
}
private void defineVarLexer(Context context, MethodVisitor mw) {
mw.visitVarInsn(ALOAD, 1);
mw.visitFieldInsn(GETFIELD, DefaultJSONParser, "lexer", desc(JSONLexer.class));
mw.visitTypeInsn(CHECKCAST, JSONLexerBase); // cast
mw.visitVarInsn(ASTORE, context.var("lexer"));
}
private void _quickNextTokenComma(Context context, MethodVisitor mw) {
Label quickElse_ = new Label(), quickElseIf0_ = new Label(), quickElseIf1_ = new Label(), quickElseIf2_ = new Label(), quickEnd_ = new Label();
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
mw.visitInsn(DUP);
mw.visitVarInsn(ISTORE, context.var("ch"));
mw.visitVarInsn(BIPUSH, ',');
mw.visitJumpInsn(IF_ICMPNE, quickElseIf0_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
mw.visitInsn(POP);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitLdcInsn(JSONToken.COMMA);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
mw.visitJumpInsn(GOTO, quickEnd_);
mw.visitLabel(quickElseIf0_);
mw.visitVarInsn(ILOAD, context.var("ch"));
mw.visitVarInsn(BIPUSH, '}');
mw.visitJumpInsn(IF_ICMPNE, quickElseIf1_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
mw.visitInsn(POP);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitLdcInsn(JSONToken.RBRACE);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
mw.visitJumpInsn(GOTO, quickEnd_);
mw.visitLabel(quickElseIf1_);
mw.visitVarInsn(ILOAD, context.var("ch"));
mw.visitVarInsn(BIPUSH, ']');
mw.visitJumpInsn(IF_ICMPNE, quickElseIf2_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
mw.visitInsn(POP);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitLdcInsn(JSONToken.RBRACKET);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
mw.visitJumpInsn(GOTO, quickEnd_);
mw.visitLabel(quickElseIf2_);
mw.visitVarInsn(ILOAD, context.var("ch"));
mw.visitVarInsn(BIPUSH, JSONLexer.EOI);
mw.visitJumpInsn(IF_ICMPNE, quickElse_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitLdcInsn(JSONToken.EOF);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
mw.visitJumpInsn(GOTO, quickEnd_);
mw.visitLabel(quickElse_);
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "nextToken", "()V");
mw.visitLabel(quickEnd_);
}
private void _deserialze_obj(Context context, MethodVisitor mw, Label reset_, FieldInfo fieldInfo,
Class<?> fieldClass, int i) {
Label matched_ = new Label();
Label _end_if = new Label();
mw.visitVarInsn(ALOAD, context.var("lexer"));
mw.visitVarInsn(ALOAD, 0);
mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "matchField", "([C)Z");
mw.visitJumpInsn(IFNE, matched_);
mw.visitInsn(ACONST_NULL);
mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
mw.visitJumpInsn(GOTO, _end_if);
mw.visitLabel(matched_);
_setFlag(mw, context, i);
// increment matchedCount
mw.visitVarInsn(ILOAD, context.var("matchedCount"));
mw.visitInsn(ICONST_1);
mw.visitInsn(IADD);
mw.visitVarInsn(ISTORE, context.var("matchedCount"));
_deserObject(context, mw, fieldInfo, fieldClass, i);
mw.visitVarInsn(ALOAD, 1);
mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getResolveStatus", "()I");
mw.visitLdcInsn(com.alibaba.fastjson.parser.DefaultJSONParser.NeedToResolve);
mw.visitJumpInsn(IF_ICMPNE, _end_if);
mw.visitVarInsn(ALOAD, 1);
mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getLastResolveTask", "()" + desc(ResolveTask.class));
mw.visitVarInsn(ASTORE, context.var("resolveTask"));
mw.visitVarInsn(ALOAD, context.var("resolveTask"));
mw.visitVarInsn(ALOAD, 1);
mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getContext", "()" + desc(ParseContext.class));
mw.visitFieldInsn(PUTFIELD, type(ResolveTask.class), "ownerContext", desc(ParseContext.class));
mw.visitVarInsn(ALOAD, context.var("resolveTask"));
mw.visitVarInsn(ALOAD, 0);
mw.visitLdcInsn(fieldInfo.name);
mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "getFieldDeserializer",
"(Ljava/lang/String;)" + desc(FieldDeserializer.class));
mw.visitFieldInsn(PUTFIELD, type(ResolveTask.class), "fieldDeserializer", desc(FieldDeserializer.class));
mw.visitVarInsn(ALOAD, 1);
mw.visitLdcInsn(com.alibaba.fastjson.parser.DefaultJSONParser.NONE);
mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "setResolveStatus", "(I)V");
mw.visitLabel(_end_if);
}
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
JSONArray array;
if (context.object == null) {
context.object = array = new JSONArray();
} else {
array = (JSONArray) context.object;
}
for (int i = array.size(); i < propertyNamesHash.length; ++i) {
array.add(null);
}
// if (lexer.token() == JSONToken.LBRACKET) {
// lexer.nextToken();
// JSONArray array;
//
// array = new JSONArray();
// for (;;) {
// if (lexer.token() == JSONToken.LBRACE) {
// int index = lexer.seekObjectToField(propertyNamesHash);
// int matchStat = lexer.matchStat;
// if (matchStat == JSONLexer.VALUE) {
// Object value;
// switch (lexer.token()) {
// case JSONToken.LITERAL_INT:
// value = lexer.integerValue();
// lexer.nextToken();
// break;
// case JSONToken.LITERAL_STRING:
// value = lexer.stringVal();
// lexer.nextToken();
// break;
// default:
// value = parser.parse();
// break;
// }
//
// array.add(index, value);
// if (lexer.token() == JSONToken.RBRACE) {
// lexer.nextToken();
// continue;
// } else {
// lexer.skipObject();
// }
// } else {
// lexer.skipObject();
// }
// }
//
// if (lexer.token() == JSONToken.RBRACKET) {
// break;
// } else if (lexer.token() == JSONToken.COMMA) {
// lexer.nextToken();
// continue;
// } else {
// throw new JSONException("illegal json.");
// }
// }
//
// context.object = array;
// return;
// }
for_:
for (;;) {
int index = lexer.seekObjectToField(propertyNamesHash);
int matchStat = lexer.matchStat;
if (matchStat == JSONLexer.VALUE) {
Object value;
switch (lexer.token()) {
case JSONToken.LITERAL_INT:
value = lexer.integerValue();
lexer.nextToken(JSONToken.COMMA);
break;
case JSONToken.LITERAL_FLOAT:
value = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
break;
case JSONToken.LITERAL_STRING:
value = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
break;
default:
value = parser.parse();
break;
}
array.set(index, value);
if (lexer.token() == JSONToken.COMMA) {
continue for_;
}
}
break;
}
}
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
JSONArray array;
if (context.object == null) {
context.object = array = new JSONArray();
} else {
array = (JSONArray) context.object;
}
for (int i = array.size(); i < propertyNamesHash.length; ++i) {
array.add(null);
}
// if (lexer.token() == JSONToken.LBRACKET) {
// lexer.nextToken();
// JSONArray array;
//
// array = new JSONArray();
// for (;;) {
// if (lexer.token() == JSONToken.LBRACE) {
// int index = lexer.seekObjectToField(propertyNamesHash);
// int matchStat = lexer.matchStat;
// if (matchStat == JSONLexer.VALUE) {
// Object value;
// switch (lexer.token()) {
// case JSONToken.LITERAL_INT:
// value = lexer.integerValue();
// lexer.nextToken();
// break;
// case JSONToken.LITERAL_STRING:
// value = lexer.stringVal();
// lexer.nextToken();
// break;
// default:
// value = parser.parse();
// break;
// }
//
// array.add(index, value);
// if (lexer.token() == JSONToken.RBRACE) {
// lexer.nextToken();
// continue;
// } else {
// lexer.skipObject();
// }
// } else {
// lexer.skipObject();
// }
// }
//
// if (lexer.token() == JSONToken.RBRACKET) {
// break;
// } else if (lexer.token() == JSONToken.COMMA) {
// lexer.nextToken();
// continue;
// } else {
// throw new JSONException("illegal json.");
// }
// }
//
// context.object = array;
// return;
// }
for_:
for (;;) {
int index = lexer.seekObjectToField(propertyNamesHash);
int matchStat = lexer.matchStat;
if (matchStat == JSONLexer.VALUE) {
Object value;
switch (lexer.token()) {
case JSONToken.LITERAL_INT:
value = lexer.integerValue();
lexer.nextToken(JSONToken.COMMA);
break;
case JSONToken.LITERAL_FLOAT:
value = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
break;
case JSONToken.LITERAL_STRING:
value = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
break;
default:
value = parser.parse();
break;
}
array.set(index, value);
if (lexer.token() == JSONToken.COMMA) {
continue for_;
}
}
break;
}
}
public FastJsonResumableParser(JSONLexerBase lexer, SurfingContext context, StaticPrimitiveHolder staticPrimitiveHolder) {
this.lexer = lexer;
this.context = context;
this.staticPrimitiveHolder = staticPrimitiveHolder;
}