Skip to content

Change json::to_xxx(&Json) functions to be json::Json::to_xxx(&self) #8679

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

Merged
merged 1 commit into from
Aug 24, 2013
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
124 changes: 60 additions & 64 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,26 +459,24 @@ impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
}
}

/// Encodes a json value into a io::writer
pub fn to_writer(wr: @io::Writer, json: &Json) {
let mut encoder = Encoder(wr);
json.encode(&mut encoder)
}

/// Encodes a json value into a string
pub fn to_str(json: &Json) -> ~str {
io::with_str_writer(|wr| to_writer(wr, json))
}
impl Json{
/// Encodes a json value into a io::writer. Uses a single line.
pub fn to_writer(&self, wr: @io::Writer) {
let mut encoder = Encoder(wr);
self.encode(&mut encoder)
}

/// Encodes a json value into a io::writer
pub fn to_pretty_writer(wr: @io::Writer, json: &Json) {
let mut encoder = PrettyEncoder(wr);
json.encode(&mut encoder)
}
/// Encodes a json value into a io::writer.
/// Pretty-prints in a more readable format.
pub fn to_pretty_writer(&self, wr: @io::Writer) {
let mut encoder = PrettyEncoder(wr);
self.encode(&mut encoder)
}

/// Encodes a json value into a string
pub fn to_pretty_str(json: &Json) -> ~str {
io::with_str_writer(|wr| to_pretty_writer(wr, json))
/// Encodes a json value into a string
pub fn to_pretty_str(&self) -> ~str {
io::with_str_writer(|wr| self.to_pretty_writer(wr))
}
}

pub struct Parser<T> {
Expand Down Expand Up @@ -1307,7 +1305,10 @@ impl<A:ToJson> ToJson for Option<A> {
}

impl to_str::ToStr for Json {
fn to_str(&self) -> ~str { to_str(self) }
/// Encodes a json value into a string
fn to_str(&self) -> ~str {
io::with_str_writer(|wr| self.to_writer(wr))
}
}

impl to_str::ToStr for Error {
Expand Down Expand Up @@ -1358,69 +1359,67 @@ mod tests {

#[test]
fn test_write_null() {
assert_eq!(to_str(&Null), ~"null");
assert_eq!(to_pretty_str(&Null), ~"null");
assert_eq!(Null.to_str(), ~"null");
assert_eq!(Null.to_pretty_str(), ~"null");
}


#[test]
fn test_write_number() {
assert_eq!(to_str(&Number(3f)), ~"3");
assert_eq!(to_pretty_str(&Number(3f)), ~"3");
assert_eq!(Number(3f).to_str(), ~"3");
assert_eq!(Number(3f).to_pretty_str(), ~"3");

assert_eq!(to_str(&Number(3.1f)), ~"3.1");
assert_eq!(to_pretty_str(&Number(3.1f)), ~"3.1");
assert_eq!(Number(3.1f).to_str(), ~"3.1");
assert_eq!(Number(3.1f).to_pretty_str(), ~"3.1");

assert_eq!(to_str(&Number(-1.5f)), ~"-1.5");
assert_eq!(to_pretty_str(&Number(-1.5f)), ~"-1.5");
assert_eq!(Number(-1.5f).to_str(), ~"-1.5");
assert_eq!(Number(-1.5f).to_pretty_str(), ~"-1.5");

assert_eq!(to_str(&Number(0.5f)), ~"0.5");
assert_eq!(to_pretty_str(&Number(0.5f)), ~"0.5");
assert_eq!(Number(0.5f).to_str(), ~"0.5");
assert_eq!(Number(0.5f).to_pretty_str(), ~"0.5");
}

#[test]
fn test_write_str() {
assert_eq!(to_str(&String(~"")), ~"\"\"");
assert_eq!(to_pretty_str(&String(~"")), ~"\"\"");
assert_eq!(String(~"").to_str(), ~"\"\"");
assert_eq!(String(~"").to_pretty_str(), ~"\"\"");

assert_eq!(to_str(&String(~"foo")), ~"\"foo\"");
assert_eq!(to_pretty_str(&String(~"foo")), ~"\"foo\"");
assert_eq!(String(~"foo").to_str(), ~"\"foo\"");
assert_eq!(String(~"foo").to_pretty_str(), ~"\"foo\"");
}

#[test]
fn test_write_bool() {
assert_eq!(to_str(&Boolean(true)), ~"true");
assert_eq!(to_pretty_str(&Boolean(true)), ~"true");
assert_eq!(Boolean(true).to_str(), ~"true");
assert_eq!(Boolean(true).to_pretty_str(), ~"true");

assert_eq!(to_str(&Boolean(false)), ~"false");
assert_eq!(to_pretty_str(&Boolean(false)), ~"false");
assert_eq!(Boolean(false).to_str(), ~"false");
assert_eq!(Boolean(false).to_pretty_str(), ~"false");
}

#[test]
fn test_write_list() {
assert_eq!(to_str(&List(~[])), ~"[]");
assert_eq!(to_pretty_str(&List(~[])), ~"[]");
assert_eq!(List(~[]).to_str(), ~"[]");
assert_eq!(List(~[]).to_pretty_str(), ~"[]");

assert_eq!(to_str(&List(~[Boolean(true)])), ~"[true]");
assert_eq!(List(~[Boolean(true)]).to_str(), ~"[true]");
assert_eq!(
to_pretty_str(&List(~[Boolean(true)])),
List(~[Boolean(true)]).to_pretty_str(),
~"\
[\n \
true\n\
]"
);

assert_eq!(to_str(&List(~[
let longTestList = List(~[
Boolean(false),
Null,
List(~[String(~"foo\nbar"), Number(3.5f)])
])), ~"[false,null,[\"foo\\nbar\",3.5]]");
List(~[String(~"foo\nbar"), Number(3.5f)])]);

assert_eq!(longTestList.to_str(),
~"[false,null,[\"foo\\nbar\",3.5]]");
assert_eq!(
to_pretty_str(&List(~[
Boolean(false),
Null,
List(~[String(~"foo\nbar"), Number(3.5f)])
])),
longTestList.to_pretty_str(),
~"\
[\n \
false,\n \
Expand All @@ -1435,28 +1434,30 @@ mod tests {

#[test]
fn test_write_object() {
assert_eq!(to_str(&mk_object([])), ~"{}");
assert_eq!(to_pretty_str(&mk_object([])), ~"{}");
assert_eq!(mk_object([]).to_str(), ~"{}");
assert_eq!(mk_object([]).to_pretty_str(), ~"{}");

assert_eq!(
to_str(&mk_object([(~"a", Boolean(true))])),
mk_object([(~"a", Boolean(true))]).to_str(),
~"{\"a\":true}"
);
assert_eq!(
to_pretty_str(&mk_object([(~"a", Boolean(true))])),
mk_object([(~"a", Boolean(true))]).to_pretty_str(),
~"\
{\n \
\"a\": true\n\
}"
);

assert_eq!(
to_str(&mk_object([
let complexObj = mk_object([
(~"b", List(~[
mk_object([(~"c", String(~"\x0c\r"))]),
mk_object([(~"d", String(~""))])
]))
])),
]);

assert_eq!(
complexObj.to_str(),
~"{\
\"b\":[\
{\"c\":\"\\f\\r\"},\
Expand All @@ -1465,12 +1466,7 @@ mod tests {
}"
);
assert_eq!(
to_pretty_str(&mk_object([
(~"b", List(~[
mk_object([(~"c", String(~"\x0c\r"))]),
mk_object([(~"d", String(~""))])
]))
])),
complexObj.to_pretty_str(),
~"\
{\n \
\"b\": [\n \
Expand All @@ -1494,8 +1490,8 @@ mod tests {

// We can't compare the strings directly because the object fields be
// printed in a different order.
assert_eq!(a.clone(), from_str(to_str(&a)).unwrap());
assert_eq!(a.clone(), from_str(to_pretty_str(&a)).unwrap());
assert_eq!(a.clone(), from_str(a.to_str()).unwrap());
assert_eq!(a.clone(), from_str(a.to_pretty_str()).unwrap());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ impl MetricMap {
/// Write MetricDiff to a file.
pub fn save(&self, p: &Path) {
let f = io::file_writer(p, [io::Create, io::Truncate]).unwrap();
json::to_pretty_writer(f, &self.to_json());
self.to_json().to_pretty_writer(f);
}

/// Compare against another MetricMap. Optionally compare all
Expand Down