Skip to content

Commit

Permalink
fix: handle edge case where a null array was provided
Browse files Browse the repository at this point in the history
  • Loading branch information
vaadata-pascala committed Jan 24, 2024
1 parent d457050 commit f0c9e9e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'sh.arnaud'
version = '1.0.1-SNAPSHOT'
version = '1.0.2-SNAPSHOT'

repositories {
mavenCentral()
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/sh/arnaud/javaserde/codec/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ private GrammarNewEnum readNewEnum(ByteBuffer data) throws Exception {
}

private GrammarNewArray readNewArray(ByteBuffer data) throws Exception {
if (peekByte(data) == TC_REFERENCE) {
var peeked = peekByte(data);

if (peeked == TC_NULL) {
data.get();

return null;
}

if (peeked == TC_REFERENCE) {
data.get();

return handles.retrieve(data.getInt(), GrammarNewArray.class);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/sh/arnaud/javaserde/codec/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void writeContent(GrammarContent content) throws Exception {
return;
}

throw new UnsupportedOperationException("Serialization of content not implemented yet!");
throw new UnsupportedOperationException("Serialization of content not implemented yet: ".concat(content.getClass().getName()));
}

private void referenceable(GrammarObject object, Callable<Void> runnable) throws Exception {
Expand Down Expand Up @@ -105,6 +105,11 @@ private void writeNewEnum(GrammarNewEnum newEnum) throws Exception {

private void writeNewArray(GrammarNewArray array) throws Exception {
referenceable(array, () -> {
if (array == null) {
buffer.writeByte(TC_NULL);
return null;
}

buffer.writeByte(TC_ARRAY);
writeClassDesc(array.classDesc);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.io.*;
import java.util.Date;

public class Nullable implements Serializable {
enum Enum {
ONE,
TWO
}

private final String null_string = null;
private final int[] null_ints = null;
private final Enum null_enum = null;

public static void main(String args[]) throws Exception {
var stream = new ObjectOutputStream(System.out);

stream.writeObject(new Nullable());
stream.flush();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"@handle": 8257540,
"@class": {
"@handle": 8257536,
"@name": "Nullable",
"@serial": 9613321951376902,
"@flags": 2,
"@fields": [
{
"@name": "null_enum",
"@type": "LNullable$Enum;"
},
{
"@name": "null_ints",
"@type": "[I"
},
{
"@name": "null_string",
"@type": "Ljava/lang/String;"
}
],
"@annotations": [],
"@super": null
},
"@data": [
{
"@values": [
null,
null,
null
],
"@annotations": []
}
]
}
]

0 comments on commit f0c9e9e

Please sign in to comment.