File tree Expand file tree Collapse file tree 3 files changed +54
-1
lines changed
main/java/org/msgpack/value/impl Expand file tree Collapse file tree 3 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -109,7 +109,7 @@ public void accept(ValueVisitor visitor) {
109109 @ Override
110110 public MapValue toValue () {
111111 ensureNotTraversed ();
112- Value [] keyValueArray = new Value [mapSize ];
112+ Value [] keyValueArray = new Value [mapSize * 2 ];
113113 int i = 0 ;
114114 while (hasNext ()) {
115115 keyValueArray [i ++] = nextKeyOrValue ().toValue ();
Original file line number Diff line number Diff line change 11package org .msgpack .core
22
3+ import org .msgpack .value .Value
4+ import org .msgpack .value .holder .ValueHolder
5+
6+ import scala .collection .mutable .ListBuffer
37import scala .util .Random
48import MessagePack .Code
59import java .io .ByteArrayOutputStream
@@ -413,5 +417,36 @@ class MessagePackTest extends MessagePackSpec {
413417
414418 }
415419
420+ " pack/unpack maps in lists" in {
421+ val aMap = List (Map (" f" -> " x" ))
422+
423+ check(aMap, { packer =>
424+ packer.packArrayHeader(aMap.size)
425+ for (m <- aMap) {
426+ packer.packMapHeader(m.size)
427+ for ((k, v) <- m) {
428+ packer.packString(k)
429+ packer.packString(v)
430+ }
431+ }
432+ }, { unpacker =>
433+ val holder = new ValueHolder ()
434+ unpacker.unpackValue(holder)
435+ val v = holder.get()
436+
437+ v.asArrayValue().toValueArray.map { m =>
438+ val mv = m.asMapValue()
439+ val kvs = mv.toKeyValueSeq
440+
441+ kvs.grouped(2 ).map({ kvp : Array [Value ] =>
442+ val k = kvp(0 )
443+ val v = kvp(1 )
444+
445+ (k.asString().toString, v.asString().toString)
446+ }).toMap
447+ }.toList
448+ })
449+ }
450+
416451 }
417452}
Original file line number Diff line number Diff line change @@ -52,6 +52,24 @@ class CursorTest extends MessagePackSpec {
5252 }
5353 }
5454
55+ " have map cursor" taggedAs(" map" ) in {
56+ val packedData = createMessagePackData { packer =>
57+ packer packMapHeader(1 ) packString(" f" ) packString(" x" )
58+ }
59+
60+ val cursor = mf.newUnpacker(packedData).getCursor
61+ val mapCursor = cursor.nextRef().getMapCursor
62+ mapCursor.size() shouldBe 1
63+
64+ val mapValue = mapCursor.toValue
65+ val data = mapValue.toKeyValueSeq
66+
67+ data should have length 2
68+
69+ data(0 ).asString().toString shouldBe " f"
70+ data(1 ).asString().toString shouldBe " x"
71+ }
72+
5573 " traverse ValueRef faster than traversing Value" taggedAs(" ref" ) in {
5674 val N = 10000
5775 val data = binSeq(N )
You can’t perform that action at this time.
0 commit comments