无法将VALUE_STRING解码为base64格式。

3

我正在尝试使用fasterxml jackson反序列化json,其中一个字段是字符串,但我需要将其读取为字节数组,请参阅以下Bean和主类:

public class Serialization implements Serializable{

    private static final long serialVersionUID = 5894318390213780082L;
    private String name = null;

    @JsonDeserialize(using = StringtoByteArray.class)
    private byte[] pass = null;
//getter setter
}  

public class StringtoByteArray extends JsonDeserializer<byte []> {

    @Override
    public byte[] deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException, JsonProcessingException {
        return (jsonParser.getBinaryValue());
    }
}

public class App 
{
    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        Serialization sr = new Serialization();
        ObjectMapper mapper = new ObjectMapper();

        sr = mapper.readValue(new File("D:\\check.json"), Serialization.class);
        System.out.println("sr values ::" +sr.toString());

        mapper.writeValue(new File("c:\\user.json"), sr);
    }
}

我的Json

{
      "name": "AD",
      "pass": "pp"
}

反序列化时出现异常

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: 将VALUE_STRING解码为base64(MIME-NO-LINEFEEDS)失败:在base64内容中存在非法字符“"”(代码0x22)

请提供建议

1个回答

1
如果使用Java 8:在我的反序列化实现中,我需要返回
return (Base64.getEncoder().encode(jsonParser.getText().getBytes()))

1
最好始终指定字符集getBytes(StandardCharsets.UTF_8) - Joop Eggen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接