json优化

This commit is contained in:
redkale
2024-10-15 15:28:48 +08:00
parent 0d45e6c43b
commit 50994c3f78
6 changed files with 43 additions and 905 deletions

View File

@@ -21,4 +21,6 @@ import static java.lang.annotation.RetentionPolicy.*;
public @interface ClassDepends {
Class[] value() default {};
String comment() default "";
}

View File

@@ -231,7 +231,7 @@ public class JsonByteBufferWriter extends JsonWriter {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) (0xf0 | ((uc >> 18))));
buffer.put((byte) (0xf0 | (uc >> 18)));
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
@@ -249,7 +249,7 @@ public class JsonByteBufferWriter extends JsonWriter {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) (0xe0 | ((c >> 12))));
buffer.put((byte) (0xe0 | (c >> 12)));
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
@@ -476,287 +476,6 @@ public class JsonByteBufferWriter extends JsonWriter {
}
}
@Override
public void writeLastFieldShortValue(final byte[] fieldBytes, final char[] fieldChars, final short value) {
byte[] bs1 = fieldBytes;
byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value));
int expandsize = expand(bs1.length + bs2.length + 1);
if (expandsize == 0) { // 只需要一个buffer
final ByteBuffer buffer = this.buffers[currBufIndex];
buffer.put(bs1);
buffer.put(bs2);
buffer.put((byte) '}');
} else {
ByteBuffer buffer = this.buffers[currBufIndex];
for (byte b : bs1) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
for (byte b : bs2) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '}');
}
}
}
@Override
public void writeLastFieldIntValue(final byte[] fieldBytes, final char[] fieldChars, final int value) {
byte[] bs1 = fieldBytes;
byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value));
int expandsize = expand(bs1.length + bs2.length + 1);
if (expandsize == 0) { // 只需要一个buffer
final ByteBuffer buffer = this.buffers[currBufIndex];
buffer.put(bs1);
buffer.put(bs2);
buffer.put((byte) '}');
} else {
ByteBuffer buffer = this.buffers[currBufIndex];
for (byte b : bs1) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
for (byte b : bs2) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '}');
}
}
}
@Override
public void writeLastFieldLongValue(final byte[] fieldBytes, final char[] fieldChars, final long value) {
byte[] bs1 = fieldBytes;
byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value));
int expandsize = expand(bs1.length + bs2.length + 1);
if (expandsize == 0) { // 只需要一个buffer
final ByteBuffer buffer = this.buffers[currBufIndex];
buffer.put(bs1);
buffer.put(bs2);
buffer.put((byte) '}');
} else {
ByteBuffer buffer = this.buffers[currBufIndex];
for (byte b : bs1) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
for (byte b : bs2) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '}');
}
}
}
@Override
public void writeLastFieldLatin1Value(final byte[] fieldBytes, final char[] fieldChars, final String value) {
if (value == null && nullable()) {
writeTo(fieldBytes);
writeNull();
writeTo('}');
return;
}
if (value == null || (tiny() && value.isEmpty())) {
expand(1);
this.buffers[currBufIndex].put((byte) '}');
} else {
byte[] bs1 = fieldBytes;
byte[] bs2 = Utility.latin1ByteArray(value);
int expandsize = expand(bs1.length + bs2.length + 3);
if (expandsize == 0) { // 只需要一个buffer
final ByteBuffer buffer = this.buffers[currBufIndex];
buffer.put(bs1);
buffer.put((byte) '"');
buffer.put(bs2);
buffer.put((byte) '"');
buffer.put((byte) '}');
} else {
ByteBuffer buffer = this.buffers[currBufIndex];
for (byte b : bs1) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '"');
}
for (byte b : bs2) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '"');
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '}');
}
}
}
}
@Override // firstFieldBytes 必须带{开头
public void writeObjectByOnlyOneLatin1FieldValue(
final byte[] firstFieldBytes, final char[] firstFieldChars, final String value) {
if (value == null && nullable()) {
writeTo('{');
writeTo(firstFieldBytes);
writeNull();
writeTo('}');
return;
}
if (value == null || (tiny() && value.isEmpty())) {
int expandsize = expand(2);
if (expandsize == 0) { // 只需要一个buffer
ByteBuffer bb = this.buffers[currBufIndex];
bb.put((byte) '{');
bb.put((byte) '}');
} else {
ByteBuffer bb = this.buffers[currBufIndex];
bb.put((byte) '{');
bb = nextByteBuffer();
bb.put((byte) '}');
}
} else {
byte[] bs1 = firstFieldBytes;
byte[] bs2 = Utility.latin1ByteArray(value);
int expandsize = expand(bs1.length + bs2.length + 3);
if (expandsize == 0) { // 只需要一个buffer
final ByteBuffer buffer = this.buffers[currBufIndex];
buffer.put(bs1);
buffer.put((byte) '"');
buffer.put(bs2);
buffer.put((byte) '"');
buffer.put((byte) '}');
} else {
ByteBuffer buffer = this.buffers[currBufIndex];
for (byte b : bs1) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '"');
}
for (byte b : bs2) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '"');
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '}');
}
}
}
}
@Override // firstFieldBytes 必须带{开头, lastFieldBytes必须,开头
public void writeObjectByOnlyTwoIntFieldValue(
final byte[] firstFieldBytes,
final char[] firstFieldChars,
final int value1,
final byte[] lastFieldBytes,
final char[] lastFieldChars,
final int value2) {
byte[] bs1 = firstFieldBytes;
byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value1));
byte[] bs3 = lastFieldBytes;
byte[] bs4 = Utility.latin1ByteArray(String.valueOf(value2));
int expandsize = expand(bs1.length + bs2.length + bs3.length + bs4.length + 1);
if (expandsize == 0) { // 只需要一个buffer
final ByteBuffer buffer = this.buffers[currBufIndex];
buffer.put(bs1);
buffer.put(bs2);
buffer.put(bs3);
buffer.put(bs4);
buffer.put((byte) '}');
} else {
ByteBuffer buffer = this.buffers[currBufIndex];
for (byte b : bs1) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
for (byte b : bs2) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
for (byte b : bs3) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
for (byte b : bs4) {
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put(b);
}
{
if (!buffer.hasRemaining()) {
buffer = nextByteBuffer();
}
buffer.put((byte) '}');
}
}
}
@Override
public void writeBoolean(boolean value) {
writeTo(value ? CHARS_TUREVALUE : CHARS_FALSEVALUE);
@@ -774,11 +493,6 @@ public class JsonByteBufferWriter extends JsonWriter {
@Override
public void writeString(String value) {
writeString(true, value);
}
@Override
public void writeString(final boolean quote, String value) {
if (value == null) {
writeNull();
return;
@@ -808,7 +522,7 @@ public class JsonByteBufferWriter extends JsonWriter {
}
}
if (len == chs.length) {
writeTo(-1, quote, chs, 0, len);
writeTo(-1, true, chs, 0, len);
return;
}
int expandsize = -1;
@@ -817,9 +531,7 @@ public class JsonByteBufferWriter extends JsonWriter {
expandsize = expand(byteLength);
if (expandsize == 0) { // 只需要一个buffer
final ByteBuffer buffer = this.buffers[currBufIndex];
if (quote) {
buffer.put((byte) '"');
}
buffer.put((byte) '"');
for (int i = 0; i < chs.length; i++) {
char c = chs[i];
switch (c) {
@@ -859,9 +571,7 @@ public class JsonByteBufferWriter extends JsonWriter {
break;
}
}
if (quote) {
buffer.put((byte) '"');
}
buffer.put((byte) '"');
return;
}
}
@@ -889,7 +599,7 @@ public class JsonByteBufferWriter extends JsonWriter {
}
}
char[] cs = Utility.charArray(sb);
writeTo(expandsize, quote, cs, 0, sb.length());
writeTo(expandsize, true, cs, 0, sb.length());
}
@Override

View File

@@ -250,157 +250,6 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
src[count++] = BYTE_DQUOTE;
}
@Override
public void writeLastFieldShortValue(final byte[] fieldBytes, final char[] fieldChars, final short value) {
byte[] bs1 = fieldBytes;
byte[] bs2 = (value >= 0 && value < TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES[value]
: ((value < 0 && value > -TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES2[-value]
: Utility.latin1ByteArray(String.valueOf(value)));
int len1 = bs1.length;
int len2 = bs2.length;
byte[] src = expand(len1 + len2 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
src[count++] = BYTE_RBRACE;
}
@Override
public void writeLastFieldIntValue(final byte[] fieldBytes, final char[] fieldChars, final int value) {
byte[] bs1 = fieldBytes;
byte[] bs2 = (value >= 0 && value < TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES[value]
: ((value < 0 && value > -TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES2[-value]
: Utility.latin1ByteArray(String.valueOf(value)));
int len1 = bs1.length;
int len2 = bs2.length;
byte[] src = expand(len1 + len2 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
src[count++] = BYTE_RBRACE;
}
@Override
public void writeLastFieldLongValue(final byte[] fieldBytes, final char[] fieldChars, final long value) {
byte[] bs1 = fieldBytes;
byte[] bs2 = (value >= 0 && value < TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES[(int) value]
: ((value < 0 && value > -TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES2[(int) -value]
: Utility.latin1ByteArray(String.valueOf(value)));
int len1 = bs1.length;
int len2 = bs2.length;
byte[] src = expand(len1 + len2 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
src[count++] = BYTE_RBRACE;
}
@Override
public void writeLastFieldLatin1Value(final byte[] fieldBytes, final char[] fieldChars, final String value) {
if (value == null && nullable()) {
writeTo(fieldBytes);
writeNull();
writeTo(BYTE_RBRACE);
return;
}
if (value == null || (tiny() && value.isEmpty())) {
expand(1);
content[count++] = BYTE_RBRACE;
} else {
byte[] bs1 = fieldBytes;
byte[] bs2 = Utility.latin1ByteArray(value);
int len1 = bs1.length;
int len2 = bs2.length;
byte[] src = expand(len1 + len2 + 3);
int c = count;
System.arraycopy(bs1, 0, src, c, len1);
c += len1;
src[c++] = BYTE_DQUOTE;
System.arraycopy(bs2, 0, src, c, len2);
c += len2;
src[c++] = BYTE_DQUOTE;
src[c++] = BYTE_RBRACE;
count = c;
}
}
@Override // firstFieldBytes 必须带{开头
public void writeObjectByOnlyOneLatin1FieldValue(
final byte[] firstFieldBytes, final char[] firstFieldChars, final String value) {
if (value == null && nullable()) {
writeTo(BYTE_LBRACE);
writeTo(firstFieldBytes);
writeNull();
writeTo(BYTE_RBRACE);
return;
}
if (value == null || (tiny() && value.isEmpty())) {
expand(2);
content[count++] = BYTE_LBRACE;
content[count++] = BYTE_RBRACE;
} else {
byte[] bs1 = firstFieldBytes;
byte[] bs2 = Utility.latin1ByteArray(value);
int len1 = bs1.length;
int len2 = bs2.length;
byte[] src = expand(len1 + len2 + 3);
int c = count;
System.arraycopy(bs1, 0, src, c, len1);
c += len1;
src[c++] = BYTE_DQUOTE;
System.arraycopy(bs2, 0, src, c, len2);
c += len2;
src[c++] = BYTE_DQUOTE;
src[c++] = BYTE_RBRACE;
count = c;
}
}
@Override // firstFieldBytes 必须带{开头, lastFieldBytes必须,开头
public void writeObjectByOnlyTwoIntFieldValue(
final byte[] firstFieldBytes,
final char[] firstFieldChars,
final int value1,
final byte[] lastFieldBytes,
final char[] lastFieldChars,
final int value2) {
byte[] bs1 = firstFieldBytes;
byte[] bs2 = (value1 >= 0 && value1 < TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES[value1]
: ((value1 < 0 && value1 > -TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES2[-value1]
: Utility.latin1ByteArray(String.valueOf(value1)));
byte[] bs3 = lastFieldBytes;
byte[] bs4 = (value2 >= 0 && value2 < TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES[value2]
: ((value2 < 0 && value2 > -TENTHOUSAND_MAX)
? TENTHOUSAND_BYTES2[-value2]
: Utility.latin1ByteArray(String.valueOf(value2)));
int len1 = bs1.length;
int len2 = bs2.length;
int len3 = bs3.length;
int len4 = bs4.length;
byte[] src = expand(len1 + len2 + len3 + len4 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
System.arraycopy(bs3, 0, src, count, len3);
count += len3;
System.arraycopy(bs4, 0, src, count, len4);
count += len4;
src[count++] = BYTE_RBRACE;
}
public JsonBytesWriter clear() {
this.count = 0;
return this;
@@ -427,30 +276,23 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
@Override
public void writeString(String value) {
writeString(true, value);
}
@Override
public void writeString(final boolean quote, String value) {
if (value == null) {
writeNull();
return;
}
if (Utility.isLatin1(value)) {
writeEscapeLatinString(quote, Utility.latin1ByteArray(value));
writeEscapeLatinString(Utility.latin1ByteArray(value));
return;
}
byte[] utf16s = Utility.utf16ByteArray(value);
if (utf16s != null) { // JDK9+
writeEscapeUTF16String(quote, utf16s);
writeEscapeUTF16String(utf16s);
return;
}
int len = value.length();
byte[] bytes = expand(len * 4 + 2);
int curr = count;
if (quote) {
bytes[curr++] = BYTE_DQUOTE;
}
bytes[curr++] = BYTE_DQUOTE;
for (int i = 0; i < len; i++) {
char ch = value.charAt(i);
if (ch < 14) {
@@ -499,20 +341,16 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
bytes[curr++] = (byte) (0x80 | (ch & 0x3f));
}
}
if (quote) {
bytes[curr++] = BYTE_DQUOTE;
}
bytes[curr++] = BYTE_DQUOTE;
count = curr;
}
// see java.lang.StringCoding.encodeUTF8_UTF16 方法
private void writeEscapeUTF16String(final boolean quote, byte[] value) {
private void writeEscapeUTF16String(byte[] value) {
int len = value.length;
byte[] bytes = expand(len * 4 + 2);
int curr = count;
if (quote) {
bytes[curr++] = BYTE_DQUOTE;
}
bytes[curr++] = BYTE_DQUOTE;
byte[] src = value;
int i = 0;
while (i < len) {
@@ -578,9 +416,7 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
}
}
}
if (quote) {
bytes[curr++] = BYTE_DQUOTE;
}
bytes[curr++] = BYTE_DQUOTE;
count = curr;
}
@@ -594,12 +430,10 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
private static final int MIN_SUPPLEMENTARY_CODE_POINT_MORE =
(MIN_SUPPLEMENTARY_CODE_POINT - (MIN_HIGH_SURROGATE << 10) - MIN_LOW_SURROGATE);
private void writeEscapeLatinString(final boolean quote, byte[] value) {
private void writeEscapeLatinString(byte[] value) {
byte[] bytes = expand(value.length * 2 + 2);
int curr = count;
if (quote) {
bytes[curr++] = BYTE_DQUOTE;
}
bytes[curr++] = BYTE_DQUOTE;
for (byte b : value) {
if (b == BYTE_DQUOTE) {
bytes[curr++] = '\\';
@@ -630,9 +464,7 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
bytes[curr++] = b;
}
}
if (quote) {
bytes[curr++] = BYTE_DQUOTE;
}
bytes[curr++] = BYTE_DQUOTE;
count = curr;
}

View File

@@ -5,8 +5,6 @@
package org.redkale.convert.json;
import static org.redkale.convert.json.JsonWriter.BYTE_DQUOTE;
import static org.redkale.convert.json.JsonWriter.BYTE_LBRACE;
import static org.redkale.convert.json.JsonWriter.BYTE_RBRACE;
import static org.redkale.convert.json.JsonWriter.DEFAULT_SIZE;
import static org.redkale.convert.json.JsonWriter.DigitOnes;
import static org.redkale.convert.json.JsonWriter.DigitTens;
@@ -206,149 +204,6 @@ public class JsonCharsWriter extends JsonWriter {
src[count++] = BYTE_DQUOTE;
}
@Override
public void writeLastFieldShortValue(final byte[] fieldBytes, final char[] fieldChars, final short value) {
char[] bs1 = fieldChars;
char[] bs2 = (value >= 0 && value < TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS[value]
: ((value < 0 && value > -TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS2[-value]
: String.valueOf(value).toCharArray());
int len1 = bs1.length;
int len2 = bs2.length;
char[] src = expand(len1 + len2 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
src[count++] = BYTE_RBRACE;
}
@Override
public void writeLastFieldIntValue(final byte[] fieldBytes, final char[] fieldChars, final int value) {
char[] bs1 = fieldChars;
char[] bs2 = (value >= 0 && value < TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS[value]
: ((value < 0 && value > -TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS2[-value]
: String.valueOf(value).toCharArray());
int len1 = bs1.length;
int len2 = bs2.length;
char[] src = expand(len1 + len2 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
src[count++] = BYTE_RBRACE;
}
@Override
public void writeLastFieldLongValue(final byte[] fieldBytes, final char[] fieldChars, final long value) {
char[] bs1 = fieldChars;
char[] bs2 = (value >= 0 && value < TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS[(int) value]
: ((value < 0 && value > -TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS2[(int) -value]
: String.valueOf(value).toCharArray());
int len1 = bs1.length;
int len2 = bs2.length;
char[] src = expand(len1 + len2 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
src[count++] = BYTE_RBRACE;
}
@Override
public void writeLastFieldLatin1Value(final byte[] fieldBytes, final char[] fieldChars, final String value) {
if (value == null && nullable()) {
writeTo(fieldChars);
writeNull();
writeTo(BYTE_RBRACE);
return;
}
if (value == null || (tiny() && value.isEmpty())) {
expand(1);
content[count++] = BYTE_RBRACE;
} else {
int len1 = fieldChars.length;
int len2 = value.length();
char[] src = expand(len1 + len2 + 3);
System.arraycopy(fieldChars, 0, src, count, len1);
count += len1;
content[count++] = BYTE_DQUOTE;
value.getChars(0, len2, content, count);
count += len2;
content[count++] = BYTE_DQUOTE;
content[count++] = BYTE_RBRACE;
}
}
@Override // firstFieldBytes 必须带{开头
public void writeObjectByOnlyOneLatin1FieldValue(
byte[] firstFieldBytes, char[] firstFieldChars, final String value) {
if (value == null && nullable()) {
writeTo(BYTE_LBRACE);
writeTo(firstFieldChars);
writeNull();
writeTo(BYTE_RBRACE);
return;
}
if (value == null || (tiny() && value.isEmpty())) {
expand(2);
content[count++] = BYTE_LBRACE;
content[count++] = BYTE_RBRACE;
} else {
int len1 = firstFieldChars.length;
int len2 = value.length();
char[] src = expand(len1 + len2 + 3);
System.arraycopy(firstFieldChars, 0, src, count, len1);
count += len1;
content[count++] = BYTE_DQUOTE;
value.getChars(0, len2, content, count);
count += len2;
content[count++] = BYTE_DQUOTE;
content[count++] = BYTE_RBRACE;
}
}
@Override // firstFieldBytes 必须带{开头, lastFieldBytes必须,开头
public void writeObjectByOnlyTwoIntFieldValue(
final byte[] firstFieldBytes,
final char[] firstFieldChars,
final int value1,
final byte[] lastFieldBytes,
final char[] lastFieldChars,
final int value2) {
char[] bs1 = firstFieldChars;
char[] bs2 = (value1 >= 0 && value1 < TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS[value1]
: ((value1 < 0 && value1 > -TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS2[-value1]
: String.valueOf(value1).toCharArray());
char[] bs3 = lastFieldChars;
char[] bs4 = (value2 >= 0 && value2 < TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS[value2]
: ((value2 < 0 && value2 > -TENTHOUSAND_MAX)
? TENTHOUSAND_CHARS2[-value2]
: String.valueOf(value2).toCharArray());
int len1 = bs1.length;
int len2 = bs2.length;
int len3 = bs3.length;
int len4 = bs4.length;
char[] src = expand(len1 + len2 + len3 + len4 + 1);
System.arraycopy(bs1, 0, src, count, len1);
count += len1;
System.arraycopy(bs2, 0, src, count, len2);
count += len2;
System.arraycopy(bs3, 0, src, count, len3);
count += len3;
System.arraycopy(bs4, 0, src, count, len4);
count += len4;
src[count++] = BYTE_RBRACE;
}
public byte[] toBytes() {
return Utility.encodeUTF8(content, 0, count);
}
@@ -359,33 +214,26 @@ public class JsonCharsWriter extends JsonWriter {
@Override
public void writeString(String value) {
writeString(true, value);
}
@Override
public void writeString(final boolean quote, String value) {
if (value == null) {
writeNull();
return;
}
final String str = value;
if (Utility.isLatin1(str)) {
writeEscapeLatinString(quote, Utility.latin1ByteArray(str));
writeEscapeLatinString(Utility.latin1ByteArray(str));
return;
}
if (false) {
byte[] utf16s = Utility.utf16ByteArray(value);
if (utf16s != null) { // JDK9+
writeEscapeUTF16String(quote, utf16s);
writeEscapeUTF16String(utf16s);
return;
}
}
final int len = str.length();
char[] chars = expand(len * 2 + 2);
int curr = count;
if (quote) {
chars[curr++] = BYTE_DQUOTE;
}
chars[curr++] = BYTE_DQUOTE;
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch < 14) {
@@ -421,20 +269,16 @@ public class JsonCharsWriter extends JsonWriter {
chars[curr++] = ch;
}
}
if (quote) {
chars[curr++] = BYTE_DQUOTE;
}
chars[curr++] = BYTE_DQUOTE;
count = curr;
}
private void writeEscapeUTF16String(final boolean quote, byte[] value) {
private void writeEscapeUTF16String(byte[] value) {
byte[] bytes = value;
int len = bytes.length;
char[] chars = expand(len + 2);
int curr = count;
if (quote) {
chars[curr++] = BYTE_DQUOTE;
}
chars[curr++] = BYTE_DQUOTE;
byte b1, b2;
for (int i = 0; i < len; i += 2) {
b1 = bytes[i];
@@ -473,18 +317,14 @@ public class JsonCharsWriter extends JsonWriter {
chars[curr++] = ch;
}
}
if (quote) {
chars[curr++] = BYTE_DQUOTE;
}
chars[curr++] = BYTE_DQUOTE;
count = curr;
}
private void writeEscapeLatinString(final boolean quote, byte[] value) {
private void writeEscapeLatinString(byte[] value) {
char[] chars = expand(value.length * 2 + 2);
int curr = count;
if (quote) {
chars[curr++] = BYTE_DQUOTE;
}
chars[curr++] = BYTE_DQUOTE;
for (byte b : value) {
if (b == BYTE_DQUOTE) {
chars[curr++] = '\\';
@@ -515,9 +355,7 @@ public class JsonCharsWriter extends JsonWriter {
chars[curr++] = (char) b;
}
}
if (quote) {
chars[curr++] = BYTE_DQUOTE;
}
chars[curr++] = BYTE_DQUOTE;
count = curr;
}

View File

@@ -117,50 +117,22 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
null);
final int membersSize = elements.size();
boolean onlyTwoIntFieldObjectFlag = false; // 只包含两个int字段
boolean onlyOneLatin1FieldObjectFlag = false; // 只包含一个Latin1 String字段
boolean onlyShotIntLongLatin1MoreFieldObjectFlag = true;
int intFieldCount = 0;
for (AccessibleObject element : elements) {
final String fieldName = factory.readConvertFieldName(clazz, element);
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldName + "FieldBytes", "[B", null, null);
fv.visitEnd();
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldName + "CommaFieldBytes", "[B", null, null);
fv.visitEnd();
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldName + "FirstFieldBytes", "[B", null, null);
fv.visitEnd();
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldName + "FieldChars", "[C", null, null);
fv.visitEnd();
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldName + "CommaFieldChars", "[C", null, null);
fv.visitEnd();
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldName + "FirstFieldChars", "[C", null, null);
fv.visitEnd();
final Class fieldType = readGetSetFieldType(element);
if (fieldType != String.class && !fieldType.isPrimitive()) {
fv = cw.visitField(ACC_PROTECTED, fieldName + "Encoder", encodeableDesc, null, null);
fv.visitEnd();
}
if (fieldType == int.class) {
intFieldCount++;
}
if (fieldType == String.class && membersSize == 1 && isConvertStandardString(factory, element)) {
onlyOneLatin1FieldObjectFlag = true;
} else if (fieldType != short.class
&& fieldType != int.class
&& fieldType != long.class
&& !(fieldType == String.class && isConvertStandardString(factory, element))) {
onlyShotIntLongLatin1MoreFieldObjectFlag = false;
}
}
if (intFieldCount == 2 && intFieldCount == membersSize) {
onlyTwoIntFieldObjectFlag = true;
}
if (onlyShotIntLongLatin1MoreFieldObjectFlag && membersSize < 2) {
onlyShotIntLongLatin1MoreFieldObjectFlag = false; // 字段个数必须大于1
}
onlyTwoIntFieldObjectFlag = false; // 废弃此特性
onlyOneLatin1FieldObjectFlag = false; // 废弃此特性
onlyShotIntLongLatin1MoreFieldObjectFlag = false; // 废弃此特性
{ // 构造函数
mv = (cw.visitMethod(
ACC_PUBLIC, "<init>", "(" + jsonfactoryDesc + typeDesc + objEncoderDesc + ")V", null, null));
@@ -190,11 +162,6 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
mv.visitLdcInsn(",\"" + fieldName + "\":");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "getBytes", "()[B", false);
mv.visitFieldInsn(PUTFIELD, newDynName, fieldName + "CommaFieldBytes", "[B");
// xxxFirstFieldBytes
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn("{\"" + fieldName + "\":");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "getBytes", "()[B", false);
mv.visitFieldInsn(PUTFIELD, newDynName, fieldName + "FirstFieldBytes", "[B");
// xxxFieldChars
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn("\"" + fieldName + "\":");
@@ -205,11 +172,6 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
mv.visitLdcInsn(",\"" + fieldName + "\":");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "toCharArray", "()[C", false);
mv.visitFieldInsn(PUTFIELD, newDynName, fieldName + "CommaFieldChars", "[C");
// xxxFirstFieldChars
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn("{\"" + fieldName + "\":");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "toCharArray", "()[C", false);
mv.visitFieldInsn(PUTFIELD, newDynName, fieldName + "FirstFieldChars", "[C");
}
mv.visitInsn(RETURN);
Label label2 = new Label();
@@ -264,176 +226,9 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
final boolean tiny = ConvertFactory.checkTinyFeature(factory.getFeatures());
final boolean nullable = ConvertFactory.checkNullableFeature(factory.getFeatures());
final Class firstType = readGetSetFieldType(elements.get(0));
final boolean mustHadComma = firstType.isPrimitive()
&& (firstType != boolean.class || !tiny || nullable); // byte/short/char/int/float/long/double
if (onlyOneLatin1FieldObjectFlag) {
// out.writeObjectByOnlyOneLatin1FieldValue(messageFirstFieldBytes, value.getMessage());elementIndex++;
elementIndex++;
AccessibleObject element = elements.get(elementIndex);
final String fieldName = factory.readConvertFieldName(clazz, element);
final Class fieldType = readGetSetFieldType(element);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "FirstFieldBytes", "[B");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldName + "FirstFieldChars", "[C");
mv.visitVarInsn(ALOAD, 2); // String message = value.getMessage(); 加载 value
if (element instanceof Field) {
mv.visitFieldInsn(
GETFIELD,
valtypeName,
((Field) element).getName(),
org.redkale.asm.Type.getDescriptor(fieldType));
} else {
mv.visitMethodInsn(
INVOKEVIRTUAL,
valtypeName,
((Method) element).getName(),
"()" + org.redkale.asm.Type.getDescriptor(fieldType),
false);
}
mv.visitMethodInsn(
INVOKEVIRTUAL,
writerName,
"writeObjectByOnlyOneLatin1FieldValue",
"([B[CLjava/lang/String;)V",
false);
maxLocals++;
} else if (onlyTwoIntFieldObjectFlag) {
elementIndex++;
AccessibleObject element1 = elements.get(elementIndex);
final String fieldName1 = factory.readConvertFieldName(clazz, element1);
final Class fieldType1 = readGetSetFieldType(element1);
elementIndex++;
AccessibleObject element2 = elements.get(elementIndex);
final String fieldName2 = factory.readConvertFieldName(clazz, element2);
final Class fieldtype2 = readGetSetFieldType(element2);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldName1 + "FirstFieldBytes", "[B");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldName1 + "FirstFieldChars", "[C");
mv.visitVarInsn(ALOAD, 2); // String message = value.getMessage(); 加载 value
if (element1 instanceof Field) {
mv.visitFieldInsn(
GETFIELD,
valtypeName,
((Field) element1).getName(),
org.redkale.asm.Type.getDescriptor(fieldType1));
} else {
mv.visitMethodInsn(
INVOKEVIRTUAL,
valtypeName,
((Method) element1).getName(),
"()" + org.redkale.asm.Type.getDescriptor(fieldType1),
false);
}
maxLocals++;
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldName2 + "CommaFieldBytes", "[B");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldName2 + "CommaFieldChars", "[C");
mv.visitVarInsn(ALOAD, 2); // String message = value.getMessage(); 加载 value
if (element2 instanceof Field) {
mv.visitFieldInsn(
GETFIELD,
valtypeName,
((Field) element2).getName(),
org.redkale.asm.Type.getDescriptor(fieldtype2));
} else {
mv.visitMethodInsn(
INVOKEVIRTUAL,
valtypeName,
((Method) element2).getName(),
"()" + org.redkale.asm.Type.getDescriptor(fieldtype2),
false);
}
maxLocals++;
mv.visitMethodInsn(
INVOKEVIRTUAL, writerName, "writeObjectByOnlyTwoIntFieldValue", "([B[CI[B[CI)V", false);
} else if (onlyShotIntLongLatin1MoreFieldObjectFlag && mustHadComma) {
for (AccessibleObject element : elements) {
elementIndex++;
final String fieldName = factory.readConvertFieldName(clazz, element);
final Class fieldType = readGetSetFieldType(element);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(
GETFIELD,
newDynName,
fieldName + (elementIndex == 0 ? "FirstFieldBytes" : "CommaFieldBytes"),
"[B");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(
GETFIELD,
newDynName,
fieldName + (elementIndex == 0 ? "FirstFieldChars" : "CommaFieldChars"),
"[C");
mv.visitVarInsn(ALOAD, 2); // String message = value.getMessage(); 加载 value
if (element instanceof Field) {
mv.visitFieldInsn(
GETFIELD,
valtypeName,
((Field) element).getName(),
org.redkale.asm.Type.getDescriptor(fieldType));
} else {
mv.visitMethodInsn(
INVOKEVIRTUAL,
valtypeName,
((Method) element).getName(),
"()" + org.redkale.asm.Type.getDescriptor(fieldType),
false);
}
if (fieldType == short.class) {
mv.visitMethodInsn(
INVOKEVIRTUAL,
writerName,
elementIndex + 1 == membersSize ? "writeLastFieldShortValue" : "writeFieldShortValue",
"([B[CS)V",
false);
} else if (fieldType == int.class) {
mv.visitMethodInsn(
INVOKEVIRTUAL,
writerName,
elementIndex + 1 == membersSize ? "writeLastFieldIntValue" : "writeFieldIntValue",
"([B[CI)V",
false);
} else if (fieldType == long.class) {
mv.visitMethodInsn(
INVOKEVIRTUAL,
writerName,
elementIndex + 1 == membersSize ? "writeLastFieldLongValue" : "writeFieldLongValue",
"([B[CJ)V",
false);
} else {
mv.visitMethodInsn(
INVOKEVIRTUAL,
writerName,
elementIndex + 1 == membersSize ? "writeLastFieldLatin1Value" : "writeFieldLatin1Value",
"([B[CLjava/lang/String;)V",
false);
}
if (fieldType == long.class || fieldType == double.class) {
maxLocals += 2;
} else {
maxLocals++;
}
}
} else {
final boolean mustHadComma = (firstType.isPrimitive() && (firstType != boolean.class || !tiny || nullable))
|| membersSize == 1; // byte/short/char/int/float/long/double
{
{ // out.writeTo('{');
mv.visitVarInsn(ALOAD, 1);
mv.visitIntInsn(BIPUSH, '{');
@@ -634,10 +429,9 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
INVOKEVIRTUAL, writerName, "writeString", "(Ljava/lang/String;)V", false);
} else {
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ICONST_1);
mv.visitVarInsn(loadid, maxLocals);
mv.visitMethodInsn(
INVOKEVIRTUAL, writerName, "writeLatin1To", "(ZLjava/lang/String;)V", false);
INVOKEVIRTUAL, writerName, "writeStandardString", "(Ljava/lang/String;)V", false);
}
} else { // int[],Boolean[],String[]
mv.visitVarInsn(ALOAD, 0);

View File

@@ -83,51 +83,14 @@ public abstract class JsonWriter extends Writer {
}
}
@ClassDepends
public abstract void writeFieldShortValue(final byte[] fieldBytes, final char[] fieldChars, final short value);
@ClassDepends
public abstract void writeFieldIntValue(final byte[] fieldBytes, final char[] fieldChars, final int value);
@ClassDepends
public abstract void writeFieldLongValue(final byte[] fieldBytes, final char[] fieldChars, final long value);
@ClassDepends
public abstract void writeFieldLatin1Value(final byte[] fieldBytes, final char[] fieldChars, final String value);
@ClassDepends
@Deprecated(since = "2.8.0")
public abstract void writeLastFieldShortValue(final byte[] fieldBytes, final char[] fieldChars, final short value);
@ClassDepends
@Deprecated(since = "2.8.0")
public abstract void writeLastFieldIntValue(final byte[] fieldBytes, final char[] fieldChars, final int value);
@ClassDepends
@Deprecated(since = "2.8.0")
public abstract void writeLastFieldLongValue(final byte[] fieldBytes, final char[] fieldChars, final long value);
@ClassDepends
@Deprecated(since = "2.8.0")
public abstract void writeLastFieldLatin1Value(final byte[] fieldBytes, final char[] fieldChars, String value);
// firstFieldBytes 必须带{开头
@ClassDepends
@Deprecated(since = "2.8.0")
public abstract void writeObjectByOnlyOneLatin1FieldValue(
final byte[] firstFieldBytes, final char[] firstFieldChars, final String value);
// firstFieldBytes 必须带{开头, lastFieldBytes必须,开头
@ClassDepends
@Deprecated(since = "2.8.0")
public abstract void writeObjectByOnlyTwoIntFieldValue(
final byte[] firstFieldBytes,
final char[] firstFieldChars,
final int value1,
final byte[] lastFieldBytes,
final char[] lastFieldChars,
final int value2);
@Override
@ClassDepends
public abstract void writeBoolean(boolean value);
@@ -140,12 +103,16 @@ public abstract class JsonWriter extends Writer {
@ClassDepends
public abstract void writeLong(long value);
public abstract void writeString(final boolean quote, String value);
@Override
@ClassDepends
public abstract void writeString(String value);
@Override
@ClassDepends
public final void writeStandardString(String value) {
writeLatin1To(true, value);
}
@Override // 只容许JsonBytesWriter重写此方法
public void writeField(EnMember member, String fieldName, Type fieldType, int fieldPos) {
if (this.comma) {
@@ -164,21 +131,11 @@ public abstract class JsonWriter extends Writer {
}
}
@Override
public final void writeStandardString(String value) {
writeLatin1To(true, value);
}
// ----------------------------------------------------------------------------------------------
public final void writeTo(final char... chs) { // 只能是 0 - 127 的字符
writeTo(chs, 0, chs.length);
}
@Override
public final void writeByte(byte value) {
writeInt(value);
}
public final void writeTo(final byte[] chs) { // 只能是 0 - 127 的字符
writeTo(chs, 0, chs.length);
}
@@ -201,6 +158,11 @@ public abstract class JsonWriter extends Writer {
writeArrayE();
}
@Override
public final void writeByte(byte value) {
writeInt(value);
}
@Override
public final void writeChar(char value) {
writeInt(value);