Skip to content

Commit 285fa14

Browse files
committed
added MessagePack.convert and MessagePack.unconvert
1 parent 82a75c7 commit 285fa14

4 files changed

Lines changed: 64 additions & 12 deletions

File tree

src/main/java/org/msgpack/MessagePack.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import org.msgpack.packer.Packer;
2727
import org.msgpack.packer.StreamPacker;
2828
import org.msgpack.packer.BufferPacker;
29+
import org.msgpack.packer.Unconverter;
2930
import org.msgpack.unpacker.Unpacker;
3031
import org.msgpack.unpacker.StreamUnpacker;
3132
import org.msgpack.unpacker.BufferUnpacker;
33+
import org.msgpack.unpacker.Converter;
3234
import org.msgpack.value.Value;
3335
import org.msgpack.template.*;
3436

@@ -125,6 +127,25 @@ public <T> T unpack(ByteBuffer b, Class<T> c) { // TODO IOException
125127
return null;
126128
}
127129

130+
public <T> T convert(Value v, T to) throws IOException { // TODO IOException
131+
// TODO
132+
Template tmpl = getTemplate(to.getClass());
133+
return (T)tmpl.read(new Converter(v), to);
134+
}
135+
136+
public <T> T convert(Value v, Class<T> c) throws IOException { // TODO IOException
137+
// TODO
138+
Template tmpl = getTemplate(c);
139+
return (T)tmpl.read(new Converter(v), null);
140+
}
141+
142+
public Value unconvert(Object v) throws IOException { // TODO IOException
143+
Template tmpl = getTemplate(v.getClass());
144+
Unconverter pk = new Unconverter();
145+
tmpl.write(pk, v);
146+
return pk.getResult();
147+
}
148+
128149
protected Template getTemplate(Class<?> c) {
129150
Template tmpl = registry.lookup(c);
130151
if(tmpl == null) {

src/main/java/org/msgpack/unpacker/Converter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public int readArrayBegin() {
123123
throw new MessageTypeException("Expected array but got not array value");
124124
}
125125
ArrayValue a = v.asArrayValue();
126-
values[stack.getDepth()] = a.getElementArray();
127126
stack.pushArray(a.size());
127+
values[stack.getDepth()] = a.getElementArray();
128128
return a.size();
129129
}
130130

@@ -153,8 +153,8 @@ public int readMapBegin() {
153153
throw new MessageTypeException("Expected array but got not array value");
154154
}
155155
MapValue m = v.asMapValue();
156-
values[stack.getDepth()] = m.getKeyValueArray();
157156
stack.pushMap(m.size());
157+
values[stack.getDepth()] = m.getKeyValueArray();
158158
return m.size();
159159
}
160160

@@ -186,7 +186,7 @@ private Value getTop() {
186186
return (Value) values[0];
187187
}
188188
Value[] array = (Value[]) values[stack.getDepth()];
189-
return array[stack.getTopCount()];
189+
return array[array.length - stack.getTopCount()];
190190
}
191191

192192
@Override
@@ -209,14 +209,14 @@ protected void readValue(Unconverter uc) {
209209
if(v.isArray()) {
210210
ArrayValue a = v.asArrayValue();
211211
uc.writeArrayBegin(a.size());
212-
values[stack.getDepth()] = a.getElementArray();
213212
stack.pushArray(a.size());
213+
values[stack.getDepth()] = a.getElementArray();
214214

215215
} else if(v.isMap()) {
216216
MapValue m = v.asMapValue();
217217
uc.writeMapBegin(m.size());
218-
values[stack.getDepth()] = m.getKeyValueArray();
219218
stack.pushMap(m.size());
219+
values[stack.getDepth()] = m.getKeyValueArray();
220220

221221
} else {
222222
uc.write(v);
@@ -253,13 +253,13 @@ public void skip() {
253253
while(true) {
254254
if(v.isArray()) {
255255
ArrayValue a = v.asArrayValue();
256-
values[stack.getDepth()] = a.getElementArray();
257256
stack.pushArray(a.size());
257+
values[stack.getDepth()] = a.getElementArray();
258258

259259
} else if(v.isMap()) {
260260
MapValue m = v.asMapValue();
261-
values[stack.getDepth()] = m.getKeyValueArray();
262261
stack.pushMap(m.size());
262+
values[stack.getDepth()] = m.getKeyValueArray();
263263

264264
} else {
265265
stack.reduceCount();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.msgpack;
2+
3+
import java.math.BigInteger;
4+
import java.nio.ByteBuffer;
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Arrays;
12+
13+
import org.junit.Test;
14+
import org.msgpack.MessagePack;
15+
import org.msgpack.value.Value;
16+
import org.msgpack.unpacker.Converter;
17+
import org.msgpack.packer.Unconverter;
18+
19+
import junit.framework.TestCase;
20+
21+
public class TestSimpleConvertUnconvert extends TestCase {
22+
@Test
23+
public void testSimpleConvert() throws IOException {
24+
MessagePack msgpack = new MessagePack();
25+
byte[] raw = msgpack.pack(new int[] {1,2,3});
26+
27+
Value v = msgpack.unpack(raw);
28+
29+
int[] array = msgpack.convert(v, new int[3]);
30+
assertTrue(Arrays.equals(new int[] {1,2,3}, array));
31+
32+
Value v2 = msgpack.unconvert(array);
33+
assertEquals(v, v2);
34+
}
35+
}
36+

src/test/java/org/msgpack/TestSimplePackUnpack.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@ public class TestSimplePackUnpack extends TestCase {
1919
@Test
2020
public void testSimplePackUnpack() throws IOException {
2121
MessagePack msgpack = new MessagePack();
22-
2322
byte[] raw = msgpack.pack(new int[] {1,2,3});
2423

2524
Value v = msgpack.unpack(raw);
26-
2725
int[] a = msgpack.unpack(raw, new int[3]);
2826

2927
Value vb = msgpack.unpack(ByteBuffer.wrap(raw));
30-
3128
int[] ab = msgpack.unpack(ByteBuffer.wrap(raw), new int[3]);
32-
33-
System.out.println("value: "+vb);
3429
}
3530
}
3631

0 commit comments

Comments
 (0)