in core/src/main/java/com/alibaba/fastjson2/JSONBDump.java [57:491]
private void dumpAny() {
if (offset >= bytes.length) {
return;
}
type = bytes[offset++];
switch (type) {
case BC_NULL:
jsonWriter.writeNull();
return;
case BC_TRUE:
jsonWriter.writeBool(true);
return;
case BC_FALSE:
jsonWriter.writeBool(false);
return;
case BC_DOUBLE_NUM_0:
jsonWriter.writeDouble(0);
return;
case BC_DOUBLE_NUM_1:
jsonWriter.writeDouble(1);
return;
case BC_INT8:
jsonWriter.writeInt8(bytes[offset++]);
return;
case BC_INT16:
jsonWriter.writeInt16(
(short) (
(bytes[offset++] << 8)
+ (bytes[offset++] & 0xFF)
)
);
return;
case BC_TIMESTAMP_MINUTES:
case BC_TIMESTAMP_SECONDS:
case BC_INT32: {
int int32Value =
((bytes[offset + 3] & 0xFF)) +
((bytes[offset + 2] & 0xFF) << 8) +
((bytes[offset + 1] & 0xFF) << 16) +
((bytes[offset]) << 24);
offset += 4;
jsonWriter.writeInt32(int32Value);
return;
}
case BC_INT64_INT: {
int int32Value =
((bytes[offset + 3] & 0xFF)) +
((bytes[offset + 2] & 0xFF) << 8) +
((bytes[offset + 1] & 0xFF) << 16) +
((bytes[offset]) << 24);
offset += 4;
jsonWriter.writeInt64(int32Value);
return;
}
case BC_TIMESTAMP_MILLIS:
case BC_INT64: {
long int64Value =
((bytes[offset + 7] & 0xFFL)) +
((bytes[offset + 6] & 0xFFL) << 8) +
((bytes[offset + 5] & 0xFFL) << 16) +
((bytes[offset + 4] & 0xFFL) << 24) +
((bytes[offset + 3] & 0xFFL) << 32) +
((bytes[offset + 2] & 0xFFL) << 40) +
((bytes[offset + 1] & 0xFFL) << 48) +
((long) (bytes[offset]) << 56);
offset += 8;
jsonWriter.writeInt64(int64Value);
return;
}
case BC_BIGINT: {
int len = readInt32Value();
byte[] bytes = new byte[len];
System.arraycopy(this.bytes, offset, bytes, 0, len);
offset += len;
jsonWriter.writeBigInt(
new BigInteger(bytes));
return;
}
case BC_BIGINT_LONG: {
jsonWriter.writeInt64(
readInt64Value()
);
return;
}
case BC_FLOAT: {
int int32Value =
((bytes[offset + 3] & 0xFF)) +
((bytes[offset + 2] & 0xFF) << 8) +
((bytes[offset + 1] & 0xFF) << 16) +
((bytes[offset]) << 24);
offset += 4;
jsonWriter.writeFloat(
Float.intBitsToFloat(int32Value));
return;
}
case BC_FLOAT_INT: {
jsonWriter.writeFloat(
readInt32Value()
);
return;
}
case BC_DOUBLE: {
long int64Value =
((bytes[offset + 7] & 0xFFL)) +
((bytes[offset + 6] & 0xFFL) << 8) +
((bytes[offset + 5] & 0xFFL) << 16) +
((bytes[offset + 4] & 0xFFL) << 24) +
((bytes[offset + 3] & 0xFFL) << 32) +
((bytes[offset + 2] & 0xFFL) << 40) +
((bytes[offset + 1] & 0xFFL) << 48) +
((long) (bytes[offset]) << 56);
offset += 8;
jsonWriter.writeDouble(
Double.longBitsToDouble(int64Value));
return;
}
case BC_DOUBLE_LONG: {
jsonWriter.writeDouble(
readInt64Value()
);
return;
}
case BC_STR_UTF8: {
int strlen = readLength();
String str = new String(bytes, offset, strlen, StandardCharsets.UTF_8);
offset += strlen;
jsonWriter.writeString(str);
return;
}
case BC_CHAR: {
int intValue = readInt32Value();
jsonWriter.writeChar((char) intValue);
return;
}
case BC_STR_UTF16: {
int strlen = readLength();
String str = new String(bytes, offset, strlen, StandardCharsets.UTF_16);
offset += strlen;
jsonWriter.writeString(str);
return;
}
case BC_STR_UTF16LE: {
int strlen = readLength();
String str;
if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) {
byte[] chars = new byte[strlen];
System.arraycopy(bytes, offset, chars, 0, strlen);
str = STRING_CREATOR_JDK11.apply(chars, UTF16);
} else {
str = new String(bytes, offset, strlen, StandardCharsets.UTF_16LE);
}
offset += strlen;
jsonWriter.writeString(str);
return;
}
case BC_STR_UTF16BE: {
int strlen = readLength();
String str;
if (STRING_CREATOR_JDK11 != null && JDKUtils.BIG_ENDIAN) {
byte[] chars = new byte[strlen];
System.arraycopy(bytes, offset, chars, 0, strlen);
str = STRING_CREATOR_JDK11.apply(chars, UTF16);
} else {
str = new String(bytes, offset, strlen, StandardCharsets.UTF_16BE);
}
offset += strlen;
jsonWriter.writeString(str);
return;
}
case BC_STR_GB18030: {
if (GB18030 == null) {
GB18030 = Charset.forName("GB18030");
}
int strlen = readLength();
String str = new String(bytes, offset, strlen, GB18030);
offset += strlen;
jsonWriter.writeString(str);
return;
}
case BC_SYMBOL: {
int symbol;
if (isInt()) {
symbol = readInt32Value();
if (raw) {
jsonWriter.writeString("#" + symbol);
} else {
String name = getString(symbol);
jsonWriter.writeString(name);
}
} else {
String name = readString();
symbol = readInt32Value();
symbols.put(symbol, name);
if (raw) {
jsonWriter.writeString(name + "#" + symbol);
} else {
jsonWriter.writeString(name);
}
}
return;
}
case BC_DECIMAL: {
int scale = readInt32Value();
BigInteger unscaledValue;
int type = bytes[offset++];
switch (type) {
case BC_BIGINT_LONG:
unscaledValue = BigInteger.valueOf(
readInt64Value()
);
break;
case BC_INT32:
unscaledValue = BigInteger.valueOf(
readInt32Value()
);
break;
case BC_INT64:
unscaledValue = BigInteger.valueOf(
IOUtils.getLongBE(bytes, offset)
);
offset += 8;
break;
default:
if (type >= BC_INT32_NUM_MIN && type <= BC_INT32_NUM_MAX) {
unscaledValue = BigInteger.valueOf(type);
} else if (type >= BC_INT32_BYTE_MIN && type <= BC_INT32_BYTE_MAX) {
unscaledValue = BigInteger.valueOf(((type - BC_INT32_BYTE_ZERO) << 8)
+ (bytes[offset++] & 0xFF));
} else if (type >= BC_INT32_SHORT_MIN && type <= BC_INT32_SHORT_MAX) {
unscaledValue = BigInteger.valueOf(((type - BC_INT32_SHORT_ZERO) << 16)
+ ((bytes[offset++] & 0xFF) << 8)
+ (bytes[offset++] & 0xFF));
} else {
int len = readInt32Value();
byte[] bytes = new byte[len];
System.arraycopy(this.bytes, offset, bytes, 0, len);
offset += len;
unscaledValue = new BigInteger(bytes);
}
break;
}
BigDecimal decimal;
if (scale == 0) {
decimal = new BigDecimal(unscaledValue);
} else {
decimal = new BigDecimal(unscaledValue, scale);
}
jsonWriter.writeDecimal(decimal, 0, null);
return;
}
case BC_DECIMAL_LONG: {
jsonWriter.writeDecimal(
BigDecimal.valueOf(
readInt64Value()
),
0,
null
);
return;
}
case BC_TYPED_ANY: {
boolean isInt = isInt();
int symbol;
String typeName = null;
if (isInt) {
symbol = readInt32Value();
} else {
typeName = readString();
symbol = readInt32Value();
symbols.put(symbol, typeName);
}
if (!raw && bytes[offset] == BC_OBJECT) {
if (typeName == null) {
typeName = getString(symbol);
}
offset++;
dumpObject(typeName);
return;
}
jsonWriter.startObject();
jsonWriter.writeName("@type");
jsonWriter.writeColon();
if (typeName == null) {
if (symbol < 0) {
if (raw) {
jsonWriter.writeString("#" + symbol);
} else {
String name = symbolTable.getName(-symbol);
jsonWriter.writeString(name);
}
} else {
jsonWriter.writeString("#" + symbol);
}
} else {
if (raw) {
jsonWriter.writeString(typeName + "#" + symbol);
} else {
jsonWriter.writeString(typeName);
}
}
jsonWriter.writeName("@value");
jsonWriter.writeColon();
dumpAny();
jsonWriter.endObject();
return;
}
case BC_BINARY: {
int len = readInt32Value();
byte[] bytes = new byte[len];
System.arraycopy(this.bytes, offset, bytes, 0, len);
offset += len;
jsonWriter.writeBinary(bytes);
return;
}
case BC_REFERENCE: {
dumpReference();
break;
}
case BC_LOCAL_DATETIME: {
int year = (bytes[offset++] << 8) + (bytes[offset++] & 0xFF);
int month = bytes[offset++];
int dayOfMonth = bytes[offset++];
int hour = bytes[offset++];
int minute = bytes[offset++];
int second = bytes[offset++];
int nano = readInt32Value();
LocalDateTime localDateTime = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nano);
jsonWriter.writeLocalDateTime(localDateTime);
break;
}
case BC_LOCAL_DATE: {
int year = (bytes[offset++] << 8) + (bytes[offset++] & 0xFF);
int month = bytes[offset++];
int dayOfMonth = bytes[offset++];
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
jsonWriter.writeLocalDate(localDate);
break;
}
case BC_TIMESTAMP:
long epochSeconds = readInt64Value();
int nano = readInt32Value();
jsonWriter.writeInstant(
Instant.ofEpochSecond(epochSeconds, nano)
);
break;
case BC_OBJECT: {
dumpObject(null);
break;
}
default:
if (type >= BC_INT32_NUM_MIN && type <= BC_INT32_NUM_MAX) {
jsonWriter.writeInt32(type);
return;
}
if (type >= BC_INT64_NUM_MIN && type <= BC_INT64_NUM_MAX) {
long value = INT64_NUM_LOW_VALUE + (type - BC_INT64_NUM_MIN);
jsonWriter.writeInt64(value);
return;
}
if (type >= BC_INT32_BYTE_MIN && type <= BC_INT32_BYTE_MAX) {
int value = ((type - BC_INT32_BYTE_ZERO) << 8)
+ (bytes[offset++] & 0xFF);
jsonWriter.writeInt32(value);
return;
}
if (type >= BC_INT32_SHORT_MIN && type <= BC_INT32_SHORT_MAX) {
int value = ((type - BC_INT32_SHORT_ZERO) << 16)
+ ((bytes[offset++] & 0xFF) << 8)
+ (bytes[offset++] & 0xFF);
jsonWriter.writeInt32(value);
return;
}
if (type >= BC_INT64_BYTE_MIN && type <= BC_INT64_BYTE_MAX) {
int value = ((type - BC_INT64_BYTE_ZERO) << 8)
+ (bytes[offset++] & 0xFF);
jsonWriter.writeInt32(value);
return;
}
if (type >= BC_INT64_SHORT_MIN && type <= BC_INT64_SHORT_MAX) {
int value = ((type - BC_INT64_SHORT_ZERO) << 16)
+ ((bytes[offset++] & 0xFF) << 8)
+ (bytes[offset++] & 0xFF);
jsonWriter.writeInt64(value);
return;
}
if (type >= BC_ARRAY_FIX_MIN && type <= BC_ARRAY) {
dumpArray();
return;
}
if (type >= BC_STR_ASCII_FIX_MIN) {
strlen = type == BC_STR_ASCII
? readLength()
: type - BC_STR_ASCII_FIX_MIN;
if (strlen < 0) {
jsonWriter.writeRaw("{\"$symbol\":");
jsonWriter.writeInt32(strlen);
jsonWriter.writeRaw("}");
return;
}
String str = new String(bytes, offset, strlen, StandardCharsets.ISO_8859_1);
offset += strlen;
jsonWriter.writeString(str);
return;
}
throw new JSONException("not support type : " + typeName(type) + ", offset " + offset);
}
}