Skip to content

Commit cc7e390

Browse files
committed
Merge pull request msgpack#250 from msgpack/issue_238
Make MessagePackGenerator be able to serialize extended type
2 parents ef2a934 + ddc7e6f commit cc7e390

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.msgpack.jackson.dataformat;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.JsonSerializer;
6+
import com.fasterxml.jackson.databind.SerializerProvider;
7+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
8+
9+
import java.io.IOException;
10+
import java.nio.ByteBuffer;
11+
12+
@JsonSerialize(using = MessagePackExtensionType.Serializer.class)
13+
public class MessagePackExtensionType
14+
{
15+
private final int extType;
16+
private final ByteBuffer byteBuffer;
17+
18+
public MessagePackExtensionType(int extType, ByteBuffer byteBuffer) {
19+
this.extType = extType;
20+
this.byteBuffer = byteBuffer.isReadOnly() ?
21+
byteBuffer : byteBuffer.asReadOnlyBuffer();
22+
}
23+
24+
public int extType() {
25+
return extType;
26+
}
27+
28+
public ByteBuffer byteBuffer() {
29+
return byteBuffer;
30+
}
31+
32+
public static class Serializer extends JsonSerializer<MessagePackExtensionType> {
33+
@Override
34+
public void serialize(MessagePackExtensionType value, JsonGenerator gen, SerializerProvider serializers)
35+
throws IOException, JsonProcessingException {
36+
if (gen instanceof MessagePackGenerator) {
37+
MessagePackGenerator msgpackGenerator = (MessagePackGenerator)gen;
38+
msgpackGenerator.writeExtendedType(value);
39+
}
40+
else {
41+
throw new IllegalStateException("'gen' is expected to be MessagePackGenerator but it's " + gen.getClass());
42+
}
43+
}
44+
}
45+
}

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ else if (v instanceof BigDecimal) {
213213
else if (v instanceof Boolean) {
214214
messagePacker.packBoolean((Boolean) v);
215215
}
216+
else if (v instanceof MessagePackExtensionType) {
217+
MessagePackExtensionType extendedType = (MessagePackExtensionType) v;
218+
ByteBuffer buf = extendedType.byteBuffer();
219+
messagePacker.packExtensionTypeHeader((byte)extendedType.extType(), buf.remaining());
220+
messagePacker.writePayload(buf);
221+
}
216222
else {
217223
throw new IllegalArgumentException(v.toString());
218224
}
@@ -408,6 +414,10 @@ public void writeNull()
408414
addValueToStackTop(null);
409415
}
410416

417+
public void writeExtendedType(MessagePackExtensionType extendedType) throws IOException {
418+
addValueToStackTop(extendedType);
419+
}
420+
411421
@Override
412422
public void close()
413423
throws IOException

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackGeneratorTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.JsonGenerator;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import org.junit.Test;
22+
import org.msgpack.core.ExtensionTypeHeader;
2223
import org.msgpack.core.MessagePack;
2324
import org.msgpack.core.MessageUnpacker;
2425
import org.msgpack.core.buffer.ArrayBufferInput;
@@ -29,12 +30,14 @@
2930
import java.io.IOException;
3031
import java.io.OutputStream;
3132
import java.math.BigDecimal;
33+
import java.nio.ByteBuffer;
3234
import java.util.ArrayList;
3335
import java.util.Arrays;
3436
import java.util.HashMap;
3537
import java.util.List;
3638
import java.util.Map;
3739

40+
import static org.junit.Assert.assertArrayEquals;
3841
import static org.junit.Assert.assertEquals;
3942
import static org.junit.Assert.assertFalse;
4043
import static org.junit.Assert.assertTrue;
@@ -71,6 +74,10 @@ public void testGeneratorShouldWriteObject()
7174
childArray.add("child#1");
7275
childArray.add(1.23f);
7376
hashMap.put("childArray", childArray);
77+
// #10
78+
byte[] hello = "hello".getBytes("UTF-8");
79+
ByteBuffer buffer = ByteBuffer.wrap(hello);
80+
hashMap.put("ext", new MessagePackExtensionType(17, buffer));
7481

7582
long bitmap = 0;
7683
byte[] bytes = objectMapper.writeValueAsBytes(hashMap);
@@ -142,11 +149,24 @@ else if (key.equals("childArray")) {
142149
assertEquals(1.23f, messageUnpacker.unpackFloat(), 0.01f);
143150
bitmap |= 0x1 << 9;
144151
}
152+
else if (key.equals("ext")) {
153+
// #10
154+
ExtensionTypeHeader header = messageUnpacker.unpackExtensionTypeHeader();
155+
assertEquals(17, header.getType());
156+
assertEquals(5, header.getLength());
157+
ByteBuffer payload = ByteBuffer.allocate(header.getLength());
158+
payload.flip();
159+
payload.limit(payload.capacity());
160+
messageUnpacker.readPayload(payload);
161+
payload.flip();
162+
assertArrayEquals("hello".getBytes(), payload.array());
163+
bitmap |= 0x1 << 10;
164+
}
145165
else {
146166
assertTrue(false);
147167
}
148168
}
149-
assertEquals(0x03FF, bitmap);
169+
assertEquals(0x07FF, bitmap);
150170
}
151171

152172
@Test

0 commit comments

Comments
 (0)