Domanda

I am using Gson to serialize/deserialize a class that contains a static nested class. The class looks like below.

public class ClassA {
private NestedClass nestedClass;

public NestedClass getNestedClass() {
return nestedClass;
}

public void setNestedClass(NestedClass nestedClass) {
this.nestedClass = nestedClass;
}

public static class NestedClass implements Serializable {

public NestedClass() {
}
}
}

The serialization works fine, but when i am trying to deserialize the json string back into an object i get the following SecurityException.

java.lang.SecurityException: Can not make a java.lang.Class constructor accessible
    at java.lang.reflect.AccessibleObject.setAccessible0(Unknown Source)
    at java.lang.reflect.AccessibleObject.setAccessible(Unknown Source)
    at com.google.gson.MappedObjectConstructor.getNoArgsConstructor(MappedObjectConstructor.java:86)
    at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:63)
    at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:54)
    at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:42)
    at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:55)

The exception seems to be coming from Java while trying to reflect the inner class. Has anyone been in that situation before?

Any help appreciated.

È stato utile?

Soluzione

It appears you're running Gson on a JVM with a SecurityManager enabled. You'll need to disable the security manager to use Gson.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top