Skip to content

Commit

Permalink
fix dump jsonb decimal
Browse files Browse the repository at this point in the history
wenshao committed Sep 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 45b54ec commit 093cd5c
Showing 2 changed files with 52 additions and 8 deletions.
40 changes: 32 additions & 8 deletions core/src/main/java/com/alibaba/fastjson2/JSONBDump.java
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@

import static com.alibaba.fastjson2.JSONB.Constants.*;
import static com.alibaba.fastjson2.JSONB.typeName;
import static com.alibaba.fastjson2.util.JDKUtils.STRING_CREATOR_JDK11;
import static com.alibaba.fastjson2.util.JDKUtils.UTF16;
import static com.alibaba.fastjson2.util.JDKUtils.*;
import static com.alibaba.fastjson2.util.JDKUtils.BIG_ENDIAN;

final class JSONBDump {
static Charset GB18030;
@@ -267,19 +267,43 @@ private void dumpAny() {
case BC_DECIMAL: {
int scale = readInt32Value();
BigInteger unscaledValue;
int type = bytes[offset++]; // bigInt
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:
long unscaledValueLong = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
unscaledValue = BigInteger.valueOf(
BIG_ENDIAN
? unscaledValueLong
: Long.reverseBytes(unscaledValueLong));
offset += 8;
break;
default:
int len = readInt32Value();
byte[] bytes = new byte[len];
System.arraycopy(this.bytes, offset, bytes, 0, len);
offset += len;
unscaledValue = new BigInteger(bytes);
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;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.alibaba.fastjson2.issues_2900;

import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2954 {
@Test
public void testB() {
BigDecimal value = new BigDecimal("20.0");
byte[] bytes = JSONB.toBytes(value);
JSONReader jsonReader = JSONReader.ofJSONB(bytes);
assertEquals(value, jsonReader.readBigDecimal());
assertEquals(value.toPlainString(), JSONB.toJSONString(bytes));
}
}

0 comments on commit 093cd5c

Please sign in to comment.