json
This commit is contained in:
@@ -360,37 +360,36 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldBooleanValue(byte[] fieldBytes, char[] fieldChars, boolean comma, boolean value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
public boolean writeFieldBooleanValue(Object fieldArray, boolean comma, boolean value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldByteValue(byte[] fieldBytes, char[] fieldChars, boolean comma, byte value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
public boolean writeFieldByteValue(Object fieldArray, boolean comma, byte value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldShortValue(byte[] fieldBytes, char[] fieldChars, boolean comma, short value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
public boolean writeFieldShortValue(Object fieldArray, boolean comma, short value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldIntValue(byte[] fieldBytes, char[] fieldChars, boolean comma, int value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
public boolean writeFieldIntValue(Object fieldArray, boolean comma, int value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldLongValue(byte[] fieldBytes, char[] fieldChars, boolean comma, long value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
public boolean writeFieldLongValue(Object fieldArray, boolean comma, long value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldObjectValue(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, Encodeable encodeable, Object value) {
|
||||
public boolean writeFieldObjectValue(Object fieldArray, boolean comma, Encodeable encodeable, Object value) {
|
||||
if (value == null && !nullable()) {
|
||||
return comma;
|
||||
}
|
||||
byte[] bs1 = fieldBytes;
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
int expandsize = expand(1 + bs1.length);
|
||||
if (expandsize == 0) { // 只需要一个buffer
|
||||
final ByteBuffer buffer = this.buffers[currBufIndex];
|
||||
@@ -411,11 +410,11 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldStringValue(byte[] fieldBytes, char[] fieldChars, boolean comma, String value) {
|
||||
public boolean writeFieldStringValue(Object fieldArray, boolean comma, String value) {
|
||||
if (value == null || (tiny() && value.isEmpty())) {
|
||||
return comma;
|
||||
}
|
||||
byte[] bs1 = fieldBytes;
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
int expandsize = expand(1 + bs1.length);
|
||||
if (expandsize == 0) { // 只需要一个buffer
|
||||
final ByteBuffer buffer = this.buffers[currBufIndex];
|
||||
@@ -436,12 +435,11 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean writeFieldLatin1Value(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, boolean quote, String value) {
|
||||
protected boolean writeFieldLatin1Value(Object fieldArray, boolean comma, boolean quote, String value) {
|
||||
if (value == null || (tiny() && value.isEmpty())) {
|
||||
return comma;
|
||||
}
|
||||
byte[] bs1 = fieldBytes;
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
byte[] bs2 = Utility.latin1ByteArray(value);
|
||||
int expandsize = expand(bs1.length + bs2.length + 3);
|
||||
if (expandsize == 0) { // 只需要一个buffer
|
||||
|
||||
@@ -186,8 +186,8 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldBooleanValue(byte[] fieldBytes, char[] fieldChars, boolean comma, boolean value) {
|
||||
byte[] bs1 = fieldBytes;
|
||||
public boolean writeFieldBooleanValue(Object fieldArray, boolean comma, boolean value) {
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
byte[] bs2 = value ? BYTES_TUREVALUE : BYTES_FALSEVALUE;
|
||||
int len1 = bs1.length;
|
||||
int len2 = bs2.length;
|
||||
@@ -201,8 +201,8 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldByteValue(byte[] fieldBytes, char[] fieldChars, boolean comma, byte value) {
|
||||
byte[] bs1 = fieldBytes;
|
||||
public boolean writeFieldByteValue(Object fieldArray, boolean comma, byte value) {
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
byte[] bs2 = value >= 0 ? TENTHOUSAND_BYTES[value] : TENTHOUSAND_BYTES2[-value];
|
||||
int len1 = bs1.length;
|
||||
int len2 = bs2.length;
|
||||
@@ -216,8 +216,8 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldShortValue(byte[] fieldBytes, char[] fieldChars, boolean comma, short value) {
|
||||
byte[] bs1 = fieldBytes;
|
||||
public boolean writeFieldShortValue(Object fieldArray, boolean comma, short value) {
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
byte[] bs2 = value >= 0 ? TENTHOUSAND_BYTES[value] : TENTHOUSAND_BYTES2[-value];
|
||||
int len1 = bs1.length;
|
||||
int len2 = bs2.length;
|
||||
@@ -231,8 +231,8 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldIntValue(byte[] fieldBytes, char[] fieldChars, boolean comma, int value) {
|
||||
byte[] bs1 = fieldBytes;
|
||||
public boolean writeFieldIntValue(Object fieldArray, boolean comma, int value) {
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
byte[] src = expand(len1 + 12);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -243,8 +243,8 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldLongValue(byte[] fieldBytes, char[] fieldChars, boolean comma, long value) {
|
||||
byte[] bs1 = fieldBytes;
|
||||
public boolean writeFieldLongValue(Object fieldArray, boolean comma, long value) {
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
byte[] src = expand(len1 + 21);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -255,12 +255,11 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldObjectValue(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, Encodeable encodeable, Object value) {
|
||||
public boolean writeFieldObjectValue(Object fieldArray, boolean comma, Encodeable encodeable, Object value) {
|
||||
if (value == null && !nullable()) {
|
||||
return comma;
|
||||
}
|
||||
byte[] bs1 = fieldBytes;
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
byte[] src = expand(1 + len1);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -271,11 +270,11 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldStringValue(byte[] fieldBytes, char[] fieldChars, boolean comma, String value) {
|
||||
public boolean writeFieldStringValue(Object fieldArray, boolean comma, String value) {
|
||||
if (value == null || (tiny() && value.isEmpty())) {
|
||||
return comma;
|
||||
}
|
||||
byte[] bs1 = fieldBytes;
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
byte[] src = expand(1 + len1);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -286,12 +285,11 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean writeFieldLatin1Value(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, boolean quote, String value) {
|
||||
protected boolean writeFieldLatin1Value(Object fieldArray, boolean comma, boolean quote, String value) {
|
||||
if (value == null || (tiny() && value.isEmpty())) {
|
||||
return comma;
|
||||
}
|
||||
byte[] bs1 = fieldBytes;
|
||||
byte[] bs1 = (byte[]) fieldArray;
|
||||
byte[] bs2 = Utility.latin1ByteArray(value);
|
||||
int len1 = bs1.length;
|
||||
int len2 = bs2.length;
|
||||
|
||||
@@ -141,8 +141,8 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldBooleanValue(byte[] fieldBytes, char[] fieldChars, boolean comma, boolean value) {
|
||||
char[] bs1 = fieldChars;
|
||||
public boolean writeFieldBooleanValue(Object fieldArray, boolean comma, boolean value) {
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
char[] bs2 = value ? CHARS_TUREVALUE : CHARS_FALSEVALUE;
|
||||
int len1 = bs1.length;
|
||||
int len2 = bs2.length;
|
||||
@@ -156,8 +156,8 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldShortValue(byte[] fieldBytes, char[] fieldChars, boolean comma, short value) {
|
||||
char[] bs1 = fieldChars;
|
||||
public boolean writeFieldShortValue(Object fieldArray, boolean comma, short value) {
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
char[] bs2 = value >= 0 ? TENTHOUSAND_CHARS[value] : TENTHOUSAND_CHARS2[-value];
|
||||
int len1 = bs1.length;
|
||||
int len2 = bs2.length;
|
||||
@@ -171,8 +171,8 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldByteValue(byte[] fieldBytes, char[] fieldChars, boolean comma, byte value) {
|
||||
char[] bs1 = fieldChars;
|
||||
public boolean writeFieldByteValue(Object fieldArray, boolean comma, byte value) {
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
char[] bs2 = value >= 0 ? TENTHOUSAND_CHARS[value] : TENTHOUSAND_CHARS2[-value];
|
||||
int len1 = bs1.length;
|
||||
int len2 = bs2.length;
|
||||
@@ -186,8 +186,8 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldIntValue(byte[] fieldBytes, char[] fieldChars, boolean comma, int value) {
|
||||
char[] bs1 = fieldChars;
|
||||
public boolean writeFieldIntValue(Object fieldArray, boolean comma, int value) {
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
char[] src = expand(bs1.length + 12);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -198,8 +198,8 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldLongValue(byte[] fieldBytes, char[] fieldChars, boolean comma, long value) {
|
||||
char[] bs1 = fieldChars;
|
||||
public boolean writeFieldLongValue(Object fieldArray, boolean comma, long value) {
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
char[] src = expand(len1 + 21);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -210,12 +210,11 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldObjectValue(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, Encodeable encodeable, Object value) {
|
||||
public boolean writeFieldObjectValue(Object fieldArray, boolean comma, Encodeable encodeable, Object value) {
|
||||
if (value == null && !nullable()) {
|
||||
return comma;
|
||||
}
|
||||
char[] bs1 = fieldChars;
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
char[] src = expand(1 + len1);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -226,11 +225,11 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean writeFieldStringValue(byte[] fieldBytes, char[] fieldChars, boolean comma, String value) {
|
||||
public boolean writeFieldStringValue(Object fieldArray, boolean comma, String value) {
|
||||
if (value == null || (tiny() && value.isEmpty())) {
|
||||
return comma;
|
||||
}
|
||||
char[] bs1 = fieldChars;
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
char[] src = expand(1 + len1);
|
||||
if (comma) src[count++] = BYTE_COMMA;
|
||||
@@ -241,12 +240,11 @@ public class JsonCharsWriter extends JsonWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean writeFieldLatin1Value(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, boolean quote, String value) {
|
||||
protected boolean writeFieldLatin1Value(Object fieldArray, boolean comma, boolean quote, String value) {
|
||||
if (value == null || (tiny() && value.isEmpty())) {
|
||||
return comma;
|
||||
}
|
||||
char[] bs1 = fieldChars;
|
||||
char[] bs1 = (char[]) fieldArray;
|
||||
int len1 = bs1.length;
|
||||
int len2 = value.length();
|
||||
|
||||
|
||||
@@ -111,7 +111,6 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
|
||||
final String jsonwriterDesc = org.redkale.asm.Type.getDescriptor(JsonWriter.class);
|
||||
final String encodeableDesc = org.redkale.asm.Type.getDescriptor(Encodeable.class);
|
||||
final String objEncoderDesc = org.redkale.asm.Type.getDescriptor(ObjectEncoder.class);
|
||||
final String objectDesc = org.redkale.asm.Type.getDescriptor(Object.class);
|
||||
final String valtypeDesc = org.redkale.asm.Type.getDescriptor(clazz);
|
||||
// ------------------------------------------------------------------------------
|
||||
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
|
||||
@@ -215,7 +214,6 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
|
||||
}
|
||||
|
||||
int elementIndex = -1;
|
||||
{
|
||||
{ // out.writeTo('{');
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
@@ -233,122 +231,23 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
|
||||
commaLabel = new Label();
|
||||
mv.visitLabel(commaLabel);
|
||||
}
|
||||
// comma = out.writeFieldIntValue(ageFieldBytes, ageFieldChars, comma, value.getAge());
|
||||
for (AccessibleObject element : elements) {
|
||||
elementIndex++;
|
||||
final String fieldName = factory.readConvertFieldName(clazz, element);
|
||||
final Class fieldType = readGetSetFieldType(element);
|
||||
mv.visitVarInsn(ALOAD, 1); // JsonWriter
|
||||
mv.visitVarInsn(ALOAD, 0); // this.xxxFieldBytes 第一个参数
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "FieldBytes", "[B");
|
||||
mv.visitVarInsn(ALOAD, 0); // this.xxxFieldChars 第二个参数
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "FieldChars", "[C");
|
||||
if (commaLabel != null) {
|
||||
mv.visitVarInsn(ILOAD, 3); // comma 第三个参数
|
||||
} else {
|
||||
mv.visitInsn(elementIndex == 0 ? ICONST_0 : ICONST_1); // comma=false 第三个参数
|
||||
}
|
||||
if (mixedNames.containsKey(fieldName)) { // Encodeable 第四个参数
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "Encoder", encodeableDesc);
|
||||
}
|
||||
mv.visitVarInsn(ALOAD, 2); // value.getXXX() 第四/五个参数
|
||||
if (element instanceof Field) {
|
||||
mv.visitFieldInsn(
|
||||
GETFIELD,
|
||||
valtypeName,
|
||||
((Field) element).getName(),
|
||||
org.redkale.asm.Type.getDescriptor(fieldType));
|
||||
} else {
|
||||
mv.visitMethodInsn(
|
||||
((Method) element).getDeclaringClass().isInterface() ? INVOKEINTERFACE : INVOKEVIRTUAL,
|
||||
valtypeName,
|
||||
((Method) element).getName(),
|
||||
"()" + org.redkale.asm.Type.getDescriptor(fieldType),
|
||||
false);
|
||||
}
|
||||
if (fieldType == boolean.class || fieldType == Boolean.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldBooleanValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == byte.class || fieldType == Byte.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldByteValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == short.class || fieldType == Short.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldShortValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == char.class || fieldType == Character.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldCharValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == int.class || fieldType == Integer.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldIntValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == float.class || fieldType == Float.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldFloatValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == long.class || fieldType == Long.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldLongValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == double.class || fieldType == Double.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldDoubleValue",
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == String.class) {
|
||||
String writeFieldName = "writeFieldStringValue";
|
||||
if (isConvertStandardString(factory, element)) {
|
||||
writeFieldName = "writeFieldStandardStringValue";
|
||||
}
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
writeFieldName,
|
||||
"([B[CZ" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else {
|
||||
// writeFieldObjectValue(fieldBytes, fieldChars, comma, encodeable, value)
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldObjectValue",
|
||||
"([B[CZ" + encodeableDesc + objectDesc + ")Z",
|
||||
false);
|
||||
}
|
||||
if (commaLabel != null && elementIndex + 1 < elements.size()) {
|
||||
mv.visitVarInsn(ISTORE, 3); // comma = out.writeFieldXXXValue()
|
||||
} else {
|
||||
mv.visitInsn(POP);
|
||||
}
|
||||
}
|
||||
|
||||
// if (out.charsMode()) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "charsMode", "()Z", false);
|
||||
Label charIf = new Label();
|
||||
mv.visitJumpInsn(IFEQ, charIf);
|
||||
// comma = out.writeFieldIntValue(fieldArray, comma, value.getAge());
|
||||
dynConvertToMethod(clazz, newDynName, mv, factory, mixedNames, elements, commaLabel, true);
|
||||
Label byteIf = new Label();
|
||||
mv.visitJumpInsn(GOTO, byteIf);
|
||||
mv.visitLabel(charIf);
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] {Opcodes.INTEGER}, 0, null);
|
||||
// comma = out.writeFieldIntValue(fieldArray, comma, value.getAge());
|
||||
dynConvertToMethod(clazz, newDynName, mv, factory, mixedNames, elements, commaLabel, false);
|
||||
mv.visitLabel(byteIf);
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
|
||||
|
||||
{ // out.writeTo('}');
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitIntInsn(BIPUSH, '}');
|
||||
@@ -576,6 +475,139 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
|
||||
}
|
||||
}
|
||||
|
||||
private static void dynConvertToMethod(
|
||||
final Class clazz,
|
||||
final String newDynName,
|
||||
final MethodVisitor mv,
|
||||
final JsonFactory factory,
|
||||
final Map<String, AccessibleObject> mixedNames,
|
||||
final List<AccessibleObject> elements,
|
||||
final Label commaLabel,
|
||||
final boolean charMode) {
|
||||
final String valtypeName = clazz.getName().replace('.', '/');
|
||||
final String writerName = JsonWriter.class.getName().replace('.', '/');
|
||||
final String encodeableDesc = org.redkale.asm.Type.getDescriptor(Encodeable.class);
|
||||
final String objectDesc = org.redkale.asm.Type.getDescriptor(Object.class);
|
||||
int elementIndex = -1;
|
||||
for (AccessibleObject element : elements) {
|
||||
elementIndex++;
|
||||
final String fieldName = factory.readConvertFieldName(clazz, element);
|
||||
final Class fieldType = readGetSetFieldType(element);
|
||||
mv.visitVarInsn(ALOAD, 1); // JsonWriter
|
||||
mv.visitVarInsn(ALOAD, 0); // this.xxxFieldBytes 第一个参数
|
||||
if (charMode) {
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "FieldChars", "[C");
|
||||
} else {
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "FieldBytes", "[B");
|
||||
}
|
||||
if (commaLabel != null) {
|
||||
mv.visitVarInsn(ILOAD, 3); // comma 第三个参数
|
||||
} else {
|
||||
mv.visitInsn(elementIndex == 0 ? ICONST_0 : ICONST_1); // comma=false 第二个参数
|
||||
}
|
||||
if (mixedNames.containsKey(fieldName)) { // Encodeable 第三个参数
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "Encoder", encodeableDesc);
|
||||
}
|
||||
mv.visitVarInsn(ALOAD, 2); // value.getXXX() 第三/四个参数
|
||||
if (element instanceof Field) {
|
||||
mv.visitFieldInsn(
|
||||
GETFIELD,
|
||||
valtypeName,
|
||||
((Field) element).getName(),
|
||||
org.redkale.asm.Type.getDescriptor(fieldType));
|
||||
} else {
|
||||
mv.visitMethodInsn(
|
||||
((Method) element).getDeclaringClass().isInterface() ? INVOKEINTERFACE : INVOKEVIRTUAL,
|
||||
valtypeName,
|
||||
((Method) element).getName(),
|
||||
"()" + org.redkale.asm.Type.getDescriptor(fieldType),
|
||||
false);
|
||||
}
|
||||
if (fieldType == boolean.class || fieldType == Boolean.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldBooleanValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == byte.class || fieldType == Byte.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldByteValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == short.class || fieldType == Short.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldShortValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == char.class || fieldType == Character.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldCharValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == int.class || fieldType == Integer.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldIntValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == float.class || fieldType == Float.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldFloatValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == long.class || fieldType == Long.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldLongValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == double.class || fieldType == Double.class) {
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldDoubleValue",
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else if (fieldType == String.class) {
|
||||
String writeFieldName = "writeFieldStringValue";
|
||||
if (isConvertStandardString(factory, element)) {
|
||||
writeFieldName = "writeFieldStandardStringValue";
|
||||
}
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
writeFieldName,
|
||||
"(" + objectDesc + "Z" + org.redkale.asm.Type.getDescriptor(fieldType) + ")Z",
|
||||
false);
|
||||
} else {
|
||||
// writeFieldObjectValue(fieldArray, comma, encodeable, value)
|
||||
mv.visitMethodInsn(
|
||||
INVOKEVIRTUAL,
|
||||
writerName,
|
||||
"writeFieldObjectValue",
|
||||
"(" + objectDesc + "Z" + encodeableDesc + objectDesc + ")Z",
|
||||
false);
|
||||
}
|
||||
if (commaLabel != null && elementIndex + 1 < elements.size()) {
|
||||
mv.visitVarInsn(ISTORE, 3); // comma = out.writeFieldXXXValue()
|
||||
} else {
|
||||
mv.visitInsn(POP);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static boolean isConvertStandardString(JsonFactory factory, AccessibleObject element) {
|
||||
if (element instanceof Field) {
|
||||
return ((Field) element).getAnnotation(ConvertStandardString.class) != null
|
||||
|
||||
@@ -76,125 +76,114 @@ public abstract class JsonWriter extends Writer {
|
||||
@ClassDepends
|
||||
public abstract void writeLatin1To(final boolean quote, final String value);
|
||||
|
||||
@ClassDepends
|
||||
public void writeField(final byte[] fieldBytes, final char[] fieldChars) {
|
||||
if (charsMode()) {
|
||||
writeTo(fieldChars, 0, fieldChars.length);
|
||||
} else {
|
||||
writeTo(fieldBytes, 0, fieldBytes.length);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------- writeFieldXXXValue 调用前不需要判断值是否为null ----------------------------
|
||||
@ClassDepends
|
||||
public abstract boolean writeFieldBooleanValue(byte[] fieldBytes, char[] fieldChars, boolean comma, boolean value);
|
||||
public abstract boolean writeFieldBooleanValue(Object fieldArray, boolean comma, boolean value);
|
||||
|
||||
@ClassDepends
|
||||
public abstract boolean writeFieldByteValue(byte[] fieldBytes, char[] fieldChars, boolean comma, byte value);
|
||||
public abstract boolean writeFieldByteValue(Object fieldArray, boolean comma, byte value);
|
||||
|
||||
@ClassDepends
|
||||
public abstract boolean writeFieldShortValue(byte[] fieldBytes, char[] fieldChars, boolean comma, short value);
|
||||
public abstract boolean writeFieldShortValue(Object fieldArray, boolean comma, short value);
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldCharValue(byte[] fieldBytes, char[] fieldChars, boolean comma, char value) {
|
||||
return writeFieldIntValue(fieldBytes, fieldChars, comma, value);
|
||||
public final boolean writeFieldCharValue(Object fieldArray, boolean comma, char value) {
|
||||
return writeFieldIntValue(fieldArray, comma, value);
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public abstract boolean writeFieldIntValue(byte[] fieldBytes, char[] fieldChars, boolean comma, int value);
|
||||
public abstract boolean writeFieldIntValue(Object fieldArray, boolean comma, int value);
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldFloatValue(byte[] fieldBytes, char[] fieldChars, boolean comma, float value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
public final boolean writeFieldFloatValue(Object fieldArray, boolean comma, float value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public abstract boolean writeFieldLongValue(byte[] fieldBytes, char[] fieldChars, boolean comma, long value);
|
||||
public abstract boolean writeFieldLongValue(Object fieldArray, boolean comma, long value);
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldDoubleValue(byte[] fieldBytes, char[] fieldChars, boolean comma, double value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
public final boolean writeFieldDoubleValue(Object fieldArray, boolean comma, double value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldBooleanValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Boolean value) {
|
||||
public final boolean writeFieldBooleanValue(Object fieldArray, boolean comma, Boolean value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldBooleanValue(fieldBytes, fieldChars, comma, value.booleanValue());
|
||||
return writeFieldBooleanValue(fieldArray, comma, value.booleanValue());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldByteValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Byte value) {
|
||||
public final boolean writeFieldByteValue(Object fieldArray, boolean comma, Byte value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldByteValue(fieldBytes, fieldChars, comma, value.byteValue());
|
||||
return writeFieldByteValue(fieldArray, comma, value.byteValue());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldShortValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Short value) {
|
||||
public final boolean writeFieldShortValue(Object fieldArray, boolean comma, Short value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldShortValue(fieldBytes, fieldChars, comma, value.shortValue());
|
||||
return writeFieldShortValue(fieldArray, comma, value.shortValue());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldCharValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Character value) {
|
||||
public final boolean writeFieldCharValue(Object fieldArray, boolean comma, Character value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldIntValue(fieldBytes, fieldChars, comma, value.charValue());
|
||||
return writeFieldIntValue(fieldArray, comma, value.charValue());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldIntValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Integer value) {
|
||||
public final boolean writeFieldIntValue(Object fieldArray, boolean comma, Integer value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldIntValue(fieldBytes, fieldChars, comma, value.intValue());
|
||||
return writeFieldIntValue(fieldArray, comma, value.intValue());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldFloatValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Float value) {
|
||||
public final boolean writeFieldFloatValue(Object fieldArray, boolean comma, Float value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldLongValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Long value) {
|
||||
public final boolean writeFieldLongValue(Object fieldArray, boolean comma, Long value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldLongValue(fieldBytes, fieldChars, comma, value.longValue());
|
||||
return writeFieldLongValue(fieldArray, comma, value.longValue());
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldDoubleValue(byte[] fieldBytes, char[] fieldChars, boolean comma, Double value) {
|
||||
public final boolean writeFieldDoubleValue(Object fieldArray, boolean comma, Double value) {
|
||||
if (value == null) {
|
||||
return comma;
|
||||
}
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, false, String.valueOf(value));
|
||||
return writeFieldLatin1Value(fieldArray, comma, false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final boolean writeFieldStandardStringValue(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, String value) {
|
||||
return writeFieldLatin1Value(fieldBytes, fieldChars, comma, true, value);
|
||||
public final boolean writeFieldStandardStringValue(Object fieldArray, boolean comma, String value) {
|
||||
return writeFieldLatin1Value(fieldArray, comma, true, value);
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public abstract boolean writeFieldStringValue(byte[] fieldBytes, char[] fieldChars, boolean comma, String value);
|
||||
public abstract boolean writeFieldStringValue(Object fieldArray, boolean comma, String value);
|
||||
|
||||
@ClassDepends
|
||||
public abstract boolean writeFieldObjectValue(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, Encodeable encodeable, Object value);
|
||||
Object fieldArray, boolean comma, Encodeable encodeable, Object value);
|
||||
|
||||
protected abstract boolean writeFieldLatin1Value(
|
||||
byte[] fieldBytes, char[] fieldChars, boolean comma, boolean quote, String value);
|
||||
protected abstract boolean writeFieldLatin1Value(Object fieldArray, boolean comma, boolean quote, String value);
|
||||
|
||||
// ---------------------------- writeFieldXXXValue 主要供JsonDynEncoder使用 ----------------------------
|
||||
|
||||
|
||||
@@ -31,7 +31,11 @@ public class _DyncFortuneJsonEncoder extends JsonDynEncoder<Fortune> {
|
||||
return;
|
||||
}
|
||||
out.writeTo('{');
|
||||
out.writeFieldIntValue(idFieldBytes, idFieldChars, false, value.getId());
|
||||
if (out.charsMode()) {
|
||||
out.writeFieldIntValue(idFieldChars, false, value.getId());
|
||||
} else {
|
||||
out.writeFieldIntValue(idFieldBytes, false, value.getId());
|
||||
}
|
||||
out.writeTo('}');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,11 @@ public class _DyncMessageJsonEncoder extends JsonDynEncoder<Message> {
|
||||
return;
|
||||
}
|
||||
out.writeTo('{');
|
||||
out.writeFieldStandardStringValue(messageFieldBytes, messageFieldChars, false, value.getMessage());
|
||||
if (out.charsMode()) {
|
||||
out.writeFieldStandardStringValue(messageFieldChars, false, value.getMessage());
|
||||
} else {
|
||||
out.writeFieldStandardStringValue(messageFieldBytes, false, value.getMessage());
|
||||
}
|
||||
out.writeTo('}');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,13 +46,23 @@ public class _DyncUserJsonEncoder extends JsonDynEncoder<User> {
|
||||
}
|
||||
out.writeTo('{');
|
||||
boolean comma = false;
|
||||
comma = out.writeFieldIntValue(ageFieldBytes, ageFieldChars, comma, value.getAge());
|
||||
comma = out.writeFieldObjectValue(
|
||||
createTimeFieldBytes, createTimeFieldChars, comma, this.createTimeEncoder, value.getCreateTime());
|
||||
comma = out.writeFieldLongValue(idFieldBytes, idFieldChars, comma, value.getId());
|
||||
comma = out.writeFieldStringValue(nameFieldBytes, nameFieldChars, comma, value.getName());
|
||||
comma = out.writeFieldStringValue(sexFieldBytes, sexFieldChars, comma, value.getSex());
|
||||
out.writeFieldStringValue(nickNameFieldBytes, nickNameFieldChars, comma, value.getNickName());
|
||||
if (out.charsMode()) {
|
||||
comma = out.writeFieldIntValue(ageFieldChars, comma, value.getAge());
|
||||
comma = out.writeFieldObjectValue(
|
||||
createTimeFieldChars, comma, this.createTimeEncoder, value.getCreateTime());
|
||||
comma = out.writeFieldLongValue(idFieldChars, comma, value.getId());
|
||||
comma = out.writeFieldStringValue(nameFieldChars, comma, value.getName());
|
||||
comma = out.writeFieldStringValue(sexFieldChars, comma, value.getSex());
|
||||
out.writeFieldStringValue(nickNameFieldChars, comma, value.getNickName());
|
||||
} else {
|
||||
comma = out.writeFieldIntValue(ageFieldBytes, comma, value.getAge());
|
||||
comma = out.writeFieldObjectValue(
|
||||
createTimeFieldBytes, comma, this.createTimeEncoder, value.getCreateTime());
|
||||
comma = out.writeFieldLongValue(idFieldBytes, comma, value.getId());
|
||||
comma = out.writeFieldStringValue(nameFieldBytes, comma, value.getName());
|
||||
comma = out.writeFieldStringValue(sexFieldBytes, comma, value.getSex());
|
||||
out.writeFieldStringValue(nickNameFieldBytes, comma, value.getNickName());
|
||||
}
|
||||
out.writeTo('}');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,13 @@ public class _DyncWorldJsonEncoder extends JsonDynEncoder<World> {
|
||||
|
||||
out.writeTo('{');
|
||||
boolean comma = false;
|
||||
comma = out.writeFieldIntValue(idFieldBytes, idFieldChars, comma, value.getId());
|
||||
out.writeFieldIntValue(randomNumberFieldBytes, randomNumberFieldChars, comma, value.getRandomNumber());
|
||||
if (out.charsMode()) {
|
||||
comma = out.writeFieldIntValue(idFieldChars, comma, value.getId());
|
||||
out.writeFieldIntValue(randomNumberFieldChars, comma, value.getRandomNumber());
|
||||
} else {
|
||||
comma = out.writeFieldIntValue(idFieldBytes, comma, value.getId());
|
||||
out.writeFieldIntValue(randomNumberFieldBytes, comma, value.getRandomNumber());
|
||||
}
|
||||
out.writeTo('}');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user