账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    JSON解析问题:Type id handling not implemented for
    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"])] 
    

    这种如何解决?

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 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, stringTypeID is a new instance of the TID class, 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 stringTypeID to 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. The JSON.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 stringTypeID to access the name value 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.name is assigned the value of the name key in the deserialized JSON object, which is "John". The name property is accessed using the type ID, which is retrieved from the stringTypeID object.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部