Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.msgpack.jackson.dataformat;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.IOException;
import java.nio.ByteBuffer;

@JsonSerialize(using = MessagePackExtensionType.Serializer.class)
public class MessagePackExtensionType
{
private final int extType;
private final ByteBuffer byteBuffer;

public MessagePackExtensionType(int extType, ByteBuffer byteBuffer) {
this.extType = extType;
this.byteBuffer = byteBuffer.isReadOnly() ?
byteBuffer : byteBuffer.asReadOnlyBuffer();
}

public int extType() {
return extType;
}

public ByteBuffer byteBuffer() {
return byteBuffer;
}

public static class Serializer extends JsonSerializer<MessagePackExtensionType> {
@Override
public void serialize(MessagePackExtensionType value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
if (gen instanceof MessagePackGenerator) {
MessagePackGenerator msgpackGenerator = (MessagePackGenerator)gen;
msgpackGenerator.writeExtendedType(value);
}
else {
throw new IllegalStateException("'gen' is expected to be MessagePackGenerator but it's " + gen.getClass());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ else if (v instanceof BigDecimal) {
else if (v instanceof Boolean) {
messagePacker.packBoolean((Boolean) v);
}
else if (v instanceof MessagePackExtensionType) {
MessagePackExtensionType extendedType = (MessagePackExtensionType) v;
ByteBuffer buf = extendedType.byteBuffer();
messagePacker.packExtensionTypeHeader((byte)extendedType.extType(), buf.remaining());
messagePacker.writePayload(buf);
}
else {
throw new IllegalArgumentException(v.toString());
}
Expand Down Expand Up @@ -408,6 +414,10 @@ public void writeNull()
addValueToStackTop(null);
}

public void writeExtendedType(MessagePackExtensionType extendedType) throws IOException {
addValueToStackTop(extendedType);
}

@Override
public void close()
throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.msgpack.core.ExtensionTypeHeader;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;
import org.msgpack.core.buffer.ArrayBufferInput;
Expand All @@ -29,12 +30,14 @@
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -71,6 +74,10 @@ public void testGeneratorShouldWriteObject()
childArray.add("child#1");
childArray.add(1.23f);
hashMap.put("childArray", childArray);
// #10
byte[] hello = "hello".getBytes("UTF-8");
ByteBuffer buffer = ByteBuffer.wrap(hello);
hashMap.put("ext", new MessagePackExtensionType(17, buffer));

long bitmap = 0;
byte[] bytes = objectMapper.writeValueAsBytes(hashMap);
Expand Down Expand Up @@ -142,11 +149,24 @@ else if (key.equals("childArray")) {
assertEquals(1.23f, messageUnpacker.unpackFloat(), 0.01f);
bitmap |= 0x1 << 9;
}
else if (key.equals("ext")) {
// #10
ExtensionTypeHeader header = messageUnpacker.unpackExtensionTypeHeader();
assertEquals(17, header.getType());
assertEquals(5, header.getLength());
ByteBuffer payload = ByteBuffer.allocate(header.getLength());
payload.flip();
payload.limit(payload.capacity());
messageUnpacker.readPayload(payload);
payload.flip();
assertArrayEquals("hello".getBytes(), payload.array());
bitmap |= 0x1 << 10;
}
else {
assertTrue(false);
}
}
assertEquals(0x03FF, bitmap);
assertEquals(0x07FF, bitmap);
}

@Test
Expand Down