FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Make MessagePackParser accept a string as a byte array field by komamitsu · Pull Request #420 · msgpack/msgpack-java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.msgpack.core.MessageFormat;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;
import org.msgpack.core.Preconditions;
import org.msgpack.core.buffer.ArrayBufferInput;
import org.msgpack.core.buffer.InputStreamBufferInput;
import org.msgpack.core.buffer.MessageBufferInput;
Expand Down Expand Up @@ -419,8 +418,14 @@ public int getTextOffset()
public byte[] getBinaryValue(Base64Variant b64variant)
throws IOException, JsonParseException
{
Preconditions.checkArgument(type == Type.BYTES);
return bytesValue;
switch (type) {
case BYTES:
return bytesValue;
case STRING:
return stringValue.getBytes(MessagePack.UTF8);
default:
throw new IllegalStateException("Invalid type=" + type);
}
}

@Override
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public void setS(String s)
}
}

public static class BinKeyPojo
{
public byte[] b;
public String s;
}

public static class UsingCustomConstructorPojo
{
final String name;
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,26 @@ public Object deserialize(byte[] data)
}
assertThat((String) values.get(4), is("Java"));
}

@Test
public void parserShouldReadStrAsBin()
throws IOException
{
MessagePacker packer = MessagePack.newDefaultPacker(out);
packer.packMapHeader(2);
// #1
packer.packString("s");
packer.packString("foo");
// #2
packer.packString("b");
packer.packString("bar");

packer.flush();

byte[] bytes = out.toByteArray();

BinKeyPojo binKeyPojo = objectMapper.readValue(bytes, BinKeyPojo.class);
assertEquals("foo", binKeyPojo.s);
assertArrayEquals("bar".getBytes(), binKeyPojo.b);
}
}

Back | FazBrowse Home | New Git URL