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

better JSON output formatting and testing #35

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/main/org/hjson/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void save(JsonValue value, Writer tw, int level) throws IOException {
switch (value.getType()) {
case OBJECT:
JsonObject obj=value.asObject();
if (obj.size()>0) nl(tw, level);
tw.write('{');
for (JsonObject.Member pair : obj) {
if (following) tw.write(",");
Expand All @@ -56,8 +55,7 @@ public void save(JsonValue value, Writer tw, int level) throws IOException {
tw.write("\":");
//save(, tw, level+1, " ", false);
JsonValue v=pair.getValue();
JsonType vType=v.getType();
if (format && vType!=JsonType.ARRAY && vType!=JsonType.OBJECT) tw.write(" ");
if (format) tw.write(" ");
if (v==null) tw.write("null");
else save(v, tw, level+1);
following=true;
Expand All @@ -68,17 +66,14 @@ public void save(JsonValue value, Writer tw, int level) throws IOException {
case ARRAY:
JsonArray arr=value.asArray();
int n=arr.size();
if (n>0) nl(tw, level);
tw.write('[');
for (int i=0; i<n; i++) {
if (following) tw.write(",");
if (i > 0) tw.write(",");
JsonValue v=arr.get(i);
JsonType vType=v.getType();
if (vType!=JsonType.ARRAY && vType!=JsonType.OBJECT) nl(tw, level+1);
save(v, tw, level+1);
following=true;
nl(tw, level+1);
save(arr.get(i), tw, level+1);
}
if (following) nl(tw, level);
if (n > 0) nl(tw, level);
tw.write(']');
break;
case BOOLEAN:
Expand Down
9 changes: 3 additions & 6 deletions src/test/org/hjson/test/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,13 @@ private static boolean test(String name, String file, boolean inputCr, boolean o
String data1=data.toString(Stringify.FORMATTED);
String hjson1=data.toString(Stringify.HJSON);
if (!shouldFail) {
JsonValue result=JsonValue.readJSON(load(name+"_result.json", inputCr));
String json2 = load(name+"_result.json", outputCr);
JsonValue result=JsonValue.readJSON(json2);
String data2=result.toString(Stringify.FORMATTED);
String hjson2=load(name+"_result.hjson", outputCr);
if (!data1.equals(data2)) return failErr(name, "parse", data1, data2);
if (!hjson1.equals(hjson2)) return failErr(name, "stringify", hjson1, hjson2);

if (isJson) {
String json1=data.toString(), json2=JsonValue.readHjson(text, opt).toString();
if (!json1.equals(json2)) return failErr(name, "json chk", json1, json2);
}
if (!data1.equals(json2)) return failErr(name, "JSON stringify", data1, json2);
}
else return failErr(name, "should fail", null, null);
}
Expand Down