Skip to content

Commit 954c323

Browse files
committed
Implement FromStr for Json
1 parent 6ae5e92 commit 954c323

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/libserialize/json.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,12 @@ impl fmt::Show for Json {
21662166
}
21672167
}
21682168

2169+
impl std::from_str::FromStr for Json {
2170+
fn from_str(s: &str) -> Option<Json> {
2171+
from_str(s).ok()
2172+
}
2173+
}
2174+
21692175
#[cfg(test)]
21702176
mod tests {
21712177
extern crate test;
@@ -2180,9 +2186,7 @@ mod tests {
21802186
InvalidSyntax, InvalidNumber, EOFWhileParsingObject, EOFWhileParsingList,
21812187
EOFWhileParsingValue, EOFWhileParsingString, KeyMustBeAString, ExpectedColon,
21822188
TrailingCharacters};
2183-
use std::f32;
2184-
use std::f64;
2185-
use std::io;
2189+
use std::{f32, f64, io};
21862190
use std::collections::TreeMap;
21872191

21882192
#[deriving(PartialEq, Encodable, Decodable, Show)]
@@ -2215,6 +2219,12 @@ mod tests {
22152219
Object(d)
22162220
}
22172221

2222+
#[test]
2223+
fn test_from_str_trait() {
2224+
let s = "null";
2225+
assert!(::std::from_str::from_str::<Json>(s).unwrap() == from_str(s).unwrap());
2226+
}
2227+
22182228
#[test]
22192229
fn test_write_null() {
22202230
assert_eq!(Null.to_str().into_string(), "null".to_string());

0 commit comments

Comments
 (0)