Skip to content
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

Closed
jpfiset opened this issue Dec 21, 2015 · 6 comments
Closed

Comments

@jpfiset
Copy link

jpfiset commented Dec 21, 2015

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.

@stleary
Copy link
Owner

stleary commented Dec 21, 2015

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?

@johnjaylward
Copy link
Contributor

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 int, but to output the entire JSONObject to a non-string writer while specifying the indent size.

@johnjaylward
Copy link
Contributor

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.

@stleary
Copy link
Owner

stleary commented Dec 22, 2015

Seems straightforward. No objection here to expanding the API in that manner. Let's see if anyone else has an opinion.

@jpfiset
Copy link
Author

jpfiset commented Dec 24, 2015

@johnjaylward, thanks for the explanation.

@stleary
Copy link
Owner

stleary commented Dec 24, 2015

See #185

@stleary stleary closed this as completed Dec 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants