Skip to content

Commit

Permalink
nwn_gff: when outputting json, sort objects
Browse files Browse the repository at this point in the history
  • Loading branch information
niv committed Sep 21, 2018
1 parent 7dbf640 commit 4f674de
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/nwn_gff.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@ of "gff": state = input.readGffRoot(false)
of "json": state = input.parseJson(inputfile).gffRootFromJson()
else: quit("Unsupported informat: " & informat)

proc postProcessJson(j: JsonNode) =
## Post-process json before emitting: We make sure to re-sort.
if j.kind == JObject:
for k, v in j.fields: postProcessJson(v)
j.fields.sort do (a, b: auto) -> int: cmpIgnoreCase(a[0], b[0])
elif j.kind == JArray:
for e in j.elems: postProcessJson(e)

case outformat:
of "gff": output.write(state)
of "json":
let j = state.toJson()
postProcessJson(j)
output.write(if args["--pretty"]: j.pretty() else: $j)
output.write("\c\L")
else: quit("Unsupported outformat: " & outformat)

0 comments on commit 4f674de

Please sign in to comment.