Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to decode VALUE_STRING as base64

Tags:

java

jackson

I am trying to Deserialize a json using fasterxml jackson , one of the fields in Json is a string but i need to read the same in a byte array PFB the Bean and main class

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);
    }
}

My json

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

At the time of Deserialize getting Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '"' (code 0x22) in base64 content Kindly suggest

like image 471
Anmol Bhaskar Avatar asked Sep 15 '26 18:09

Anmol Bhaskar


1 Answers

If using Java 8 : In my Deserialize implementation i needed to return

return (Base64.getEncoder().encode(jsonParser.getText().getBytes()))
like image 108
Anmol Bhaskar Avatar answered Sep 17 '26 06:09

Anmol Bhaskar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!