Pretty print JSON: sorting object keys

This commit is contained in:
Kevin Cantu 2012-08-30 17:38:07 -07:00 committed by Erick Tryzelaar
parent d47cb101bf
commit 272c5ab0e9

View File

@ -12,11 +12,14 @@ import io::WriterUtil;
import map;
import map::hashmap;
import map::map;
import sort;
export Json;
export Error;
export to_writer;
export to_writer_pretty;
export to_str;
export to_str_pretty;
export from_reader;
export from_str;
export eq;
@ -89,8 +92,8 @@ fn to_writer(wr: io::Writer, j: Json) {
/// Serializes a json value into a io::writer
fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
fn spaces(n: uint) -> ~str {
let ss = ~"";
n.times { str::push_str(ss, " "); }
let mut ss = ~"";
for n.times { str::push_str(ss, " "); }
return ss;
}
@ -98,7 +101,7 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
Num(n) => wr.write_str(float::to_str(n, 6u)),
String(s) => wr.write_str(escape_str(*s)),
Boolean(b) => wr.write_str(if b { ~"true" } else { ~"false" }),
List(v) => {
List(vv) => {
// [
wr.write_str(spaces(indent));
wr.write_str("[ ");
@ -108,7 +111,7 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
// elem ]
let inner_indent = indent + 2;
let mut first = true;
for (*v).each |item| {
for (*vv).each |item| {
if !first {
wr.write_str(~",\n");
wr.write_str(spaces(inner_indent));
@ -120,7 +123,16 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
// ]
wr.write_str(~" ]");
}
Dict(d) => {
Dict(dd) => {
// convert from a dictionary
let mut pairs = ~[];
for dd.each |key, value| {
vec::push(pairs, (key, value));
}
// sort by key strings
let sorted_pairs = sort::merge_sort(|a,b| *a <= *b, pairs);
// {
wr.write_str(spaces(indent));
wr.write_str(~"{ ");
@ -130,7 +142,8 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
// k: v }
let inner_indent = indent + 2;
let mut first = true;
for d.each |key, value| {
for sorted_pairs.each |kv| {
let (key, value) = kv;
if !first {
wr.write_str(~",\n");
wr.write_str(spaces(inner_indent));