package com.zipkey; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.zipkey.bean.Body; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class JsonZipKey { private static final char[] keyList = new char[52]; static { int total = 0; for(int c = 'a'; c <= 'z'; c++){ keyList[total++] = (char) c; } for(int c = 'A'; c <= 'Z'; c++){ keyList[total++] = (char) c; } } /** * { * "_": 1, * "map" : {"a":"uid"....}, * "id": "1" * } * @return */ public static <T> ObjectNode getJSONZipMap(Class<T> cl, String id, boolean shortToLong, boolean justMap) throws Exception { JsonNode json = JsonUtil.objToJsonNode(cl.newInstance()); ObjectNode jsonNode = JsonUtil.createObject(); Iterator<String> it = json.fieldNames(); int i = 0; while(it.hasNext()) { String k = it.next(); if(shortToLong) { jsonNode.put(getZipChar(i++), k); } else { jsonNode.put(k, getZipChar(i++)); } } if(justMap) return jsonNode; ObjectNode r = JsonUtil.createObject(); r.set("map", jsonNode); r.put("id", id); return r; } public static ObjectNode zip(JsonNode map, JsonNode value) { if(map == null) { throw new RuntimeException("map is not null!"); } ObjectNode r = JsonUtil.createObject(); value.fieldNames().forEachRemaining(a -> { JsonNode v = value.get(a); if(v != null && !v.isNull()) { JsonNode field = map.get(a); if(field == null) { throw new RuntimeException("map field not find: " + a); } r.set(field.asText(), v); } }); return r; } private static String getZipChar(int i) { if(i < 52) return keyList[i] + ""; StringBuilder sb = new StringBuilder(); do { sb.append(keyList[i % 52]); i = i / 52; } while (i > 52); sb.append(keyList[i-1]); return sb.toString(); } private static class JsonUtil { private static ObjectMapper mapper = new ObjectMapper(); private static Map<String, JsonNode> zipMap = new ConcurrentHashMap<>(); static { mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); } public static void putZipMap(String id, JsonNode map) { zipMap.put(id, map); } public static void getZipMap(String id, JsonNode map) { zipMap.put(id, map); } public static JsonNode objToJsonNode(Object obj) throws JsonProcessingException { return toJsonNode(toJson(obj)); } public static String toJson(Object obj) throws JsonProcessingException { return mapper.writeValueAsString(obj); } public static JsonNode toJsonNode(String json) throws JsonProcessingException { return mapper.readTree(json); } public static ObjectNode createObject() { return mapper.createObjectNode(); } } public static void main(String[] args) throws Exception { // Set<String> set = new HashSet<>(); // int size = 52 * 52 * 52; // for(int i = 0;i < size;i++) { // set.add(getZipChar(i)); // } // if(set.size() != size) { // throw new Exception(size + " error " + set.size()); // } // System.out.println(getJSONZipMap(Body.class, "1", true, false)); // System.out.println(getJSONZipMap(Body.class, "1", false, true)); ObjectNode map = getJSONZipMap(Body.class, "1", false, true); Body b = new Body(); b.setJson("{}"); b.setAti("aa"); b.setBit(11); b.setCity("china"); b.setDate("2023-01-01"); ObjectNode r = JsonZipKey.zip(map, JsonUtil.objToJsonNode(b)); r.put("_", "1"); System.out.println(r); } }
标签:map,JsonNode,jackson,ObjectNode,return,json,static,import,压缩工具 From: https://www.cnblogs.com/math-and-it/p/18138589