Skip to content

Commit

Permalink
support BitSet, fix #265
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 16, 2022
1 parent b71baf8 commit 4b5f213
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BitSet> {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 24 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/jsonb/BitSetTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4b5f213

Please sign in to comment.