Skip to content

Commit e4e3f4c

Browse files
committed
Unpacker#read returns null if nil
1 parent 75763c7 commit e4e3f4c

3 files changed

Lines changed: 37 additions & 25 deletions

File tree

src/main/java/org/msgpack/unpacker/AbstractUnpacker.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ public Value readValue() throws IOException {
6868
}
6969

7070

71+
protected abstract boolean tryReadNil() throws IOException;
72+
7173
@Override
7274
public <T> T read(Class<T> klass) throws IOException {
75+
if(tryReadNil()) {
76+
return null;
77+
}
7378
Template<? super T> tmpl = msgpack.lookup(klass);
7479
return (T) tmpl.read(this, null);
7580
}
7681

7782
@Override
7883
public <T> T read(T to) throws IOException {
84+
if(tryReadNil()) {
85+
return null;
86+
}
7987
Template<? super T> tmpl = msgpack.lookup((Class<T>) to.getClass());
8088
return (T) tmpl.read(this, to);
8189
}

src/main/java/org/msgpack/unpacker/Converter.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ private void ensureValue() throws IOException {
5656
}
5757
}
5858

59-
//@Override
60-
//public boolean tryReadNil() throws IOException {
61-
// stack.checkCount();
62-
// if(getTop().isNil()) {
63-
// stack.reduceCount();
64-
// if(stack.getDepth() == 0) {
65-
// value = null;
66-
// }
67-
// return true;
68-
// }
69-
// return false;
70-
//}
59+
@Override
60+
public boolean tryReadNil() throws IOException {
61+
stack.checkCount();
62+
if(getTop().isNil()) {
63+
stack.reduceCount();
64+
if(stack.getDepth() == 0) {
65+
value = null;
66+
}
67+
return true;
68+
}
69+
return false;
70+
}
7171

7272
@Override
7373
public boolean trySkipNil() throws IOException {
@@ -403,5 +403,9 @@ public void reset() {
403403
stack.clear();
404404
value = null;
405405
}
406+
407+
@Override
408+
public void close() throws IOException {
409+
}
406410
}
407411

src/main/java/org/msgpack/unpacker/MessagePackUnpacker.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,19 @@ private void readRawBodyCont() throws IOException {
319319
}
320320
}
321321

322-
//@Override
323-
//public boolean tryReadNil() throws IOException {
324-
// stack.checkCount();
325-
// int b = getHeadByte() & 0xff;
326-
// if(b == 0xc0) {
327-
// // nil is read
328-
// stack.reduceCount();
329-
// headByte = REQUIRE_TO_READ_HEAD;
330-
// return true;
331-
// }
332-
// // not nil
333-
// return false;
334-
//}
322+
@Override
323+
protected boolean tryReadNil() throws IOException {
324+
stack.checkCount();
325+
int b = getHeadByte() & 0xff;
326+
if(b == 0xc0) {
327+
// nil is read
328+
stack.reduceCount();
329+
headByte = REQUIRE_TO_READ_HEAD;
330+
return true;
331+
}
332+
// not nil
333+
return false;
334+
}
335335

336336
@Override
337337
public boolean trySkipNil() throws IOException {

0 commit comments

Comments
 (0)