Implement FromStr for Json

This commit is contained in:
Adolfo Ochagavía 2014-06-28 15:39:49 +02:00
parent 6ae5e92cc2
commit 954c3234a0
1 changed files with 13 additions and 3 deletions

View File

@ -2166,6 +2166,12 @@ impl fmt::Show for Json {
}
}
impl std::from_str::FromStr for Json {
fn from_str(s: &str) -> Option<Json> {
from_str(s).ok()
}
}
#[cfg(test)]
mod tests {
extern crate test;
@ -2180,9 +2186,7 @@ mod tests {
InvalidSyntax, InvalidNumber, EOFWhileParsingObject, EOFWhileParsingList,
EOFWhileParsingValue, EOFWhileParsingString, KeyMustBeAString, ExpectedColon,
TrailingCharacters};
use std::f32;
use std::f64;
use std::io;
use std::{f32, f64, io};
use std::collections::TreeMap;
#[deriving(PartialEq, Encodable, Decodable, Show)]
@ -2215,6 +2219,12 @@ mod tests {
Object(d)
}
#[test]
fn test_from_str_trait() {
let s = "null";
assert!(::std::from_str::from_str::<Json>(s).unwrap() == from_str(s).unwrap());
}
#[test]
fn test_write_null() {
assert_eq!(Null.to_str().into_string(), "null".to_string());