Jackson Custom Mapper to convert byte array to string

I am incredibly new to Jackson, and I have a problem understanding how I can do something.

I have type data byte[](data is inside classes created from JAXB). Before the data is sent to the browser, Jackson (I suppose) will turn it into JSON so that the web page can use it. At least this is my gross understanding, so far.

The JSON data shows my lines byte []as lines that do not match the view we are looking at. For example, actual data may be CAFEDEAD, but the JSON string looks like 3q2+78r+. I would like JSON to contain a stringCAFEDEAD

My question is: can I write something common for Jackson, before he creates the final JSON, turn the data byte[]into a readable sixth line? Or, if not, what other options do I have?

I have access to javascript, so if there is a way to return a JSON string, I am also for that.

+5
source share
4 answers

Jackson converts byte [] to Base64 encoded binary data. This is a safe way to transfer binary content. Otherwise, there is no way to find out which character encoding can be used by the data contained, so trying to build a String from it would be risky and error prone.

Thus, the easiest way would be to convert the contents of the base64 database to binary data.

(hex, base85), .

+3

. EclipseLink JAXB (MOXy) JAXB (JSR-222).

, MOXy JSON-.

Java

JAXB byte[] base64Binary. HexBinaryAdapter, hexBinary.

package forum15643723;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    private byte[] foo;

    @XmlJavaTypeAdapter(HexBinaryAdapter.class)
    private byte[] bar;

}

Demo

- JSON , JSON.

package forum15643723;

import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum15643723/input.json");
        Root root = (Root) unmarshaller.unmarshal(json, Root.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

jaxb.properties

MOXy JAXB, jaxb.properties , (. http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

input.json/

foo bar . foo base64Binary bar hexBinary.

{
   "foo" : "3q2+78r+",
   "bar" : "DEADBEEFCAFE"
}

+1

- , . JSON / Jackson , ObjectMapper Stringfiyed JSON,

"{"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"}"

, Javascript , , JSON, jQuery.

    ... 
    var myJson = JSONString.parse() | jQuery.parseJSON(JSONString);
    ..

   myJson.age;  //29
   myJson.name; //"mkyong"

parse() , jQuery, , . , , jQuery.

,

- , . , . , Base64 . , .

, .

,

SERVER
  1) Jackson creates JSON String
  2) Server encodes into Base64
  3) Send

CLIENT
  1) Receive
  2) Decode base64
  3) parse JSON String
0

You can use @JsonSerialize and @JsonDeSerialize annotations for any property. javadoc is in

 http://jackson.codehaus.org/1.2.1/javadoc/org/codehaus/jackson/map/annotate/JsonSerialize.html

I personally would use the serialization / deserialization method rather than client-side conversion, as you are likely to end up doing this in several places.

-1
source

All Articles