-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplBitSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplBitSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
core/src/test/java/com/alibaba/fastjson2/jsonb/BitSetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |