2

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.

nikkatsa
  • 1,751
  • 4
  • 26
  • 43

1 Answers1

0

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

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • 1
    Hi Jesse thanks for your comment. The actual problem was well hidden in my code. The exception was actually comming when Gson was trying to serialize a field in my class of Type "Class extends AnotherClass>". It seems like Gson didn't know how to deserialize an object of Class. Thus when i registered a JsonDeserializer and a JsonSerializer it worked. – nikkatsa Nov 08 '12 at 10:37