Question

I have a Java class and I want to generate a JSON string from an object of the class. However, the members of the class are as follows:

/**
 * The set containing the footer texts
 */
public HeaderOrFooter[] footers = null;
/**
 * The title of the word cloud
 */
public HeaderOrFooter title;
/**
 * The subtitle of the word cloud
 */
public HeaderOrFooter subTitle;
/**
 * The set of rectangles to be drawn
 */
public Rectangle[] rectangles;
/**
 * The set of round rectangles to be drawn
 */
public RoundRectangle[] roundRectangles;
/**
 * The set of lines to be drawn
 */
public Line[] lines;
/**
 * The set of polygons to be drawn
 */
public Polygon[] polygons;
/**
 * The set of words to be drawn
 */
public Word[] words;

and my method which converts the object to JSON looks like this:

public String convertToJSON()
{
    flexjson.JSONSerializer jsonSerializer = new flexjson.JSONSerializer();
    jsonSerializer.exclude("class");
    jsonSerializer.exclude("subTitle.class");
    jsonSerializer.exclude("title.class");
    return jsonSerializer.serialize(this);
}

I am using flexjson and its JSONSerializer object. My problem is that it only converts the title and subTitle members to JSON, the arrays are not converted. Can somebody tell me how could I include the arrays to my JSON? Thanks.

Was it helpful?

Solution

I have figured it out, here it is the correct function:

// Re-using the serializer as per "Thread Safety and Reuse"
// section of http://flexjson.sourceforge.net/
public static final flexjson.JSONSerializer jsonSerializer;
static {
    jsonSerializer = new flexjson.JSONSerializer().exclude("*.class");
}

public String toJSON() {
    return jsonSerializer.deepSerialize(this);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top