-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make JSONObject.write(Writer writer, int indentFactor, int indent) public #184
Comments
JSONObject.write(Writer, int, int) outputs the contents of the JSONObject to a Writer. I don't think you could use it, per your example, to output an int value not already contained by the JSONObject to a Writer. If you just want a convenient way to output a JSONObject to a Writer, then JSONObject.write(Writer) is already available for that purpose, without the need to pass indent params. But you may not be referring to the JSONObject when you say, "send formatted JSON to a stream". Can you clarify your use case with a code fragment that shows how your code works now, and how you would like it to work? |
I believe @jpfiset is trying to do something similar to this: protected static final int TRACE_INDENT = 1;
protected static void writeJsonObject(final HttpServletResponse response, final JSONObject jsonObject)
throws IOException, JSONException {
try (final PrintWriter writer = response.getWriter()) {
response.setContentType("application/json");
try {
if (LOG.isTraceEnabled()) {
jsonObject.write(writer, TRACE_INDENT);
} else {
jsonObject.write(writer);
}
} catch (final JSONException ex) {
..... I modified my local copy to have this public method. public Writer write(final Writer writer, final int indentFactor) throws JSONException {
return this.write(writer, indentFactor, 0);
} @jpfiset request isn't to output an |
To be clear, I don't think it makes sense to make the 3 parameter method public. Having a new public method like I did is probably a slightly cleaner solution. |
Seems straightforward. No objection here to expanding the API in that manner. Let's see if anyone else has an opinion. |
@johnjaylward, thanks for the explanation. |
See #185 |
Would it be possible to change the signature of JSONObject.write(Writer writer, int indentFactor, int indent) to public?
Currently, if I which to send formatted JSON to a stream, I need to first convert it to a String (toString(3)) and then send the string to the stream. It would be more efficient to send directly to the stream.
The text was updated successfully, but these errors were encountered: