* This is an alternate indent style to using tabs.
* @param writer The writer which to write the JSON text to.
* @param indentDepth How many spaces to use for each indent. The value should be between one to eight.
*/
public Writer write(Writer writer, int indentDepth) throws JSONException {
Serializer serializer;
if (indentDepth < 1) {
indentDepth = 0;
} else if (indentDepth > 8) {
indentDepth = 9;
}
//Try to avoid double-buffering or buffering in-memory
//writers.
Class writerClass = writer.getClass();
boolean flushIt = false;
if (!StringWriter.class.isAssignableFrom(writerClass) &&
!CharArrayWriter.class.isAssignableFrom(writerClass) &&
!BufferedWriter.class.isAssignableFrom(writerClass)) {
writer = new BufferedWriter(writer);
flushIt = true;
}
if (indentDepth > 0) {
serializer = new SerializerVerbose(writer, indentDepth);
} else {
serializer = new Serializer(writer);
}
try {
serializer.writeObject(this);
} catch (IOException iox) {
JSONException jex = new JSONException("Error occurred during input read.");
jex.initCause(iox);
throw jex;
}