0

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Have you given the arrays a value with `new Line[1]` or something similar? After that you fill in the `Line` object(s). – Lee Meador May 06 '13 at 22:36
  • The arrays are filled with value. I can serialize them separately, but I would like to generate the JSON from my object containing them. – Lajos Arpad May 07 '13 at 00:01

1 Answers1

3

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);
}
opyate
  • 5,388
  • 1
  • 37
  • 64
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175