Skip to content

Commit

Permalink
don't print trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed Aug 6, 2024
1 parent 945acdc commit e134a6c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ the terms of this License".
A tiny JSON5 parsing library for Java 8, with a focus on simplicity and minimizing size.

## Usage
First, include the library in your project. You can do this by adding the following to your build.gradle(.kts):
First, include the library in your project. You can do this by adding the following to your `build.gradle(.kts)`:
<details>
<summary>Kotlin</summary>

Expand Down Expand Up @@ -90,14 +90,14 @@ This prints out:
// comment
"key": 4,
// look, arrays work too!
"arr": [ 1, 2, 3, ],
"arr": [ 1, 2, 3 ],
// and objects!
"obj": {
"key": "value",
"key": "value"
},
// comments can also
// be multiple lines
"null": null,
"null": null
}
```

Expand All @@ -121,7 +121,8 @@ public class Example {
public static void main(String[] args) {
Example example = new Example();
Map<String, ZsonValue> zson = Zson.obj2map(example);
System.out.println(new Zson().stringify(zson));
System.out.println(new Zson()
.withQuoteKeys(false).stringify(zson));
}
}
```
Expand All @@ -130,8 +131,8 @@ This prints out:
```json5
{
// This is a comment
"key": "value",
"number": 4,
key: "value",
number: 4
}
```

Expand Down
31 changes: 25 additions & 6 deletions src/main/java/dev/nolij/zson/Zson.java
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,15 @@ public void write(@NotNull Map<String, ZsonValue> data, @NotNull Path path) thro
public void write(@NotNull Map<String, ZsonValue> data, @NotNull Appendable output) throws IOException {
output.append("{\n");

boolean first = true;

for (var entry : data.entrySet()) {
if (first) {
first = false;
} else {
output.append(",\n");
}

ZsonValue zv = entry.getValue();
String comment = zv.comment;

Expand All @@ -1030,10 +1038,10 @@ public void write(@NotNull Map<String, ZsonValue> data, @NotNull Appendable outp
if (quoteKeys)
output.append('"');

output.append(": ").append(value(zv.value)).append(",\n");
output.append(": ").append(value(zv.value));
}

output.append("}");
output.append("\n}");
}

/**
Expand Down Expand Up @@ -1083,14 +1091,25 @@ private String value(Object value) {
StringBuilder output = new StringBuilder("[");
output.append(expandArrays ? "\n" : " ");

boolean first = true;

for (Object obj : iterableValue) {
if (expandArrays)
if (!first) {
output.append(",")
.append(expandArrays ? "\n" : " ");
} else {
first = false;
}

if (expandArrays) {
output.append(indent).append(indent);
output.append(value(obj).replace("\n", "\n" + indent + indent))
.append(",")
.append(expandArrays ? "\n" : " ");
}

output.append(value(obj).replace("\n", "\n" + indent + indent));
}

output.append(expandArrays ? "\n" : " ");

if (expandArrays)
output.append(indent);

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/NoJUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public static void main(String[] args) {
// The state of the address
"state": "IL",
// The zip code of the address
"zip": 62701,
"zip": 62701
},
// The phone numbers of the person
"phoneNumbers": {
"home": "217-555-1234",
"cell": "217-555-5678",
},
}
}""";

assert expected.equals(json);
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/ZsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public void testReadWrite() {
// The state of the address
"state": "IL",
// The zip code of the address
"zip": 62701,
"zip": 62701
},
// The phone numbers of the person
"phoneNumbers": {
"home": "217-555-1234",
"cell": "217-555-5678",
},
"cell": "217-555-5678"
}
}""";

assertEquals(expected, json);
Expand Down Expand Up @@ -190,7 +190,7 @@ public void testNumbers() {
"inf": Infinity,
"w": NaN,
"java": 3405691582,
"neginf": -Infinity,
"neginf": -Infinity
}""", new Zson().stringify(map));
}

Expand All @@ -204,7 +204,7 @@ public void testObject() {
"such": "amaze",
"very": true,
"constant": "wow",
"testEnum": "ONE",
"testEnum": "ONE"
}""";

String actual = new Zson().stringify(json);
Expand Down Expand Up @@ -306,7 +306,7 @@ public void testUnquotedKeys() {
such: "amaze",
very: true,
constant: "wow",
testEnum: "TWO",
testEnum: "TWO"
}""";

String actual = new Zson().withQuoteKeys(false).stringify(json);
Expand Down Expand Up @@ -382,7 +382,7 @@ public void testObjectFields() {
String expected = """
{
"a": 0,
"set": [ "a", "b", "c", ],
"set": [ "a", "b", "c" ],
"b": {
"bool": false,
"b": 0,
Expand All @@ -393,9 +393,9 @@ public void testObjectFields() {
"d": 0.0,
"c": "\\0",
"str": null,
"e": null,
"e": null
},
"c": "ONE",
"c": "ONE"
}""";

String actual = new Zson().stringify(json);
Expand Down

0 comments on commit e134a6c

Please sign in to comment.