Search code examples
javaclassserializationinner-classesflexjson

Serializing a static inner class with FlexJSON


I'm trying to serialize this inner class

static class functionMessage{
    String type = "function";
    String id;
    String function;
    Object parameters;

    public functionMessage(String ID, String Function, Boolean Parameters) {
        this.id = ID;
        this.function = Function;
        this.parameters = (Boolean) Parameters;
    }
}

with

new flexjson.JSONSerializer().exclude("*.class").serialize( 
    new functionMessage(
        "container", 
        "showContainer", 
        Boolean.TRUE
    ) 
) 

but only {} is retured.

If I try to add public before each member variable, it gives this error:

flexjson.JSONException: Error trying to deepSerialize  
Caused by: java.lang.IllegalAccessException: Class flexjson.BeanProperty can not access a member of class with modifiers "public"

I've tried to follow the example, but it doesn't show how Person is constructed, and I don't know how making the class a static inner class affects this as I'm still fairly new to Java.

I've also tried to read all of the solutions provided for that error given by Google but still nothing.

How can flexjson.JSONSerializer() return all of the member variables of a static inner class?


Solution

  • but only {} is retured.

    Apparently flexjson uses getters to resolve JSON elements. Your class doesn't have any.

    Just add corresponding getters

    public String getType() {
        return type;
    }
    
    public String getId() {
        return id;
    }
    
    public String getFunction() {
        return function;
    }
    
    public Object getParameters() {
        return parameters;
    }
    

    As for

    If I try to add public before each member variable, it gives this error:

    That's because your static class has default package access and its fields are therefore not visible to any class outside of the package its declared in.