Malin
Malin

Reputation: 697

Json object in Map, how to get hold of it in java?

In an XPages application I have in a sessionScope variable the configuration for the application in JSON format.

I wonder how I can get hold of this configuration again in Java?

I tried:

Map<String, Object> sesScope = ExtLibUtil.getSessionScope();
if (null != sesScope.get("configJSON")){
    JsonJavaObject jsonObject = new JsonJavaObject();
    jsonObject = (JsonJavaObject) sesScope.get("configJSON");
    System.out.println("got object?");
    System.out.println("json " + jsonObject.toString());
}   

In the console I never get to the "got object?" print statement. What am I doing wrong?

Upvotes: 0

Views: 293

Answers (3)

Stanislaw Guzik
Stanislaw Guzik

Reputation: 244

The JavaScript method JSON.parse that you use, returns just a plain simple JavaScript object, not the JsonJavaObject. You can't use JavaScript object later in Java without additional unnecessary overhead. Don't use json2.jss, use JsonParser like Paul Withers have told.

Change your code that gets JSON from your Notes field:

#{javascript:
    importPackage(com.ibm.commons.util.io.json);
    var jsonText = doc.getItemValueString(itemname);
    var jsonFactory:JsonFactory = JsonJavaFactory.instanceEx;
    var jsonObject = JsonParser.fromJson(jsonFactory, jsonText);
    sessionScope.put('configJSON', jsonObject);
}

Modify your provided java code by removing unnecessary line:

Map<String, Object> sesScope = ExtLibUtil.getSessionScope();
if (null != sesScope.get("configJSON")) {
    JsonJavaObject jsonObject = (JsonJavaObject) sesScope.get("configJSON");
    System.out.println("got object?");
    System.out.println("json " + jsonObject.toString());
}

You should now be OK. Hint: if you use JsonJavaFactory.instance instead of JsonJavaFactory.instanceEx, you'll get java.util.HashMap instead of JsonJavaObject

Upvotes: 1

Vijendra Singh
Vijendra Singh

Reputation: 1

JsonJavaObject is your POJO java class ? If Yes then please use ObjectMapper of Fasterxml to map your JSON data to the fields of JsonJavaObject class.Use this method to map your json data to any POJO class

public final T validateJson(final Map<String, Object> jsonMap, final T temmplateClass) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // convert JSON string to Map
            String jsonString = objectMapper.writeValueAsString(jsonMap);
            return objectMapper.readValue(jsonString, (Class<T>) temmplateClass);
        } catch (JsonMappingException e) {
            throw new NestableRuntimeException(String.format(ApiCommonConstants.INVALID_JSON_FIELD,
                    e.getPath().get(e.getPath().size() - 1).getFieldName()));
        } catch (IOException e) {
            throw new NestableRuntimeException(e.getMessage(), e);
        }
    }

Upvotes: -1

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15739

Has the sessionScope variable been set? Use sesScope.containsKey("configJSON") to verify. If it's an empty JSON object it will be "{}" rather than null. If you're loading as JSON from a REST service or a Notes Document it may be a String rather than a JsonJavaObject. For parsing a String of JSON, you can use com.ibm.commons.util.io.json.JsonParser. The constructor for JsonJavaObject expects a Map, not sure if this is what's being passed in.

Upvotes: 1

Related Questions