Skip to content

Commit f0c9e9e

Browse files
fix: handle edge case where a null array was provided
1 parent d457050 commit f0c9e9e

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group = 'sh.arnaud'
6-
version = '1.0.1-SNAPSHOT'
6+
version = '1.0.2-SNAPSHOT'
77

88
repositories {
99
mavenCentral()

src/main/java/sh/arnaud/javaserde/codec/Decoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ private GrammarNewEnum readNewEnum(ByteBuffer data) throws Exception {
116116
}
117117

118118
private GrammarNewArray readNewArray(ByteBuffer data) throws Exception {
119-
if (peekByte(data) == TC_REFERENCE) {
119+
var peeked = peekByte(data);
120+
121+
if (peeked == TC_NULL) {
122+
data.get();
123+
124+
return null;
125+
}
126+
127+
if (peeked == TC_REFERENCE) {
120128
data.get();
121129

122130
return handles.retrieve(data.getInt(), GrammarNewArray.class);

src/main/java/sh/arnaud/javaserde/codec/Encoder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void writeContent(GrammarContent content) throws Exception {
6464
return;
6565
}
6666

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

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

106106
private void writeNewArray(GrammarNewArray array) throws Exception {
107107
referenceable(array, () -> {
108+
if (array == null) {
109+
buffer.writeByte(TC_NULL);
110+
return null;
111+
}
112+
108113
buffer.writeByte(TC_ARRAY);
109114
writeClassDesc(array.classDesc);
110115

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.*;
2+
import java.util.Date;
3+
4+
public class Nullable implements Serializable {
5+
enum Enum {
6+
ONE,
7+
TWO
8+
}
9+
10+
private final String null_string = null;
11+
private final int[] null_ints = null;
12+
private final Enum null_enum = null;
13+
14+
public static void main(String args[]) throws Exception {
15+
var stream = new ObjectOutputStream(System.out);
16+
17+
stream.writeObject(new Nullable());
18+
stream.flush();
19+
}
20+
}
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"@handle": 8257540,
4+
"@class": {
5+
"@handle": 8257536,
6+
"@name": "Nullable",
7+
"@serial": 9613321951376902,
8+
"@flags": 2,
9+
"@fields": [
10+
{
11+
"@name": "null_enum",
12+
"@type": "LNullable$Enum;"
13+
},
14+
{
15+
"@name": "null_ints",
16+
"@type": "[I"
17+
},
18+
{
19+
"@name": "null_string",
20+
"@type": "Ljava/lang/String;"
21+
}
22+
],
23+
"@annotations": [],
24+
"@super": null
25+
},
26+
"@data": [
27+
{
28+
"@values": [
29+
null,
30+
null,
31+
null
32+
],
33+
"@annotations": []
34+
}
35+
]
36+
}
37+
]

0 commit comments

Comments
 (0)