|
26 | 26 | import org.msgpack.core.buffer.OutputStreamBufferOutput; |
27 | 27 | import org.msgpack.value.ExtensionValue; |
28 | 28 |
|
| 29 | +import java.io.ByteArrayOutputStream; |
29 | 30 | import java.io.File; |
30 | 31 | import java.io.FileInputStream; |
31 | 32 | import java.io.FileOutputStream; |
@@ -488,4 +489,52 @@ public void testReadPrimitiveObjectViaObjectMapper() |
488 | 489 | assertEquals(bytes[1], bs[1]); |
489 | 490 | assertEquals(bytes[2], bs[2]); |
490 | 491 | } |
| 492 | + |
| 493 | + @Test |
| 494 | + public void testBinaryKey() |
| 495 | + throws Exception |
| 496 | + { |
| 497 | + File tempFile = createTempFile(); |
| 498 | + FileOutputStream out = new FileOutputStream(tempFile); |
| 499 | + MessagePacker packer = MessagePack.newDefaultPacker(out); |
| 500 | + packer.packMapHeader(2); |
| 501 | + packer.packString("foo"); |
| 502 | + packer.packDouble(3.14); |
| 503 | + byte[] bytes = "bar".getBytes(); |
| 504 | + packer.packBinaryHeader(bytes.length); |
| 505 | + packer.writePayload(bytes); |
| 506 | + packer.packLong(42); |
| 507 | + packer.close(); |
| 508 | + |
| 509 | + ObjectMapper mapper = new ObjectMapper(new MessagePackFactory()); |
| 510 | + Map<String, Object> object = mapper.readValue(new FileInputStream(tempFile), new TypeReference<Map<String, Object>>() {}); |
| 511 | + assertEquals(2, object.size()); |
| 512 | + assertEquals(3.14, object.get("foo")); |
| 513 | + assertEquals(42, object.get("bar")); |
| 514 | + } |
| 515 | + |
| 516 | + @Test |
| 517 | + public void testBinaryKeyInNestedObject() |
| 518 | + throws Exception |
| 519 | + { |
| 520 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 521 | + MessagePacker packer = MessagePack.newDefaultPacker(out); |
| 522 | + packer.packArrayHeader(2); |
| 523 | + packer.packMapHeader(1); |
| 524 | + byte[] bytes = "bar".getBytes(); |
| 525 | + packer.packBinaryHeader(bytes.length); |
| 526 | + packer.writePayload(bytes); |
| 527 | + packer.packInt(12); |
| 528 | + packer.packInt(1); |
| 529 | + packer.close(); |
| 530 | + |
| 531 | + ObjectMapper mapper = new ObjectMapper(new MessagePackFactory()); |
| 532 | + List<Object> objects = mapper.readValue(out.toByteArray(), new TypeReference<List<Object>>() {}); |
| 533 | + assertEquals(2, objects.size()); |
| 534 | + @SuppressWarnings(value="unchecked") |
| 535 | + Map<String, Object> map = (Map<String, Object>) objects.get(0); |
| 536 | + assertEquals(1, map.size()); |
| 537 | + assertEquals(12, map.get("bar")); |
| 538 | + assertEquals(1, objects.get(1)); |
| 539 | + } |
491 | 540 | } |
0 commit comments