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

JSONObject(2); #356

Closed
RMelanson opened this issue Jul 15, 2017 · 7 comments
Closed

JSONObject(2); #356

RMelanson opened this issue Jul 15, 2017 · 7 comments

Comments

@RMelanson
Copy link

JSONObject(2) does not indent for example:
System.out.println("jsonObject.toString(2) = \n"+jsonObject.toString(2));
Prints Out:
{"TABLE":{"yhoo":{}}}

Should be:
{
"TABLE":{
"yhoo":{
}
}
}

@stleary
Copy link
Owner

stleary commented Jul 15, 2017

Please provide a complete example. Also, in the 'should be' section, use markdown styling to retain your preferred spacing (use 3 grave accent AKA backtick ` chars in a row, before and after your output)

@RMelanson
Copy link
Author

RMelanson commented Jul 15, 2017 via email

@johnjaylward
Copy link
Contributor

```java
public static final void someFunction(){
    // do something
}
```

generates

public static final void someFunction(){
    // do something
}
```json
{
    "int": 0,
    "string": "string",
    "bool": false
}
```

generates

{
    "int": 0,
    "string": "string",
    "bool": false
}

@johnjaylward
Copy link
Contributor

johnjaylward commented Jul 15, 2017

@stleary Here is a test case:

JSONObject jo = new JSONObject().put("TABLE", new JSONObject().put("yhoo", new JSONObject()));
assertEquals("toString(2)","{\n" + 
        "  \"TABLE\": {\n" + 
        "    \"yhoo\": {\n" + 
        "    }\n" + 
        "  }\n" + 
        "}", jo.toString(2));

and it currently fails like so:

org.junit.ComparisonFailure: toString(2) expected:<["{
  ]TABLE": {
    "yhoo"...> but was:<[{
  "]TABLE": {
    "yhoo"...>

The JUNIT error logging is making it look weird though. It's miss aligning it's surrounding brackets.

I believe that @RMelanson was expecting output like this:

{
  "TABLE": {
    "yhoo": {
    }
  }
}

however, in https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2260 we have a check for a single element in an object. If it's a single key object, no indenting happens

The following test passes on current master:

JSONObject jo = new JSONObject().put("TABLE", new JSONObject().put("yhoo", new JSONObject()));
assertEquals("toString(2)","{\"TABLE\": {\"yhoo\": {}}}", jo.toString(2));

@johnjaylward
Copy link
Contributor

@RMelanson If you read my last comment on GitHub directly (I edited it a lot which doesn't go through emails) you'll see that this looks like expected behavior. When a JSONArray or JSONObject has only one element or key, then the indentation does not take effect.

@stleary I don't mind changing this, but I think updating the toString and write javadocs would be sufficient to indicate the behavior.

@stleary
Copy link
Owner

stleary commented Jul 15, 2017

@johnjaylward thanks for doing the analysis. Agree this is a case of works-as-designed, not an error. Javadoc update would be a reasonable way to address the issue.

@stleary
Copy link
Owner

stleary commented Jul 15, 2017

@RMelanson thanks for making the issue known. No objection if someone wants to create a pull request to document the behavior.

@stleary stleary closed this as completed Jul 15, 2017
johnjaylward added a commit to johnjaylward/JSON-java that referenced this issue Jul 15, 2017
stleary added a commit to stleary/JSON-Java-unit-test that referenced this issue Jul 15, 2017
stleary added a commit that referenced this issue Jul 15, 2017
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