Business scenario: Recently, we are doing a file path encryption. Many classes are designed to file paths. Because the framework used in our project is quite wonderful, however, many classes inherit a parent class that processes return parameters. I started working in this parent class:
The tool for serialization uses GSON belonging to Google:
1 import com.alibaba.fastjson.JSONObject; 2 import com.fasterxml.jackson.databind.ObjectMapper; 3 import com.google.gson.*; 4 import com.sxx.inpa.CommonEnctyptUtil; 5 6 import java.io.IOException; 7 import java.lang.reflect.Type; 8 import java.util.*; 9 10 public class GosnDemo { 11 12 public static void main(String[] args) throws IOException { 13 // GsonBuilder Used to generate Gson object. Regulations Gson The format of serialization and deserialization of. 14 GsonBuilder gsonBuilder = new GsonBuilder(); 15 // Here, we define the serializer of anonymous inner class, and specify that only for String Type value To serialize, you can do the business operations you want during the serialization process 16 // Of course, my business here is to AES encryption 17 JsonSerializer<String> serializer = 18 new JsonSerializer<String>() { 19 @Override 20 public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) { 21 String substring = ""; 22 // if (src.contains("/nfsc/") || src.contains("nfsc/")) { 23 String[] split = src.split("\\|"); 24 List<String> strings = Arrays.asList(split); 25 StringBuffer stringBuffer = new StringBuffer(); 26 strings.forEach(a -> { 27 // CommonEnctyptUtils It's my encryption tool class 28 String aesResult = CommonEnctyptUtil.getAESResult(a, 3); 29 stringBuffer.append(aesResult).append("|"); 30 }); 31 String aesString = stringBuffer.toString(); 32 substring = aesString.substring(0, aesString.length() - 1); 33 // } 34 // The return value here is JsonElement You can create a JsonPrimitive Class ( json Metadata for) 35 // Here src String Type is json Of course JsonObject Also belong to json Metadata 36 // So you can return jsonObject 37 JsonPrimitive jsonPrimitive = new JsonPrimitive(substring); 38 return jsonPrimitive; 39 } 40 }; 41 // Register the custom serializer to GsonBuilder Of Gson Build classes 42 gsonBuilder.registerTypeAdapter(String.class, serializer); 43 // Create at this time Gson 44 Gson customGson = gsonBuilder.create(); 45 Object exResponse = ""; 46 List<Map<String, Object>> list = new ArrayList<>(); 47 Map<String, Object> map = new HashMap<>(); 48 // map.put("test", "/nfsc/|/nfsc/|/nfsc/"); 49 map.put("test", "/nfsc/"); 50 map.put("number", 1); 51 list.add(map); 52 System.out.println(list); 53 Object o = JSONObject.toJSON(list); 54 String s = o.toString(); 55 if (s.contains("/nfsc/") || s.contains("nfsc/")) { 56 // convert to json String operation 57 String customJSON = customGson.toJson(list); 58 ObjectMapper objectMapper = new ObjectMapper(); 59 // Anti serialization into objects 60 exResponse = objectMapper.readValue(customJSON, Object.class); 61 } 62 System.out.println(exResponse); 63 } 64 65 66 }
The first article is relatively simple, and the comments are OK. My goal is to be a strict man. If you have any good suggestions, you can give them to me, exchange and learn from each other. Thank you!