Skip to content

Commit a9a89b4

Browse files
committed
Packer.write() accepts null
1 parent 56473be commit a9a89b4

5 files changed

Lines changed: 31 additions & 54 deletions

File tree

src/main/java/org/msgpack/packer/AbstractPacker.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ public void writeMapEnd() throws IOException {
4444
}
4545

4646
public Packer write(Object o) throws IOException {
47+
if(o == null) {
48+
writeNil();
49+
return this;
50+
}
4751
Template tmpl = msgpack.lookup(o.getClass());
4852
tmpl.write(this, o);
4953
return this;
5054
}
5155

52-
public Packer writeOptional(Object o) throws IOException {
53-
if(o == null) {
56+
public Packer write(Value v) throws IOException {
57+
if(v == null) {
5458
writeNil();
5559
return this;
5660
}
57-
return write(o);
58-
}
59-
60-
public Packer write(Value v) throws IOException {
6161
v.writeTo(this);
6262
return this;
6363
}

src/main/java/org/msgpack/packer/Packer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ public interface Packer extends Closeable {
6767

6868
public Packer write(Object o) throws IOException;
6969

70-
public Packer writeOptional(Object o) throws IOException;
71-
7270
public Packer write(Value v) throws IOException;
7371
}
7472

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,6 @@ public <T> T read(T to) throws IOException {
7878
return (T) tmpl.read(this, to);
7979
}
8080

81-
public <T> T readOptional(Class<T> klass) throws IOException {
82-
return readOptional(klass, null);
83-
}
84-
85-
public <T> T readOptional(Class<T> klass, T defaultValue) throws IOException {
86-
if(trySkipNil()) {
87-
return defaultValue;
88-
}
89-
Template<? super T> tmpl = (Template<? super T>) msgpack.lookup(klass);
90-
return (T) tmpl.read(this, null);
91-
}
92-
93-
public <T> T readOptional(T to, T defaultValue) throws IOException {
94-
if(trySkipNil()) {
95-
return defaultValue;
96-
}
97-
Template<? super T> tmpl = msgpack.lookup((Class<T>) to.getClass());
98-
return (T) tmpl.read(this, to);
99-
}
100-
101-
public <T> T readOptional(T to) throws IOException {
102-
return readOptional(to, null);
103-
}
104-
10581

10682
public void close() throws IOException {
10783
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,5 @@ public interface Unpacker extends Iterable<Value>, Closeable {
8484
public <T> T read(Class<T> klass) throws IOException;
8585

8686
public <T> T read(T to) throws IOException;
87-
88-
public <T> T readOptional(Class<T> klass) throws IOException;
89-
90-
public <T> T readOptional(Class<T> klass, T defaultValue) throws IOException;
91-
92-
public <T> T readOptional(T to, T defaultValue) throws IOException;
93-
94-
public <T> T readOptional(T to) throws IOException;
9587
}
9688

src/test/java/org/msgpack/TestSimplePackable.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,41 @@ public void testSample01() throws IOException {
9494
assertEquals(a.f2, b.f2);
9595
}
9696

97-
// some files are NULLABLE or OPTIONAL
97+
// some files are OPTIONAL or NULLABLE
9898
public static class Sample02 implements MessagePackable {
9999
public String f0; // nullable
100-
public Long f1; // optional
101-
public Integer f2; // nullable
100+
public long f1; // primitive
101+
public Integer f2; // required
102102
public String f3; // optional
103103

104104
public Sample02() { }
105105

106106
public void writeTo(Packer pk) throws IOException {
107107
pk.writeArrayBegin(4);
108-
pk.writeOptional(f0);
109-
pk.writeOptional(f1);
110-
pk.writeOptional(f2);
111-
pk.writeOptional(f3);
108+
pk.write(f0);
109+
pk.writeLong(f1);
110+
if(f2 == null) {
111+
throw new MessageTypeException("f2 is required but null");
112+
}
113+
pk.write(f2);
114+
pk.write(f3);
112115
pk.writeArrayEnd();
113116
}
114117

115118
public void readFrom(Unpacker u) throws IOException {
116119
u.readArrayBegin();
117-
f0 = u.readOptional(String.class);
118-
f1 = u.readOptional(Long.class);
119-
f2 = u.readOptional(Integer.class);
120-
f3 = u.readOptional(String.class);
120+
f0 = u.read(String.class);
121+
f1 = u.readLong();
122+
if(u.trySkipNil()) {
123+
f2 = null;
124+
} else {
125+
f2 = u.read(Integer.class);
126+
}
127+
if(u.trySkipNil()) {
128+
f3 = null;
129+
} else {
130+
f3 = u.read(String.class);
131+
}
121132
u.readArrayEnd();
122133
}
123134
}
@@ -128,9 +139,9 @@ public void testSample02() throws IOException {
128139

129140
Sample02 a = new Sample02();
130141
a.f0 = "aaa";
131-
a.f1 = null;
132-
a.f2 = null;
133-
a.f3 = "bbb";
142+
a.f1 = 1;
143+
a.f2 = 22;
144+
a.f3 = null;
134145

135146
BufferPacker pk = msgpack.createBufferPacker();
136147
a.writeTo(pk);

0 commit comments

Comments
 (0)