您好,登錄后才能下訂單哦!
當(dāng)你希望在 Map 中不使用 String 為 Key,那么你需要使用?MessagePackKeySerializer
?來為 key 進(jìn)行序列化。
本測(cè)試方法,可以在?https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java?中找到。
/** ?*?testMessagePackSerializationMapKey ?*/ @Test @JsonSerialize(keyUsing?=?MessagePackKeySerializer.class) public?void?testMessagePackSerializationMapKey()?{ ????logger.debug("testMessagePackSerializationNotCloseInputStream"); ????byte[]?bytes?=?new?byte[0]; ????Integer?uuid_a?=?101; ????Integer?uuid_b?=?102; ????//?Instantiate?ObjectMapper?for?MessagePack ????ObjectMapper?objectMapper?=?new?ObjectMapper(new?MessagePackFactory()); ????Map<Integer,?MessageData>?map?=?new?HashMap<>(); ????MessageData?messageData?=?new?MessageData(); ????//?Element?A?in?MAP ????messageData.setUuid(UUID.randomUUID().toString()); ????messageData.setName("CWIKI.US?-?A"); ????map.put(uuid_a,?messageData); ????//?Element?B?in?MAP ????messageData?=?new?MessageData(); ????messageData.setUuid(UUID.randomUUID().toString()); ????messageData.setName("CWIKI.US?-?B"); ????map.put(uuid_b,?messageData); ????try?{ ????????//?Serialize?a?Java?object?to?byte?array ????????bytes?=?objectMapper.writeValueAsBytes(map); ????????logger.debug("Length?of?Bytes:?[{}]",?bytes.length); ????????//?Deserialize?the?byte?array?to?a?MAP ????????Map<String,?MessageData>?deserialized?=?objectMapper.readValue(bytes,?new?TypeReference<Map<Integer,?MessageData>>()?{ ????????}); ????????logger.debug("Deserialized?MAP?Count:?[{}]",?deserialized.size()); ????????logger.debug("MAP?index?0:?[{}]",?deserialized.get(uuid_a).getName()); ????????assertEquals("CWIKI.US?-?A",?deserialized.get(uuid_a).getName()); ????}?catch?(JsonProcessingException?ex)?{ ????????logger.error("Serialize?Error",?ex); ????}?catch?(IOException?e)?{ ????????e.printStackTrace(); ????} }
?
https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Dataformat
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。