Skip to content

Commit

Permalink
Pretty print JSON: indentation and newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
killerswan authored and erickt committed Aug 31, 2012
1 parent 536cb90 commit d47cb10
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/libstd/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,69 @@ 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, " "); }
return ss;
}

match j {
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) => {
// [
wr.write_str(spaces(indent));
wr.write_str("[ ");

// [ elem,
// elem,
// elem ]
let inner_indent = indent + 2;
let mut first = true;
for (*v).each |item| {
if !first {
wr.write_str(~",\n");
wr.write_str(spaces(inner_indent));
}
first = false;
to_writer_pretty(wr, item, inner_indent);
};

// ]
wr.write_str(~" ]");
}
Dict(d) => {
// {
wr.write_str(spaces(indent));
wr.write_str(~"{ ");
// { k: v,
// k: v,
// k: v }
let inner_indent = indent + 2;
let mut first = true;
for d.each |key, value| {
if !first {
wr.write_str(~",\n");
wr.write_str(spaces(inner_indent));
}
first = false;
let key = str::append(escape_str(key), ~": ");
let key_indent = str::len(key);
wr.write_str(key);
to_writer_pretty(wr, value, key_indent);
};
// }
wr.write_str(~" }");
}
Null => wr.write_str(~"null")
}
}

fn escape_str(s: ~str) -> ~str {
let mut escaped = ~"\"";
do str::chars_iter(s) |c| {
Expand All @@ -111,6 +174,11 @@ fn to_str(j: Json) -> ~str {
io::with_str_writer(|wr| to_writer(wr, j))
}

/// Serializes a json value into a string, with whitespace and sorting
fn to_str_pretty(j: Json) -> ~str {
io::with_str_writer(|wr| to_writer_pretty(wr, j, 0))
}

type Parser_ = {
rdr: io::Reader,
mut ch: char,
Expand Down

0 comments on commit d47cb10

Please sign in to comment.