1

I'm developing an application in Java that reads JSON from an API which returns values encoded in Base64. Here is a part of the JSON (actually, there are a lot of keys/values) :

{
    "id_element": "MjUxMTEz",
    "title": "VGVzdCB0aXRsZQ==",
    "description": "SSBrbmV3IHlvdSB3ZXJlIHZlcnkgY3VyaW91cyAhIEhhaGEgOkQ=",
    "picture": "aHR0cDovL3RoZWFydG1hZC5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTUvMDIvQ3VyaW91cy1HZW9yZ2UtV2FsbHBhcGVyLTcuanBn",
    "link": "",
    "id_categorie": "MTB5Nzc=",
    "active": "MQ==",
    "date_create": "MDAwMC0wMC0wMCAwMDowMDowMA=="
}

Is there a Java library that supports Base64 decode during deserialization or will I have to do this by myself ? I would like to have a Java object with the deserialized values from this JSON. Many thanks !

Mohamed Amine
  • 2,264
  • 1
  • 23
  • 36
  • refer here http://stackoverflow.com/questions/469695/decode-base64-data-in-java – Dinesh Kannan Apr 27 '15 at 09:12
  • @user1992200 Actually this is not my question. I need a library that decodes Base64 values during JSON deserialization with a JSON library, like Jackson or Gson. – Mohamed Amine Apr 27 '15 at 09:18

2 Answers2

0

Afaik there is no such library, since these are two distinct topics. Nevertheless it should be no big deal to do the decoding in a second step. That means it is more important to have a comfortable JSON library like Gson or Jackson.

NaN
  • 7,441
  • 6
  • 32
  • 51
0

You can use the apache commons codec http://commons.apache.org/proper/commons-codec/ and call the following method: byte[] org.apache.commons.codec.binary.Base64.decodeBase64(String base64String)

such as:

 /**
 * Decode string to image
 * @param encodedString The string to decode
 * @return decoded image
 */
public static BufferedImage decodeToImage(String encodedString) {

    BufferedImage image = null;
    byte[] imageByte;
    try {
        imageByte = Base64.decodeBase64(encodedString);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        image = ImageIO.read(bis);
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}
antinmaze
  • 63
  • 9