我正在尝试使用fastxml jackson反序列化json,Json中的一个字段是一个字符串但我需要在字节数组中读取相同的PFB 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"
}
在反序列化获取时 线程“main”中的异常com.fasterxml.jackson.databind.JsonMappingException:无法将VALUE_STRING解码为base64(MIME-NO-LINEFEEDS):base64内容中的非法字符“”(代码0x22) 请建议
答案 0 :(得分:1)
如果使用Java 8:在我的Deserialize实现中,我需要返回
return (Base64.getEncoder().encode(jsonParser.getText().getBytes()))