File tree Expand file tree Collapse file tree 3 files changed +36
-3
lines changed
main/java/org/msgpack/jackson/dataformat
test/java/org/msgpack/jackson/dataformat Expand file tree Collapse file tree 3 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 3232import org .msgpack .core .MessageFormat ;
3333import org .msgpack .core .MessagePack ;
3434import org .msgpack .core .MessageUnpacker ;
35- import org .msgpack .core .Preconditions ;
3635import org .msgpack .core .buffer .ArrayBufferInput ;
3736import org .msgpack .core .buffer .InputStreamBufferInput ;
3837import org .msgpack .core .buffer .MessageBufferInput ;
@@ -419,8 +418,14 @@ public int getTextOffset()
419418 public byte [] getBinaryValue (Base64Variant b64variant )
420419 throws IOException , JsonParseException
421420 {
422- Preconditions .checkArgument (type == Type .BYTES );
423- return bytesValue ;
421+ switch (type ) {
422+ case BYTES :
423+ return bytesValue ;
424+ case STRING :
425+ return stringValue .getBytes (MessagePack .UTF8 );
426+ default :
427+ throw new IllegalStateException ("Invalid type=" + type );
428+ }
424429 }
425430
426431 @ Override
Original file line number Diff line number Diff line change @@ -160,6 +160,12 @@ public void setS(String s)
160160 }
161161 }
162162
163+ public static class BinKeyPojo
164+ {
165+ public byte [] b ;
166+ public String s ;
167+ }
168+
163169 public static class UsingCustomConstructorPojo
164170 {
165171 final String name ;
Original file line number Diff line number Diff line change @@ -758,4 +758,26 @@ public Object deserialize(byte[] data)
758758 }
759759 assertThat ((String ) values .get (4 ), is ("Java" ));
760760 }
761+
762+ @ Test
763+ public void parserShouldReadStrAsBin ()
764+ throws IOException
765+ {
766+ MessagePacker packer = MessagePack .newDefaultPacker (out );
767+ packer .packMapHeader (2 );
768+ // #1
769+ packer .packString ("s" );
770+ packer .packString ("foo" );
771+ // #2
772+ packer .packString ("b" );
773+ packer .packString ("bar" );
774+
775+ packer .flush ();
776+
777+ byte [] bytes = out .toByteArray ();
778+
779+ BinKeyPojo binKeyPojo = objectMapper .readValue (bytes , BinKeyPojo .class );
780+ assertEquals ("foo" , binKeyPojo .s );
781+ assertArrayEquals ("bar" .getBytes (), binKeyPojo .b );
782+ }
761783}
You can’t perform that action at this time.
0 commit comments