Skip to content

Commit 584e9d2

Browse files
committed
Make MessagePackGenerator be able to serialize extended type
1 parent f27f1dc commit 584e9d2

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.msgpack.jackson.dataformat;
22

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;
310
import java.nio.ByteBuffer;
411

5-
/**
6-
* Created by komamitsu on 3/7/15.
7-
*/
12+
@JsonSerialize(using = MessagePackExtendedType.Serializer.class)
813
public class MessagePackExtendedType {
914
private final int extType;
1015
private final ByteBuffer byteBuffer;
@@ -22,4 +27,18 @@ public int extType() {
2227
public ByteBuffer byteBuffer() {
2328
return byteBuffer;
2429
}
30+
31+
public static class Serializer extends JsonSerializer<MessagePackExtendedType> {
32+
@Override
33+
public void serialize(MessagePackExtendedType value, JsonGenerator gen, SerializerProvider serializers)
34+
throws IOException, JsonProcessingException {
35+
if (gen instanceof MessagePackGenerator) {
36+
MessagePackGenerator msgpackGenerator = (MessagePackGenerator)gen;
37+
msgpackGenerator.writeExtendedType(value);
38+
}
39+
else {
40+
throw new IllegalStateException("gen is expected to be MessagePackGenerator but it's " + gen.getClass());
41+
}
42+
}
43+
}
2544
}

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
@@ -175,6 +175,12 @@ else if (v instanceof BigDecimal) {
175175
else if (v instanceof Boolean) {
176176
messagePacker.packBoolean((Boolean) v);
177177
}
178+
else if (v instanceof MessagePackExtendedType) {
179+
MessagePackExtendedType extendedType = (MessagePackExtendedType) v;
180+
ByteBuffer buf = extendedType.byteBuffer();
181+
messagePacker.packExtendedTypeHeader(extendedType.extType(), buf.remaining());
182+
messagePacker.writePayload(buf);
183+
}
178184
else {
179185
throw new IllegalArgumentException(v.toString());
180186
}
@@ -324,6 +330,10 @@ public void writeNull() throws IOException, JsonGenerationException {
324330
addValueToStackTop(null);
325331
}
326332

333+
public void writeExtendedType(MessagePackExtendedType extendedType) throws IOException {
334+
addValueToStackTop(extendedType);
335+
}
336+
327337
@Override
328338
public void close() throws IOException {
329339
try {

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

Lines changed: 21 additions & 2 deletions
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.ExtendedTypeHeader;
2223
import org.msgpack.core.MessagePack;
2324
import org.msgpack.core.MessageUnpacker;
2425
import org.msgpack.core.buffer.ArrayBufferInput;
@@ -29,8 +30,10 @@
2930
import java.io.IOException;
3031
import java.io.OutputStream;
3132
import java.math.BigDecimal;
33+
import java.nio.ByteBuffer;
3234
import java.util.*;
3335

36+
import static org.junit.Assert.assertArrayEquals;
3437
import static org.junit.Assert.assertEquals;
3538
import static org.junit.Assert.assertFalse;
3639
import static org.junit.Assert.assertTrue;
@@ -63,6 +66,10 @@ public void testGeneratorShouldWriteObject() throws IOException {
6366
childArray.add("child#1");
6467
childArray.add(1.23f);
6568
hashMap.put("childArray", childArray);
69+
// #10
70+
byte[] hello = "hello".getBytes("UTF-8");
71+
ByteBuffer buffer = ByteBuffer.wrap(hello);
72+
hashMap.put("ext", new MessagePackExtendedType(17, buffer));
6673

6774
long bitmap = 0;
6875
byte[] bytes = objectMapper.writeValueAsBytes(hashMap);
@@ -134,11 +141,24 @@ else if (key.equals("childArray")) {
134141
assertEquals(1.23f, messageUnpacker.unpackFloat(), 0.01f);
135142
bitmap |= 0x1 << 9;
136143
}
144+
else if (key.equals("ext")) {
145+
// #9
146+
ExtendedTypeHeader header = messageUnpacker.unpackExtendedTypeHeader();
147+
assertEquals(17, header.getType());
148+
assertEquals(5, header.getLength());
149+
ByteBuffer payload = ByteBuffer.allocate(header.getLength());
150+
payload.flip();
151+
payload.limit(payload.capacity());
152+
messageUnpacker.readPayload(payload);
153+
payload.flip();
154+
assertArrayEquals("hello".getBytes(), payload.array());
155+
bitmap |= 0x1 << 10;
156+
}
137157
else {
138158
assertTrue(false);
139159
}
140160
}
141-
assertEquals(0x03FF, bitmap);
161+
assertEquals(0x07FF, bitmap);
142162
}
143163

144164
@Test
@@ -270,7 +290,6 @@ public void testBigDecimal() throws IOException {
270290
}
271291

272292
{
273-
274293
BigDecimal decimal = new BigDecimal("1234.567890123456789012345678901234567890");
275294
List<BigDecimal> bigDecimals = Arrays.asList(
276295
decimal

0 commit comments

Comments
 (0)