1

Is there a commonly-used library that has classes for representing json in a type-safe way? I'm looking for one where json objects are stored in a Map and json arrays are in a List.

What I want to do is something like this:

myJson.get("myInt").asInt();
myJson.get("myString").asString();

.get() should return a missing node object so null checks aren't required.

Jackson is a really nice library that does all of this, but the Jackson JsonObject and JsonArray classes do not implement Map and List.

4
  • JSON is not type safe, you have to convert it into whatever type you want. mkyong.com/java/how-to-convert-java-map-to-from-json-jackson Commented Dec 12, 2014 at 17:59
  • Several of the JSON kits for Java have, eg, JSONObject and JSONArray classes for the objects and arrays. But of course you need to query the type before you ask for it, if you don't know the layout of the JSON. Commented Dec 12, 2014 at 19:14
  • Look, eg, at json.org/java/index.html Commented Dec 12, 2014 at 19:15
  • Or perhaps json.simple, since it uses Maps and Lists directly: code.google.com/p/json-simple Commented Dec 12, 2014 at 19:29

1 Answer 1

1

You have to convert your JSON into the desired type. The following example converts it into a Map<String,String>. If it can't be converted, it will throw an exception.

public class JsonMapExample {
    public static void main(String[] args) {

        String json = "{\"name\":\"mkyong\", \"age\":\"29\"}";

        Map<String,String> map = new HashMap<String,String>();
        ObjectMapper mapper = new ObjectMapper();

        try {       
            map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){});

        } catch (JsonMappingException e) {
            e.printStackTrace();
        }
    }
}

That's typically what happens when sending serialized objects over the network, you lose type safety when marshalling it, and add it back in on the other end by unmarshalling it.

See:

Sign up to request clarification or add additional context in comments.

5 Comments

Does not answer the question.
@ccleve Please be more detailed in how it does not answer the question. I think it does by saying it is not possible. My point is that there is no way to make a JsonObject or JsonArray be type safe, JSON is a string serialization, therefore inherently not type safe, you can workaround it with marshalling into type safe classes. By the way, asking for library suggestions is one of the reasons for closing a question, I am showing you how to add type safety using the library you mentioned.
It's very possible. If Jackson objects implemented the Map and List interfaces, they would meet the criteria. Jackson is also type safe. If you call node.asInt() or asText() and the node is not of the right type, it will do a conversion for you. It also handles nulls by returning a MissingNode.
They could implement the Map/List interface, but I don't see how that would help you get type safety, they would be Maps/Lists that you would need to cast anything you get out of it
Map<String, Node> map; Node node = map.get("foo"); String str = node.asString().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.