Skip to content

Commit

Permalink
Adds minor change and 2 properties to enable indented pretty-print JS…
Browse files Browse the repository at this point in the history
…ON response and to set indentation space count.

Handy for development and easily legible curl json responses in console.
Properties:
	er.rest.format.ERXJSONRestWriter.shouldPrettyPrint=false (set to true to enable)
	er.rest.format.ERXJSONRestWriter.prettyPrintIndent=2 (or your preferred indent)
  • Loading branch information
kierankelleher committed Apr 26, 2013
1 parent 45d324d commit 5deaf86
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package er.rest.format;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import er.extensions.foundation.ERXProperties;
import er.rest.ERXRestContext;
import er.rest.ERXRestRequestNode;
import er.rest.ERXRestUtils;

/**
* @property <code>er.rest.format.ERXJSONRestWriter.shouldPrettyPrint</code> Boolean property to enable pretty-printing of JSON response. Defaults to false.
* @property <code>er.rest.format.ERXJSONRestWriter.prettyPrintIndent</code> Integer property to set the pretty print indentation space count. Defaults to <code>2</code>.
*/
public class ERXJSONRestWriter implements IERXRestWriter {

// Lazily initialized static constants
private static class CONSTANTS {
static boolean SHOULD_PRETTY_PRINT = ERXProperties.booleanForKeyWithDefault("er.rest.format.ERXJSONRestWriter.shouldPrettyPrint", false);
static int PRETTY_PRINT_INDENT = ERXProperties.intForKeyWithDefault("er.rest.format.ERXJSONRestWriter.prettyPrintIndent", 2);
}

public ERXJSONRestWriter() {
}

Expand Down Expand Up @@ -37,7 +50,9 @@ else if (ERXRestUtils.isPrimitive(object)) {
response.appendContentString(String.valueOf(object));
}
else {
response.appendContentString(JSONSerializer.toJSON(object, configWithContext(context)).toString());
JSON jsonObject = JSONSerializer.toJSON(object, configWithContext(context));
String json = (CONSTANTS.SHOULD_PRETTY_PRINT ? jsonObject.toString(CONSTANTS.PRETTY_PRINT_INDENT) : jsonObject.toString());
response.appendContentString(json);
}
response.appendContentString("\n");
}
Expand Down

0 comments on commit 5deaf86

Please sign in to comment.