简体   繁体   中英

From org.json JSONObject to org.codehaus.jackson

I want to move from org.json to org.codehaus.jackson . How do I convert the following Java code?

private JSONObject myJsonMessage(String message){
    JSONObject obj = new JSONObject();
    obj.put("message",message);
    return obj;
}

I left out the try-catch block for simplicity.

Instead of JSONObject use Jackson's ObjectMapper and ObjectNode :

ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
node.put("message", "text");

This would be Jackson's equivalent of your current org.json code.

However, where Jackson really excels is in its capacity to do complex mappings between your Java classes (POJOs) and their JSON representation, as well as its streaming API which allows you to do really fast serialization, at least when compared with org.json 's counterparts.

There is no JSONObject in Jackson api. Rather than returning a JSONObject , you can either return a Map or a Java Bean with message property that has getters and setters for it.

public class MyMessage {
    private String message;

    public void setMessage(final String message) {
        this.message = message;
    }

    public String getMessage() {
        return this.message;
    }
}

So, your method will be reduced to:

private MyMessage(String message) {
    MyMessage myMessage = new MyMessage();
    myMessage.setMessage(message);
    return myMessage;
}

Another aspect of this change would be changing the serialization code, to convert MyMessage back to json string. Jackson does Java Beans, maps by default, you don't need to create a JSONObject eg,

private String serializeMessage(MyMessage message){

    //Note: you probably want to put it in a global converter and share the object mapper
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(message);
}

The above will return {message: "some message"}

I have skipped the exceptions for brevity.

If you want to upgrade from org.json library to Jackson piece by piece, and initially retaining same API, you might want to read " Upgrade from org.json to Jackson ". This would at least make your code about 3x faster for basic JSON reading and writing; plus you could -- if you so choose -- start converting processing, as Jackson makes it easy to convert between Trees and POJOs ( ObjectMapper.treeToValue(...) , valueToTree , convertValue between POJOs etc. etc).

Just keep in mind that tools that you are familiar with may bias your thinking to certain patterns, and keeping an open mind can help you find even better ones. In case of Jackson (or GSON or other mature Java JSON tools), you really should consider where proper data-binding could help, instead of using JSON-centered tree model (that org.json offers). Tree Models keep your thinking grounded to JSON structure, which is sometimes useful; but might also prevent you from seeing more natural patterns that come from defining POJO structure to reflect expected JSON, and operating on Java Objects directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM