This commit is contained in:
@@ -5,7 +5,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.convert.json;
|
package org.redkale.convert.json;
|
||||||
|
|
||||||
import java.util.function.*;
|
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import static org.redkale.convert.Reader.*;
|
import static org.redkale.convert.Reader.*;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
@@ -26,19 +25,7 @@ public class JsonReader extends Reader {
|
|||||||
private int limit;
|
private int limit;
|
||||||
|
|
||||||
public static ObjectPool<JsonReader> createPool(int max) {
|
public static ObjectPool<JsonReader> createPool(int max) {
|
||||||
return new ObjectPool<JsonReader>(max, new Creator<JsonReader>() { //为了兼容 JDK 6
|
return new ObjectPool<>(max, (Object... params) -> new JsonReader(), null, JsonReader::recycle);
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonReader create(Object... params) {
|
|
||||||
return new JsonReader();
|
|
||||||
}
|
|
||||||
}, null, new Predicate<JsonReader>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean test(JsonReader t) {
|
|
||||||
return t.recycle();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonReader() {
|
public JsonReader() {
|
||||||
@@ -107,20 +94,25 @@ public class JsonReader extends Reader {
|
|||||||
@Override
|
@Override
|
||||||
public final void skipValue() {
|
public final void skipValue() {
|
||||||
final char ch = nextGoodChar();
|
final char ch = nextGoodChar();
|
||||||
if (ch == '"' || ch == '\'') {
|
switch (ch) {
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
backChar(ch);
|
backChar(ch);
|
||||||
readString();
|
readString();
|
||||||
} else if (ch == '{') {
|
break;
|
||||||
|
case '{':
|
||||||
while (hasNext()) {
|
while (hasNext()) {
|
||||||
this.readSmallString(); //读掉field
|
this.readSmallString(); //读掉field
|
||||||
this.readBlank();
|
this.readBlank();
|
||||||
this.skipValue();
|
this.skipValue();
|
||||||
}
|
}
|
||||||
} else if (ch == '[') {
|
break;
|
||||||
|
case '[':
|
||||||
while (hasNext()) {
|
while (hasNext()) {
|
||||||
this.skipValue();
|
this.skipValue();
|
||||||
}
|
}
|
||||||
} else {
|
break;
|
||||||
|
default:
|
||||||
char c;
|
char c;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = nextChar();
|
c = nextChar();
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
package org.redkale.convert.json;
|
package org.redkale.convert.json;
|
||||||
|
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
import java.util.function.*;
|
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -34,19 +33,7 @@ public class JsonWriter extends Writer {
|
|||||||
protected boolean tiny;
|
protected boolean tiny;
|
||||||
|
|
||||||
public static ObjectPool<JsonWriter> createPool(int max) {
|
public static ObjectPool<JsonWriter> createPool(int max) {
|
||||||
return new ObjectPool<JsonWriter>(max, new Creator<JsonWriter>() {
|
return new ObjectPool<>(max, (Object... params) -> new JsonWriter(), null, (JsonWriter t) -> t.recycle());
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonWriter create(Object... params) {
|
|
||||||
return new JsonWriter();
|
|
||||||
}
|
|
||||||
}, null, new Predicate<JsonWriter>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean test(JsonWriter t) {
|
|
||||||
return t.recycle();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonWriter() {
|
public JsonWriter() {
|
||||||
@@ -202,12 +189,97 @@ public class JsonWriter extends Writer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeInt(int value) {
|
public final void writeInt(int value) {
|
||||||
writeTo(false, String.valueOf(value));
|
final char sign = value > 0 ? 0 : '-';
|
||||||
|
if (value < 0) value = -value;
|
||||||
|
int size;
|
||||||
|
for (int i = 0;; i++) {
|
||||||
|
if (value <= sizeTable[i]) {
|
||||||
|
size = i + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sign != 0) size++; //负数
|
||||||
|
expand(size);
|
||||||
|
|
||||||
|
int q, r;
|
||||||
|
int charPos = count + size;
|
||||||
|
|
||||||
|
// Generate two digits per iteration
|
||||||
|
while (value >= 65536) {
|
||||||
|
q = value / 100;
|
||||||
|
// really: r = i - (q * 100);
|
||||||
|
r = value - ((q << 6) + (q << 5) + (q << 2));
|
||||||
|
value = q;
|
||||||
|
content[--charPos] = DigitOnes[r];
|
||||||
|
content[--charPos] = DigitTens[r];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall thru to fast mode for smaller numbers
|
||||||
|
// assert(i <= 65536, i);
|
||||||
|
for (;;) {
|
||||||
|
q = (value * 52429) >>> (16 + 3);
|
||||||
|
r = value - ((q << 3) + (q << 1)); // r = i-(q*10) ...
|
||||||
|
content[--charPos] = digits[r];
|
||||||
|
value = q;
|
||||||
|
if (value == 0) break;
|
||||||
|
}
|
||||||
|
if (sign != 0) content[--charPos] = sign;
|
||||||
|
count += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeLong(long value) {
|
public final void writeLong(long value) {
|
||||||
writeTo(false, String.valueOf(value));
|
final char sign = value > 0 ? 0 : '-';
|
||||||
|
if (value < 0) value = -value;
|
||||||
|
int size = 19;
|
||||||
|
long p = 10;
|
||||||
|
for (int i = 1; i < 19; i++) {
|
||||||
|
if (value < p) {
|
||||||
|
size = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
p = 10 * p;
|
||||||
|
}
|
||||||
|
if (sign != 0) size++; //负数
|
||||||
|
expand(size);
|
||||||
|
|
||||||
|
long q;
|
||||||
|
int r;
|
||||||
|
int charPos = count + size;
|
||||||
|
|
||||||
|
// Get 2 digits/iteration using longs until quotient fits into an int
|
||||||
|
while (value > Integer.MAX_VALUE) {
|
||||||
|
q = value / 100;
|
||||||
|
// really: r = i - (q * 100);
|
||||||
|
r = (int) (value - ((q << 6) + (q << 5) + (q << 2)));
|
||||||
|
value = q;
|
||||||
|
content[--charPos] = DigitOnes[r];
|
||||||
|
content[--charPos] = DigitTens[r];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get 2 digits/iteration using ints
|
||||||
|
int q2;
|
||||||
|
int i2 = (int) value;
|
||||||
|
while (i2 >= 65536) {
|
||||||
|
q2 = i2 / 100;
|
||||||
|
// really: r = i2 - (q * 100);
|
||||||
|
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
|
||||||
|
i2 = q2;
|
||||||
|
content[--charPos] = DigitOnes[r];
|
||||||
|
content[--charPos] = DigitTens[r];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall thru to fast mode for smaller numbers
|
||||||
|
// assert(i2 <= 65536, i2);
|
||||||
|
for (;;) {
|
||||||
|
q2 = (i2 * 52429) >>> (16 + 3);
|
||||||
|
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
|
||||||
|
content[--charPos] = digits[r];
|
||||||
|
i2 = q2;
|
||||||
|
if (i2 == 0) break;
|
||||||
|
}
|
||||||
|
if (sign != 0) content[--charPos] = sign;
|
||||||
|
count += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -269,4 +341,41 @@ public class JsonWriter extends Writer {
|
|||||||
public final void writeMapE() {
|
public final void writeMapE() {
|
||||||
writeTo('}');
|
writeTo('}');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final static char[] DigitTens = {
|
||||||
|
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
|
||||||
|
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
|
||||||
|
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
|
||||||
|
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
|
||||||
|
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
|
||||||
|
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
|
||||||
|
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
|
||||||
|
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
|
||||||
|
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
|
||||||
|
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9'
|
||||||
|
};
|
||||||
|
|
||||||
|
final static char[] DigitOnes = {
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
|
||||||
|
};
|
||||||
|
|
||||||
|
final static char[] digits = {
|
||||||
|
'0', '1', '2', '3', '4', '5',
|
||||||
|
'6', '7', '8', '9', 'a', 'b',
|
||||||
|
'c', 'd', 'e', 'f', 'g', 'h',
|
||||||
|
'i', 'j', 'k', 'l', 'm', 'n',
|
||||||
|
'o', 'p', 'q', 'r', 's', 't',
|
||||||
|
'u', 'v', 'w', 'x', 'y', 'z'
|
||||||
|
};
|
||||||
|
|
||||||
|
final static int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user