I have an existing jsonobject from the javax.json.JsonObject class.
I can't for the life of me figure out how I can modify the existing values in it. Ideally I'd like to do something like this:
if(object.getString("ObjectUUID").length()==0){
object.put("ObjectUUID", UUID.randomUUID().toString());
}
According to the API you aren't allowed to modify that map.
http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
This map object provides read-only access to the JSON object data, and attempts to modify the map, whether direct or via its collection views, result in an UnsupportedOperationException.
Currently I'm getting around the problem with a quick hack but there must be a better solution than this:
if(object.getString("ObjectUUID").length()==0){
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("ObjectUUID", UUID.randomUUID().toString());
for(String key : object.keySet()){
if(!key.equals("ObjectUUID")){
job.add(key, object.get(key));
}
}
object = job.build();
}
So the question how do you modify an existing jsonobject?