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 org .scalatest .prop .PropertyChecks
@@ -410,5 +414,36 @@ class MessagePackTest extends MessagePackSpec {
410414
411415 }
412416
417+ " pack/unpack maps in lists" in {
418+ val aMap = List (Map (" f" -> " x" ))
419+
420+ check(aMap, { packer =>
421+ packer.packArrayHeader(aMap.size)
422+ for (m <- aMap) {
423+ packer.packMapHeader(m.size)
424+ for ((k, v) <- m) {
425+ packer.packString(k)
426+ packer.packString(v)
427+ }
428+ }
429+ }, { unpacker =>
430+ val holder = new ValueHolder ()
431+ unpacker.unpackValue(holder)
432+ val v = holder.get()
433+
434+ v.asArrayValue().toValueArray.map { m =>
435+ val mv = m.asMapValue()
436+ val kvs = mv.toKeyValueSeq
437+
438+ kvs.grouped(2 ).map({ kvp : Array [Value ] =>
439+ val k = kvp(0 )
440+ val v = kvp(1 )
441+
442+ (k.asString().toString, v.asString().toString)
443+ }).toMap
444+ }.toList
445+ })
446+ }
447+
413448 }
414449}
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