Jas
Jas

Reputation: 15103

Java JSON serialization and JSONObject

I tried this:

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;
import org.apache.commons.lang.Validate;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.print.attribute.standard.Media;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.io.Serializable;

public static class MyJSON implements Serializable {
    private final String name = "myname";

    // **Why don't I get this field serialized in the response?**
    private final JSONObject jsonObject = new JSONObject();

    public MyJSON() {
        try {
            jsonObject.put("mykey", "myvalue");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getName() { return name; }
    public JSONObject getJsonObject() { return jsonObject; }

}

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all entities", notes = "get all entities", response = Response.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK status"),

public Response getList() {

    return Response.ok(new MyJSON(), MediaType.APPLICATION_JSON).build();
}

the response I get:

{
  "name": "myname"
}

as you see I get only the name field of MyJSON without the jsonObject field.

any ideas how can I get the jsonObject fields also serialized?

UPDATE:

after reading Thomas comment I tried using a map:

public static class MyJSON implements Serializable {
    private final String name = "myname";
    private final Map somefield = new HashMap();

    public String getName() { return name; }
    public Map getSomefield() { return somefield; }

    public void addOther(String key, String value) {
        somefield.put(key, value);
    }

}

        MyJSON myJSON = new MyJSON();
        myJSON.addOther("mhykey", "myvalue");
        return Response.ok(myJSON, MediaType.APPLICATION_JSON).build();

Now I get again:

{
  "name": "myname" // where is the other field? (the map)
}

I wonder again why doesn't it serializes it? please note I can't use specific objects because the json can vary at one scenario certain fields at another scenario other fields, I can't create a new class for each such case.

Upvotes: 2

Views: 10793

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191728

If this is how you want the class to be serialized

{
    "name": "value",
    "mykey": "myvalue"
}

Then this is how your object should look like

class Data { 
    String name, String mykey;

    // getters, setters...
}

Alternatively, when @Thomas said, a HashMap, he did not mean "nest" a HashMap into the Object, he literally meant use a HashMap, though, not all JSON libraries support that constructor.

HashMap<String, String> data = new HashMap<String, String>(); 
data.put("name", "value");
data.put("mykey", "myvalue");

JSONObject json = new JSONObject(data);
String jsonString = json.toString();

Another thing you could do is just treat your object as a JSONObject itself.

class Data extends JSONObject {

    public Data() { }

}
Data d = new Data();
d.put("name", "value");

Though, that seems silly.

Upvotes: 3

Related Questions