Skip to content

Commit f2a0eed

Browse files
killerswanerickt
authored andcommitted
---
yaml --- r: 28194 b: refs/heads/try c: 272c5ab h: refs/heads/master v: v3
1 parent 533974b commit f2a0eed

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
5-
refs/heads/try: d47cb101bf8627025345d17a5b0e9bfb6c2d01fa
5+
refs/heads/try: 272c5ab0e9573e6e85a1d576a3c95b5213cf5c61
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df

branches/try/src/libstd/json.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import io::WriterUtil;
1212
import map;
1313
import map::hashmap;
1414
import map::map;
15+
import sort;
1516

1617
export Json;
1718
export Error;
1819
export to_writer;
20+
export to_writer_pretty;
1921
export to_str;
22+
export to_str_pretty;
2023
export from_reader;
2124
export from_str;
2225
export eq;
@@ -89,16 +92,16 @@ fn to_writer(wr: io::Writer, j: Json) {
8992
/// Serializes a json value into a io::writer
9093
fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
9194
fn spaces(n: uint) -> ~str {
92-
let ss = ~"";
93-
n.times { str::push_str(ss, " "); }
95+
let mut ss = ~"";
96+
for n.times { str::push_str(ss, " "); }
9497
return ss;
9598
}
9699

97100
match j {
98101
Num(n) => wr.write_str(float::to_str(n, 6u)),
99102
String(s) => wr.write_str(escape_str(*s)),
100103
Boolean(b) => wr.write_str(if b { ~"true" } else { ~"false" }),
101-
List(v) => {
104+
List(vv) => {
102105
// [
103106
wr.write_str(spaces(indent));
104107
wr.write_str("[ ");
@@ -108,7 +111,7 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
108111
// elem ]
109112
let inner_indent = indent + 2;
110113
let mut first = true;
111-
for (*v).each |item| {
114+
for (*vv).each |item| {
112115
if !first {
113116
wr.write_str(~",\n");
114117
wr.write_str(spaces(inner_indent));
@@ -120,7 +123,16 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
120123
// ]
121124
wr.write_str(~" ]");
122125
}
123-
Dict(d) => {
126+
Dict(dd) => {
127+
// convert from a dictionary
128+
let mut pairs = ~[];
129+
for dd.each |key, value| {
130+
vec::push(pairs, (key, value));
131+
}
132+
133+
// sort by key strings
134+
let sorted_pairs = sort::merge_sort(|a,b| *a <= *b, pairs);
135+
124136
// {
125137
wr.write_str(spaces(indent));
126138
wr.write_str(~"{ ");
@@ -130,7 +142,8 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
130142
// k: v }
131143
let inner_indent = indent + 2;
132144
let mut first = true;
133-
for d.each |key, value| {
145+
for sorted_pairs.each |kv| {
146+
let (key, value) = kv;
134147
if !first {
135148
wr.write_str(~",\n");
136149
wr.write_str(spaces(inner_indent));

0 commit comments

Comments
 (0)