| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,15 @@ | |||
| 1 | 1 | package org.msgpack.jackson.dataformat; | |
| 2 | 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; | ||
| 3 | 10 | import java.nio.ByteBuffer; | |
| 4 | 11 | ||
| 5 | - /** | ||
| 6 | - * Created by komamitsu on 3/7/15. | ||
| 7 | - */ | ||
| 12 | + @JsonSerialize(using = MessagePackExtendedType.Serializer.class) | ||
| 8 | 13 | public class MessagePackExtendedType { | |
| 9 | 14 | private final int extType; | |
| 10 | 15 | private final ByteBuffer byteBuffer; | |
@@ -22,4 +27,18 @@ public int extType() { | |||
| 22 | 27 | public ByteBuffer byteBuffer() { | |
| 23 | 28 | return byteBuffer; | |
| 24 | 29 | } | |
| 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 | + } | ||
| 25 | 44 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -175,6 +175,12 @@ else if (v instanceof BigDecimal) { | |||
| 175 | 175 | else if (v instanceof Boolean) { | |
| 176 | 176 | messagePacker.packBoolean((Boolean) v); | |
| 177 | 177 | } | |
| 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 | + } | ||
| 178 | 184 | else { | |
| 179 | 185 | throw new IllegalArgumentException(v.toString()); | |
| 180 | 186 | } | |
@@ -324,6 +330,10 @@ public void writeNull() throws IOException, JsonGenerationException { | |||
| 324 | 330 | addValueToStackTop(null); | |
| 325 | 331 | } | |
| 326 | 332 | ||
| 333 | + public void writeExtendedType(MessagePackExtendedType extendedType) throws IOException { | ||
| 334 | + addValueToStackTop(extendedType); | ||
| 335 | + } | ||
| 336 | + | ||
| 327 | 337 | @Override | |
| 328 | 338 | public void close() throws IOException { | |
| 329 | 339 | try { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,7 @@ | |||
| 19 | 19 | import com.fasterxml.jackson.core.JsonGenerator; | |
| 20 | 20 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 21 | 21 | import org.junit.Test; | |
| 22 | + import org.msgpack.core.ExtendedTypeHeader; | ||
| 22 | 23 | import org.msgpack.core.MessagePack; | |
| 23 | 24 | import org.msgpack.core.MessageUnpacker; | |
| 24 | 25 | import org.msgpack.core.buffer.ArrayBufferInput; | |
@@ -29,8 +30,10 @@ | |||
| 29 | 30 | import java.io.IOException; | |
| 30 | 31 | import java.io.OutputStream; | |
| 31 | 32 | import java.math.BigDecimal; | |
| 33 | + import java.nio.ByteBuffer; | ||
| 32 | 34 | import java.util.*; | |
| 33 | 35 | ||
| 36 | + import static org.junit.Assert.assertArrayEquals; | ||
| 34 | 37 | import static org.junit.Assert.assertEquals; | |
| 35 | 38 | import static org.junit.Assert.assertFalse; | |
| 36 | 39 | import static org.junit.Assert.assertTrue; | |
@@ -63,6 +66,10 @@ public void testGeneratorShouldWriteObject() throws IOException { | |||
| 63 | 66 | childArray.add("child#1"); | |
| 64 | 67 | childArray.add(1.23f); | |
| 65 | 68 | 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)); | ||
| 66 | 73 | ||
| 67 | 74 | long bitmap = 0; | |
| 68 | 75 | byte[] bytes = objectMapper.writeValueAsBytes(hashMap); | |
@@ -134,11 +141,24 @@ else if (key.equals("childArray")) { | |||
| 134 | 141 | assertEquals(1.23f, messageUnpacker.unpackFloat(), 0.01f); | |
| 135 | 142 | bitmap |= 0x1 << 9; | |
| 136 | 143 | } | |
| 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 | + } | ||
| 137 | 157 | else { | |
| 138 | 158 | assertTrue(false); | |
| 139 | 159 | } | |
| 140 | 160 | } | |
| 141 | - assertEquals(0x03FF, bitmap); | ||
| 161 | + assertEquals(0x07FF, bitmap); | ||
| 142 | 162 | } | |
| 143 | 163 | ||
| 144 | 164 | @Test | |
@@ -270,7 +290,6 @@ public void testBigDecimal() throws IOException { | |||
| 270 | 290 | } | |
| 271 | 291 | ||
| 272 | 292 | { | |
| 273 | - | ||
| 274 | 293 | BigDecimal decimal = new BigDecimal("1234.567890123456789012345678901234567890"); | |
| 275 | 294 | List<BigDecimal> bigDecimals = Arrays.asList( | |
| 276 | 295 | decimal | |
| Back | FazBrowse Home | New Git URL |
0 commit comments