* @param indentDepth How many spaces to use for each indent level. Should be one to eight.
*
* @throws JSONException Thrown on IO errors during serialization.
*/
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();
if (!StringWriter.class.isAssignableFrom(writerClass) &&
!CharArrayWriter.class.isAssignableFrom(writerClass) &&
!BufferedWriter.class.isAssignableFrom(writerClass)) {
writer = new BufferedWriter(writer);
}
if (indentDepth > 0) {
serializer = new SerializerVerbose(writer, indentDepth);
} else {
serializer = new Serializer(writer);
}
try {
serializer.writeArray(this);
} catch (IOException iox) {
JSONException jex = new JSONException("Error occurred during input read.");
jex.initCause(iox);
throw jex;
}