|
| 1 | +// |
| 2 | +// MessagePack for Java |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +package org.msgpack.jackson.dataformat; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 19 | +import com.fasterxml.jackson.core.JsonParser; |
| 20 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 21 | +import com.fasterxml.jackson.core.TreeNode; |
| 22 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 23 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 24 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 25 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 28 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 29 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import static org.junit.Assert.assertArrayEquals; |
| 40 | +import static org.junit.Assert.assertEquals; |
| 41 | + |
| 42 | +public class ExampleOfTypeInformationSerDe |
| 43 | + extends MessagePackDataformatTestBase |
| 44 | +{ |
| 45 | + static class A |
| 46 | + { |
| 47 | + private List<String> list = new ArrayList<String>(); |
| 48 | + |
| 49 | + public List<String> getList() |
| 50 | + { |
| 51 | + return list; |
| 52 | + } |
| 53 | + |
| 54 | + public void setList(List<String> list) |
| 55 | + { |
| 56 | + this.list = list; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + static class B |
| 61 | + { |
| 62 | + private String str; |
| 63 | + |
| 64 | + public String getStr() |
| 65 | + { |
| 66 | + return str; |
| 67 | + } |
| 68 | + |
| 69 | + public void setStr(String str) |
| 70 | + { |
| 71 | + this.str = str; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + @JsonSerialize(using=ObjectContainerSerializer.class) |
| 77 | + @JsonDeserialize(using=ObjectContainerDeserializer.class) |
| 78 | + static class ObjectContainer |
| 79 | + { |
| 80 | + private final Map<String, Object> objects; |
| 81 | + |
| 82 | + public ObjectContainer(Map<String, Object> objects) |
| 83 | + { |
| 84 | + this.objects = objects; |
| 85 | + } |
| 86 | + |
| 87 | + public Map<String, Object> getObjects() |
| 88 | + { |
| 89 | + return objects; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + static class ObjectContainerSerializer |
| 94 | + extends JsonSerializer<ObjectContainer> |
| 95 | + { |
| 96 | + @Override |
| 97 | + public void serialize(ObjectContainer value, JsonGenerator gen, SerializerProvider serializers) |
| 98 | + throws IOException, JsonProcessingException |
| 99 | + { |
| 100 | + gen.writeStartObject(); |
| 101 | + HashMap<String, String> metadata = new HashMap<String, String>(); |
| 102 | + for (Map.Entry<String, Object> entry : value.getObjects().entrySet()) { |
| 103 | + metadata.put(entry.getKey(), entry.getValue().getClass().getName()); |
| 104 | + } |
| 105 | + gen.writeObjectField("__metadata", metadata); |
| 106 | + gen.writeObjectField("objects", value.getObjects()); |
| 107 | + gen.writeEndObject(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static class ObjectContainerDeserializer |
| 112 | + extends JsonDeserializer<ObjectContainer> |
| 113 | + { |
| 114 | + @Override |
| 115 | + public ObjectContainer deserialize(JsonParser p, DeserializationContext ctxt) |
| 116 | + throws IOException, JsonProcessingException |
| 117 | + { |
| 118 | + ObjectContainer objectContainer = new ObjectContainer(new HashMap<String, Object>()); |
| 119 | + TreeNode treeNode = p.readValueAsTree(); |
| 120 | + |
| 121 | + Map<String, String> metadata = treeNode.get("__metadata").traverse(p.getCodec()).readValueAs(new TypeReference<Map<String, String>>() {}); |
| 122 | + TreeNode dataMapTree = treeNode.get("objects"); |
| 123 | + for (Map.Entry<String, String> entry : metadata.entrySet()) { |
| 124 | + try { |
| 125 | + Object o = dataMapTree.get(entry.getKey()).traverse(p.getCodec()).readValueAs(Class.forName(entry.getValue())); |
| 126 | + objectContainer.getObjects().put(entry.getKey(), o); |
| 127 | + } |
| 128 | + catch (ClassNotFoundException e) { |
| 129 | + throw new RuntimeException("Failed to deserialize: " + entry, e); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return objectContainer; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void test() |
| 139 | + throws IOException |
| 140 | + { |
| 141 | + ObjectContainer objectContainer = new ObjectContainer(new HashMap<String, Object>()); |
| 142 | + { |
| 143 | + A a = new A(); |
| 144 | + a.setList(Arrays.asList("first", "second", "third")); |
| 145 | + objectContainer.getObjects().put("a", a); |
| 146 | + |
| 147 | + B b = new B(); |
| 148 | + b.setStr("hello world"); |
| 149 | + objectContainer.getObjects().put("b", b); |
| 150 | + |
| 151 | + Double pi = 3.14; |
| 152 | + objectContainer.getObjects().put("pi", pi); |
| 153 | + } |
| 154 | + |
| 155 | + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 156 | + byte[] bytes = objectMapper.writeValueAsBytes(objectContainer); |
| 157 | + ObjectContainer restored = objectMapper.readValue(bytes, ObjectContainer.class); |
| 158 | + |
| 159 | + { |
| 160 | + assertEquals(3, restored.getObjects().size()); |
| 161 | + A a = (A) restored.getObjects().get("a"); |
| 162 | + assertArrayEquals(new String[] {"first", "second", "third"}, a.getList().toArray()); |
| 163 | + B b = (B) restored.getObjects().get("b"); |
| 164 | + assertEquals("hello world", b.getStr()); |
| 165 | + assertEquals(3.14, restored.getObjects().get("pi")); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments