@@ -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