diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java index f36a0cb8e6..7bdce6eda9 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java @@ -1013,6 +1013,10 @@ public ObjectReader getObjectReader(ObjectReaderProvider provider, Type type) { return NumberImpl.INSTANCE; } + if (type == BitSet.class) { + return ObjectReaderImplBitSet.INSTANCE; + } + if (type == OptionalInt.class) { return ObjectReaderImplOptionalInt.INSTANCE; } diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplBitSet.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplBitSet.java new file mode 100644 index 0000000000..8509f4bf8d --- /dev/null +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplBitSet.java @@ -0,0 +1,46 @@ +package com.alibaba.fastjson2.reader; + +import com.alibaba.fastjson2.JSONB; +import com.alibaba.fastjson2.JSONException; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.util.Fnv; + +import java.util.BitSet; + +public final class ObjectReaderImplBitSet extends ObjectReaderBaseModule.PrimitiveImpl { + static final ObjectReaderImplBitSet INSTANCE = new ObjectReaderImplBitSet(); + + public static final long HASH_TYPE = Fnv.hashCode64("BitSet"); + + @Override + public Class getObjectClass() { + return BitSet.class; + } + + @Override + public BitSet readJSONBObject(JSONReader jsonReader, long features) { + if (jsonReader.nextIfNull()) { + return null; + } + if (jsonReader.nextIfMatch(JSONB.Constants.BC_TYPED_ANY)) { + long typeHash = jsonReader.readTypeHashCode(); + if (typeHash != HASH_TYPE) { + String typeName = jsonReader.getString(); + throw new JSONException(typeName); + } + } + + byte[] bytes = jsonReader.readBinary(); + return BitSet.valueOf(bytes); + } + + @Override + public BitSet readObject(JSONReader jsonReader, long features) { + if (jsonReader.nextIfNull()) { + return null; + } + + byte[] bytes = jsonReader.readBinary(); + return BitSet.valueOf(bytes); + } +} diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java index 74e1f668ca..ac4db44d5b 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java @@ -783,6 +783,10 @@ public ObjectWriter getObjectWriter(Type objectType, Class objectClass) { return ObjectWriterImplBigDecimal.INSTANCE; } + if (objectType == BitSet.class) { + return ObjectWriterImplBitSet.INSTANCE; + } + if (objectType == OptionalInt.class) { return ObjectWriterImplOptionalInt.INSTANCE; } diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplBitSet.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplBitSet.java new file mode 100644 index 0000000000..6a4fd282fc --- /dev/null +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplBitSet.java @@ -0,0 +1,24 @@ +package com.alibaba.fastjson2.writer; + +import com.alibaba.fastjson2.JSONWriter; + +import java.lang.reflect.Type; +import java.util.BitSet; +import java.util.Locale; + +final class ObjectWriterImplBitSet extends ObjectWriterBaseModule.PrimitiveImpl { + static final ObjectWriterImplBitSet INSTANCE = new ObjectWriterImplBitSet(); + + @Override + public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { + if (object == null) { + jsonWriter.writeNull(); + return; + } + + BitSet bitSet = (BitSet) object; + + byte[] bytes = bitSet.toByteArray(); + jsonWriter.writeBinary(bytes); + } +} diff --git a/core/src/test/java/com/alibaba/fastjson2/jsonb/BitSetTest.java b/core/src/test/java/com/alibaba/fastjson2/jsonb/BitSetTest.java new file mode 100644 index 0000000000..3a0aa8090c --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/jsonb/BitSetTest.java @@ -0,0 +1,24 @@ +package com.alibaba.fastjson2.jsonb; + +import com.alibaba.fastjson2.JSONB; +import org.junit.jupiter.api.Test; + +import java.util.BitSet; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BitSetTest { + @Test + public void test0() { + BitSet bitSet1 = new BitSet(); + bitSet1.set(1); + bitSet1.set(5); + bitSet1.set(100); + + byte[] jsonbBytes = JSONB.toBytes(bitSet1); + + BitSet bitSet2 = JSONB.parseObject(jsonbBytes, BitSet.class); + + assertEquals(bitSet1, bitSet2); + } +}