- 53
- 0
我有一个类如下:
@CompileStatic
@JsonSerialize(using = JsonSerializer)
@JsonDeserialize(using = JsonDeserializer)
class SensitiveString {
private String rawString
SensitiveString(String raw) {
rawString = raw
}
Object asType(Class clazz) {
assert clazz == String: "Cannot cast Sensitive String to type other than string. "
return this.toString()
}
@Override
String toString() {
return rawString
}
boolean equals(SensitiveString other){
return other.rawString == rawString
}
static class JsonSerializer extends StdSerializer<SensitiveString> {
JsonSerializer() {
super(SensitiveString.class)
}
@Override
void serialize(SensitiveString value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (value == null || value.rawString == null) {
gen.writeNull()
return
}
gen.writeString(SensitiveDataGuard.INSTANCE.encryptData(value.rawString))
}
}
static class JsonDeserializer extends StdDeserializer<SensitiveString> {
JsonDeserializer() {
super(SensitiveString.class)
}
@Override
SensitiveString deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String encrypted = p.readValueAs(String)
if (encrypted == null)
return null
return new SensitiveString(SensitiveDataGuard.INSTANCE.decryptData(encrypted))
}
}
}
然后我有一个类,里面用到这个SensitiveString
class User{
String name;
SensitiveString passed;
...
}
然后我用redis做mybatis二级缓存,json解析用的GenericJackson2JsonRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer())
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer())
redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer())
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer())
当mapper里有listUsers()方法时,mybatis需要把一个List<User>序列化加到二级缓存中去,序列化的时候出错,错误信息是
[Type id handling not implemented for type mypackage.SensitiveString (by serializer of type mypackage.SensitiveString$JsonSerializer) (through reference chain: java.util.ArrayList[0]->mypackage.User["passwd"])]
这种如何解决?
- 共 0 条
- 全部回答
-
谭煌 普通会员 1楼
Type ID handling is a feature that allows JavaScript to handle different types of data structures and values more efficiently. In JSON (JavaScript Object Notation), type ID handling is implemented by defining a custom object called "TypeID" or "TID". This object provides information about the type of a value and can be used to map values of different types to a specific type ID.
When you define a type ID for a value in JSON, you assign a unique value to it, which can be used to represent the type of the value. Here's an example of how you might define a type ID for a string in JavaScript:
javascript let stringTypeID = new TID("string");In this example,
stringTypeIDis a new instance of theTIDclass, which represents a string. The type ID is assigned a value of"string".Once you have defined a type ID for a value, you can use it in JSON syntax to specify the type of the value. Here's an example of how you might use the
stringTypeIDto deserialize a JSON string into a string value:```javascript let jsonString = '{"name": "John", "age": 30, "stringTypeID": "string"}'; let deserializedString = JSON.parse(jsonString);
console.log(deserializedString.name); // Output: John console.log(deserializedString.stringTypeID); // Output: string ```
In this example,
JSON.parse(jsonString)is used to parse the JSON string into a JavaScript object. TheJSON.parse()method takes a JSON string as its argument, and returns a new JavaScript object or throws an error if the string cannot be parsed.Once you have a JSON string with a type ID, you can use the type ID to access the associated value in your JavaScript code. Here's an example of how you might use the
stringTypeIDto access thenamevalue in the deserialized JSON object:```javascript let deserializedString = JSON.parse(jsonString); let name = deserializedString.name; // Output: John
console.log(name); // Output: John ```
In this example,
deserializedString.nameis assigned the value of thenamekey in the deserialized JSON object, which is"John". Thenameproperty is accessed using the type ID, which is retrieved from thestringTypeIDobject.
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

