4

I'm using JDK 1.8.0_101 and added javax.json.jar to classpath. The below compiles just fine but throws an error. It looks like the remove() method has not been implemented.

public class Test{
    public static void main(String[] args) {
        javax.json.JsonObject o = javax.json.Json.createObjectBuilder().add("a", 1).add("b", 2).build();
        try {
            o.remove("a");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This is the output:

java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$1.remove(Collections.java:1664)
    at java.util.AbstractMap.remove(AbstractMap.java:254)
    at Test.main(Test.java:5)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

How can I fix it or when can I expect this method to be implemented? Am I using an old version of the javax.json library?

fabiog1901
  • 352
  • 3
  • 12
  • Why would you want to remove an object attribute when you can just ignore it? – Kadima Aug 18 '16 at 15:26
  • the object has to be sent to another app and doesn't have to have that info. – fabiog1901 Aug 18 '16 at 15:27
  • 2
    You can't expect that to happen, ever, since JsonObject is clearly [documented](http://docs.oracle.com/javaee/7/api/index.html?javax/json/JsonObject.html) as immutable. The javadoc even says: *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.* – JB Nizet Aug 18 '16 at 15:28
  • fair enough. Then what does that method do? How can I then go around it? I mean, in a Map you can remove key-value pairs.. – fabiog1901 Aug 18 '16 at 15:30
  • refer this - http://stackoverflow.com/questions/15603033/removing-elements – NageN Aug 18 '16 at 15:32
  • 2
    No, not if the map is immutable. Like for example if you use `Collections.unmodifiableMap(someOtherMap)` (which JsonObject is using). You can just create another JSON object containing all the entries of the original JSON object except the one you don't want. – JB Nizet Aug 18 '16 at 15:33

2 Answers2

3

You can remove any element by using JsonObjectBuilder.

For example:

public class Test{
    public static void main(String[] args) {
        javax.json.JsonObject full = javax.json.Json.createObjectBuilder().add("a", 1).add("b", 2).build();
        full = javax.json.Json.createObjectBuilder(full).remove("a").build();
        System.out.println(full); // return {"b":2}
     }
}
Red fx
  • 1,071
  • 2
  • 12
  • 26
2

JsonObject is immutable. So, you can create a new object with or without a property.

To remove a property:

public static JsonObject removeProperty(JsonObject origin, String key){
    JsonObjectBuilder builder = Json.createObjectBuilder();

    for (Map.Entry<String,JsonValue> entry : origin.entrySet()){
        if (entry.getKey().equals(key)){
            continue;
        } else {
            builder.add(entry.getKey(), entry.getValue());
        }
    }       
    return builder.build();
}

And to add a new property:

public static JsonObject addProperty(JsonObject origin, String key, String value){
    JsonObjectBuilder builder = Json.createObjectBuilder();

    for (Map.Entry<String,JsonValue> entry : origin.entrySet()){
        builder.add(entry.getKey(), entry.getValue());          
    }       

    builder.add(key, value);

    return builder.build();
}
Anderson Marques
  • 808
  • 8
  • 13