diff options
| author | bors <bors@rust-lang.org> | 2014-12-23 02:41:48 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-12-23 02:41:48 +0000 |
| commit | 62fb41c32bd97c4e9bc286a1db5d7126a06b8b91 (patch) | |
| tree | fc1c7ab2bf1a29879d45235acaf0126ceae4d107 /src/libserialize | |
| parent | 2f3cff6956d56048ef7afb6d33e17cbdb2dcf038 (diff) | |
| parent | 3583d613b9c81855feb067aeeebb525cf8a4184c (diff) | |
| download | rust-62fb41c32bd97c4e9bc286a1db5d7126a06b8b91.tar.gz rust-62fb41c32bd97c4e9bc286a1db5d7126a06b8b91.zip | |
auto merge of #20145 : alexcrichton/rust/rollup, r=alexcrichton
Diffstat (limited to 'src/libserialize')
| -rw-r--r-- | src/libserialize/json.rs | 251 | ||||
| -rw-r--r-- | src/libserialize/lib.rs | 7 | ||||
| -rw-r--r-- | src/libserialize/serialize.rs | 4 |
3 files changed, 149 insertions, 113 deletions
diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index 3181e28a121..7df5590fb40 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -57,17 +57,17 @@ //! //! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via //! the serialization API. -//! To be able to encode a piece of data, it must implement the `serialize::Encodable` trait. -//! To be able to decode a piece of data, it must implement the `serialize::Decodable` trait. +//! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait. +//! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait. //! The Rust compiler provides an annotation to automatically generate the code for these traits: -//! `#[deriving(Decodable, Encodable)]` +//! `#[deriving(RustcDecodable, RustcEncodable)]` //! //! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects. //! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value. //! A `json::Json` value can be encoded as a string or buffer using the functions described above. //! You can also use the `json::Encoder` object, which implements the `Encoder` trait. //! -//! When using `ToJson` the `Encodable` trait implementation is not mandatory. +//! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory. //! //! # Examples of use //! @@ -127,7 +127,7 @@ //! } //! } //! -//! // Only generate `Encodable` trait implementation +//! // Only generate `RustcEncodable` trait implementation //! #[deriving(Encodable)] //! pub struct ComplexNumRecord { //! uid: u8, @@ -202,10 +202,11 @@ use std::collections::{HashMap, BTreeMap}; use std::{char, f64, fmt, io, num, str}; use std::mem::{swap, transmute}; use std::num::{Float, FPNaN, FPInfinite, Int}; -use std::str::{FromStr, ScalarValue}; +use std::str::{FromStr}; use std::string; -use std::vec::Vec; use std::ops; +use unicode::str as unicode_str; +use unicode::str::Utf16Item; use Encodable; @@ -333,11 +334,39 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError let escaped = match *byte { b'"' => "\\\"", b'\\' => "\\\\", + b'\x00' => "\\u0000", + b'\x01' => "\\u0001", + b'\x02' => "\\u0002", + b'\x03' => "\\u0003", + b'\x04' => "\\u0004", + b'\x05' => "\\u0005", + b'\x06' => "\\u0006", + b'\x07' => "\\u0007", b'\x08' => "\\b", - b'\x0c' => "\\f", + b'\t' => "\\t", b'\n' => "\\n", + b'\x0b' => "\\u000b", + b'\x0c' => "\\f", b'\r' => "\\r", - b'\t' => "\\t", + b'\x0e' => "\\u000e", + b'\x0f' => "\\u000f", + b'\x10' => "\\u0010", + b'\x11' => "\\u0011", + b'\x12' => "\\u0012", + b'\x13' => "\\u0013", + b'\x14' => "\\u0014", + b'\x15' => "\\u0015", + b'\x16' => "\\u0016", + b'\x17' => "\\u0017", + b'\x18' => "\\u0018", + b'\x19' => "\\u0019", + b'\x1a' => "\\u001a", + b'\x1b' => "\\u001b", + b'\x1c' => "\\u001c", + b'\x1d' => "\\u001d", + b'\x1e' => "\\u001e", + b'\x1f' => "\\u001f", + b'\x7f' => "\\u007f", _ => { continue; } }; @@ -404,7 +433,7 @@ impl<'a> Encoder<'a> { } /// Encode the specified struct into a json [u8] - pub fn buffer_encode<T:Encodable<Encoder<'a>, io::IoError>>(object: &T) -> Vec<u8> { + pub fn buffer_encode<T: Encodable<Encoder<'a>, io::IoError>>(object: &T) -> Vec<u8> { //Serialize the object in a string using a writer let mut m = Vec::new(); // FIXME(14302) remove the transmute and unsafe block. @@ -1001,7 +1030,7 @@ impl Json { /// Returns None otherwise. pub fn as_string<'a>(&'a self) -> Option<&'a str> { match *self { - Json::String(ref s) => Some(s.as_slice()), + Json::String(ref s) => Some(s[]), _ => None } } @@ -1585,8 +1614,8 @@ impl<T: Iterator<char>> Parser<T> { } let buf = [n1, try!(self.decode_hex_escape())]; - match str::utf16_items(buf.as_slice()).next() { - Some(ScalarValue(c)) => res.push(c), + match unicode_str::utf16_items(&buf).next() { + Some(Utf16Item::ScalarValue(c)) => res.push(c), _ => return self.error(LoneLeadingSurrogateInHexEscape), } } @@ -1934,7 +1963,7 @@ pub fn from_reader(rdr: &mut io::Reader) -> Result<Json, BuilderError> { Ok(c) => c, Err(e) => return Err(io_error_to_error(e)) }; - let s = match str::from_utf8(contents.as_slice()) { + let s = match str::from_utf8(contents.as_slice()).ok() { Some(s) => s, _ => return Err(SyntaxError(NotUtf8, 0, 0)) }; @@ -1970,7 +1999,7 @@ macro_rules! expect { ($e:expr, Null) => ({ match $e { Json::Null => Ok(()), - other => Err(ExpectedError("Null".into_string(), + other => Err(ExpectedError("Null".to_string(), format!("{}", other))) } }); @@ -1991,20 +2020,20 @@ macro_rules! read_primitive { match self.pop() { Json::I64(f) => match num::cast(f) { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), format!("{}", f))), + None => Err(ExpectedError("Number".to_string(), format!("{}", f))), }, Json::U64(f) => match num::cast(f) { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), format!("{}", f))), + None => Err(ExpectedError("Number".to_string(), format!("{}", f))), }, - Json::F64(f) => Err(ExpectedError("Integer".into_string(), format!("{}", f))), + Json::F64(f) => Err(ExpectedError("Integer".to_string(), format!("{}", f))), // re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc) // is going to have a string here, as per JSON spec. Json::String(s) => match std::str::from_str(s.as_slice()) { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), s)), + None => Err(ExpectedError("Number".to_string(), s)), }, - value => Err(ExpectedError("Number".into_string(), format!("{}", value))), + value => Err(ExpectedError("Number".to_string(), format!("{}", value))), } } } @@ -2036,13 +2065,13 @@ impl ::Decoder<DecoderError> for Decoder { Json::String(s) => { // re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc) // is going to have a string here, as per JSON spec. - match std::str::from_str(s.as_slice()) { + match s.parse() { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), s)), + None => Err(ExpectedError("Number".to_string(), s)), } }, Json::Null => Ok(f64::NAN), - value => Err(ExpectedError("Number".into_string(), format!("{}", value))) + value => Err(ExpectedError("Number".to_string(), format!("{}", value))) } } @@ -2060,7 +2089,7 @@ impl ::Decoder<DecoderError> for Decoder { _ => () } } - Err(ExpectedError("single character string".into_string(), format!("{}", s))) + Err(ExpectedError("single character string".to_string(), format!("{}", s))) } fn read_str(&mut self) -> DecodeResult<string::String> { @@ -2080,36 +2109,35 @@ impl ::Decoder<DecoderError> for Decoder { let name = match self.pop() { Json::String(s) => s, Json::Object(mut o) => { - let n = match o.remove(&"variant".into_string()) { + let n = match o.remove(&"variant".to_string()) { Some(Json::String(s)) => s, Some(val) => { - return Err(ExpectedError("String".into_string(), format!("{}", val))) + return Err(ExpectedError("String".to_string(), format!("{}", val))) } None => { - return Err(MissingFieldError("variant".into_string())) + return Err(MissingFieldError("variant".to_string())) } }; - match o.remove(&"fields".into_string()) { + match o.remove(&"fields".to_string()) { Some(Json::Array(l)) => { for field in l.into_iter().rev() { self.stack.push(field); } }, Some(val) => { - return Err(ExpectedError("Array".into_string(), format!("{}", val))) + return Err(ExpectedError("Array".to_string(), format!("{}", val))) } None => { - return Err(MissingFieldError("fields".into_string())) + return Err(MissingFieldError("fields".to_string())) } } n } json => { - return Err(ExpectedError("String or Object".into_string(), format!("{}", json))) + return Err(ExpectedError("String or Object".to_string(), format!("{}", json))) } }; - let idx = match names.iter() - .position(|n| str::eq_slice(*n, name.as_slice())) { + let idx = match names.iter().position(|n| *n == name[]) { Some(idx) => idx, None => return Err(UnknownVariantError(name)) }; @@ -2319,7 +2347,7 @@ impl ToJson for bool { } impl ToJson for str { - fn to_json(&self) -> Json { Json::String(self.into_string()) } + fn to_json(&self) -> Json { Json::String(self.to_string()) } } impl ToJson for string::String { @@ -2428,7 +2456,7 @@ mod tests { use std::num::Float; use std::string; - #[deriving(Decodable, Eq, PartialEq, Show)] + #[deriving(RustcDecodable, Eq, PartialEq, Show)] struct OptionData { opt: Option<uint>, } @@ -2450,25 +2478,25 @@ mod tests { #[test] fn test_decode_option_malformed() { check_err::<OptionData>("{ \"opt\": [] }", - ExpectedError("Number".into_string(), "[]".into_string())); + ExpectedError("Number".to_string(), "[]".to_string())); check_err::<OptionData>("{ \"opt\": false }", - ExpectedError("Number".into_string(), "false".into_string())); + ExpectedError("Number".to_string(), "false".to_string())); } - #[deriving(PartialEq, Encodable, Decodable, Show)] + #[deriving(PartialEq, RustcEncodable, RustcDecodable, Show)] enum Animal { Dog, Frog(string::String, int) } - #[deriving(PartialEq, Encodable, Decodable, Show)] + #[deriving(PartialEq, RustcEncodable, RustcDecodable, Show)] struct Inner { a: (), b: uint, c: Vec<string::String>, } - #[deriving(PartialEq, Encodable, Decodable, Show)] + #[deriving(PartialEq, RustcEncodable, RustcDecodable, Show)] struct Outer { inner: Vec<Inner>, } @@ -2538,11 +2566,11 @@ mod tests { #[test] fn test_write_str() { - assert_eq!(String("".into_string()).to_string(), "\"\""); - assert_eq!(String("".into_string()).to_pretty_str(), "\"\""); + assert_eq!(String("".to_string()).to_string(), "\"\""); + assert_eq!(String("".to_string()).to_pretty_str(), "\"\""); - assert_eq!(String("homura".into_string()).to_string(), "\"homura\""); - assert_eq!(String("madoka".into_string()).to_pretty_str(), "\"madoka\""); + assert_eq!(String("homura".to_string()).to_string(), "\"homura\""); + assert_eq!(String("madoka".to_string()).to_pretty_str(), "\"madoka\""); } #[test] @@ -2571,7 +2599,7 @@ mod tests { let long_test_array = Array(vec![ Boolean(false), Null, - Array(vec![String("foo\nbar".into_string()), F64(3.5)])]); + Array(vec![String("foo\nbar".to_string()), F64(3.5)])]); assert_eq!(long_test_array.to_string(), "[false,null,[\"foo\\nbar\",3.5]]"); @@ -2596,12 +2624,12 @@ mod tests { assert_eq!( mk_object(&[ - ("a".into_string(), Boolean(true)) + ("a".to_string(), Boolean(true)) ]).to_string(), "{\"a\":true}" ); assert_eq!( - mk_object(&[("a".into_string(), Boolean(true))]).to_pretty_str(), + mk_object(&[("a".to_string(), Boolean(true))]).to_pretty_str(), "\ {\n \ \"a\": true\n\ @@ -2609,9 +2637,9 @@ mod tests { ); let complex_obj = mk_object(&[ - ("b".into_string(), Array(vec![ - mk_object(&[("c".into_string(), String("\x0c\r".into_string()))]), - mk_object(&[("d".into_string(), String("".into_string()))]) + ("b".to_string(), Array(vec![ + mk_object(&[("c".to_string(), String("\x0c\r".to_string()))]), + mk_object(&[("d".to_string(), String("".to_string()))]) ])) ]); @@ -2640,10 +2668,10 @@ mod tests { ); let a = mk_object(&[ - ("a".into_string(), Boolean(true)), - ("b".into_string(), Array(vec![ - mk_object(&[("c".into_string(), String("\x0c\r".into_string()))]), - mk_object(&[("d".into_string(), String("".into_string()))]) + ("a".to_string(), Boolean(true)), + ("b".to_string(), Array(vec![ + mk_object(&[("c".to_string(), String("\x0c\r".to_string()))]), + mk_object(&[("d".to_string(), String("".to_string()))]) ])) ]); @@ -2678,7 +2706,7 @@ mod tests { "\"Dog\"" ); - let animal = Frog("Henry".into_string(), 349); + let animal = Frog("Henry".to_string(), 349); assert_eq!( with_str_writer(|writer| { let mut encoder = Encoder::new(writer); @@ -2731,6 +2759,9 @@ mod tests { fn test_write_char() { check_encoder_for_simple!('a', "\"a\""); check_encoder_for_simple!('\t', "\"\\t\""); + check_encoder_for_simple!('\u{0000}', "\"\\u0000\""); + check_encoder_for_simple!('\u{001b}', "\"\\u001b\""); + check_encoder_for_simple!('\u{007f}', "\"\\u007f\""); check_encoder_for_simple!('\u{00a0}', "\"\u{00a0}\""); check_encoder_for_simple!('\u{abcd}', "\"\u{abcd}\""); check_encoder_for_simple!('\u{10ffff}', "\"\u{10ffff}\""); @@ -2839,7 +2870,7 @@ mod tests { assert_eq!(v, i64::MAX); let res: DecodeResult<i64> = super::decode("765.25252"); - assert_eq!(res, Err(ExpectedError("Integer".into_string(), "765.25252".into_string()))); + assert_eq!(res, Err(ExpectedError("Integer".to_string(), "765.25252".to_string()))); } #[test] @@ -2847,16 +2878,16 @@ mod tests { assert_eq!(from_str("\""), Err(SyntaxError(EOFWhileParsingString, 1, 2))); assert_eq!(from_str("\"lol"), Err(SyntaxError(EOFWhileParsingString, 1, 5))); - assert_eq!(from_str("\"\""), Ok(String("".into_string()))); - assert_eq!(from_str("\"foo\""), Ok(String("foo".into_string()))); - assert_eq!(from_str("\"\\\"\""), Ok(String("\"".into_string()))); - assert_eq!(from_str("\"\\b\""), Ok(String("\x08".into_string()))); - assert_eq!(from_str("\"\\n\""), Ok(String("\n".into_string()))); - assert_eq!(from_str("\"\\r\""), Ok(String("\r".into_string()))); - assert_eq!(from_str("\"\\t\""), Ok(String("\t".into_string()))); - assert_eq!(from_str(" \"foo\" "), Ok(String("foo".into_string()))); - assert_eq!(from_str("\"\\u12ab\""), Ok(String("\u{12ab}".into_string()))); - assert_eq!(from_str("\"\\uAB12\""), Ok(String("\u{AB12}".into_string()))); + assert_eq!(from_str("\"\""), Ok(String("".to_string()))); + assert_eq!(from_str("\"foo\""), Ok(String("foo".to_string()))); + assert_eq!(from_str("\"\\\"\""), Ok(String("\"".to_string()))); + assert_eq!(from_str("\"\\b\""), Ok(String("\x08".to_string()))); + assert_eq!(from_str("\"\\n\""), Ok(String("\n".to_string()))); + assert_eq!(from_str("\"\\r\""), Ok(String("\r".to_string()))); + assert_eq!(from_str("\"\\t\""), Ok(String("\t".to_string()))); + assert_eq!(from_str(" \"foo\" "), Ok(String("foo".to_string()))); + assert_eq!(from_str("\"\\u12ab\""), Ok(String("\u{12ab}".to_string()))); + assert_eq!(from_str("\"\\uAB12\""), Ok(String("\u{AB12}".to_string()))); } #[test] @@ -2922,7 +2953,7 @@ mod tests { assert_eq!(t, (1u, 2, 3)); let t: (uint, string::String) = super::decode("[1, \"two\"]").unwrap(); - assert_eq!(t, (1u, "two".into_string())); + assert_eq!(t, (1u, "two".to_string())); } #[test] @@ -2952,22 +2983,22 @@ mod tests { assert_eq!(from_str("{}").unwrap(), mk_object(&[])); assert_eq!(from_str("{\"a\": 3}").unwrap(), - mk_object(&[("a".into_string(), U64(3))])); + mk_object(&[("a".to_string(), U64(3))])); assert_eq!(from_str( "{ \"a\": null, \"b\" : true }").unwrap(), mk_object(&[ - ("a".into_string(), Null), - ("b".into_string(), Boolean(true))])); + ("a".to_string(), Null), + ("b".to_string(), Boolean(true))])); assert_eq!(from_str("\n{ \"a\": null, \"b\" : true }\n").unwrap(), mk_object(&[ - ("a".into_string(), Null), - ("b".into_string(), Boolean(true))])); + ("a".to_string(), Null), + ("b".to_string(), Boolean(true))])); assert_eq!(from_str( "{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(), mk_object(&[ - ("a".into_string(), F64(1.0)), - ("b".into_string(), Array(vec![Boolean(true)])) + ("a".to_string(), F64(1.0)), + ("b".to_string(), Array(vec![Boolean(true)])) ])); assert_eq!(from_str( "{\ @@ -2979,12 +3010,12 @@ mod tests { ]\ }").unwrap(), mk_object(&[ - ("a".into_string(), F64(1.0)), - ("b".into_string(), Array(vec![ + ("a".to_string(), F64(1.0)), + ("b".to_string(), Array(vec![ Boolean(true), - String("foo\nbar".into_string()), + String("foo\nbar".to_string()), mk_object(&[ - ("c".into_string(), mk_object(&[("d".into_string(), Null)])) + ("c".to_string(), mk_object(&[("d".to_string(), Null)])) ]) ])) ])); @@ -3003,13 +3034,13 @@ mod tests { v, Outer { inner: vec![ - Inner { a: (), b: 2, c: vec!["abc".into_string(), "xyz".into_string()] } + Inner { a: (), b: 2, c: vec!["abc".to_string(), "xyz".to_string()] } ] } ); } - #[deriving(Decodable)] + #[deriving(RustcDecodable)] struct FloatStruct { f: f64, a: Vec<f64> @@ -3029,7 +3060,7 @@ mod tests { assert_eq!(value, None); let value: Option<string::String> = super::decode("\"jodhpurs\"").unwrap(); - assert_eq!(value, Some("jodhpurs".into_string())); + assert_eq!(value, Some("jodhpurs".to_string())); } #[test] @@ -3039,7 +3070,7 @@ mod tests { let s = "{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"; let value: Animal = super::decode(s).unwrap(); - assert_eq!(value, Frog("Henry".into_string(), 349)); + assert_eq!(value, Frog("Henry".to_string(), 349)); } #[test] @@ -3048,8 +3079,8 @@ mod tests { \"fields\":[\"Henry\", 349]}}"; let mut map: BTreeMap<string::String, Animal> = super::decode(s).unwrap(); - assert_eq!(map.remove(&"a".into_string()), Some(Dog)); - assert_eq!(map.remove(&"b".into_string()), Some(Frog("Henry".into_string(), 349))); + assert_eq!(map.remove(&"a".to_string()), Some(Dog)); + assert_eq!(map.remove(&"b".to_string()), Some(Frog("Henry".to_string(), 349))); } #[test] @@ -3058,7 +3089,7 @@ mod tests { Err(SyntaxError(EOFWhileParsingObject, 3u, 8u))); } - #[deriving(Decodable)] + #[deriving(RustcDecodable)] #[allow(dead_code)] struct DecodeStruct { x: f64, @@ -3066,7 +3097,7 @@ mod tests { z: string::String, w: Vec<DecodeStruct> } - #[deriving(Decodable)] + #[deriving(RustcDecodable)] enum DecodeEnum { A(f64), B(string::String) @@ -3089,30 +3120,30 @@ mod tests { } #[test] fn test_decode_errors_struct() { - check_err::<DecodeStruct>("[]", ExpectedError("Object".into_string(), "[]".into_string())); + check_err::<DecodeStruct>("[]", ExpectedError("Object".to_string(), "[]".to_string())); check_err::<DecodeStruct>("{\"x\": true, \"y\": true, \"z\": \"\", \"w\": []}", - ExpectedError("Number".into_string(), "true".into_string())); + ExpectedError("Number".to_string(), "true".to_string())); check_err::<DecodeStruct>("{\"x\": 1, \"y\": [], \"z\": \"\", \"w\": []}", - ExpectedError("Boolean".into_string(), "[]".into_string())); + ExpectedError("Boolean".to_string(), "[]".to_string())); check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": {}, \"w\": []}", - ExpectedError("String".into_string(), "{}".into_string())); + ExpectedError("String".to_string(), "{}".to_string())); check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": \"\", \"w\": null}", - ExpectedError("Array".into_string(), "null".into_string())); + ExpectedError("Array".to_string(), "null".to_string())); check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": \"\"}", - MissingFieldError("w".into_string())); + MissingFieldError("w".to_string())); } #[test] fn test_decode_errors_enum() { check_err::<DecodeEnum>("{}", - MissingFieldError("variant".into_string())); + MissingFieldError("variant".to_string())); check_err::<DecodeEnum>("{\"variant\": 1}", - ExpectedError("String".into_string(), "1".into_string())); + ExpectedError("String".to_string(), "1".to_string())); check_err::<DecodeEnum>("{\"variant\": \"A\"}", - MissingFieldError("fields".into_string())); + MissingFieldError("fields".to_string())); check_err::<DecodeEnum>("{\"variant\": \"A\", \"fields\": null}", - ExpectedError("Array".into_string(), "null".into_string())); + ExpectedError("Array".to_string(), "null".to_string())); check_err::<DecodeEnum>("{\"variant\": \"C\", \"fields\": []}", - UnknownVariantError("C".into_string())); + UnknownVariantError("C".to_string())); } #[test] @@ -3325,15 +3356,15 @@ mod tests { let mut tree = BTreeMap::new(); - tree.insert("hello".into_string(), String("guten tag".into_string())); - tree.insert("goodbye".into_string(), String("sayonara".into_string())); + tree.insert("hello".to_string(), String("guten tag".to_string())); + tree.insert("goodbye".to_string(), String("sayonara".to_string())); let json = Array( // The following layout below should look a lot like // the pretty-printed JSON (indent * x) vec! ( // 0x - String("greetings".into_string()), // 1x + String("greetings".to_string()), // 1x Object(tree), // 1x + 2x + 2x + 1x ) // 0x // End JSON array (7 lines) @@ -3397,7 +3428,7 @@ mod tests { }; let mut decoder = Decoder::new(json_obj); let result: Result<HashMap<uint, bool>, DecoderError> = Decodable::decode(&mut decoder); - assert_eq!(result, Err(ExpectedError("Number".into_string(), "a".into_string()))); + assert_eq!(result, Err(ExpectedError("Number".to_string(), "a".to_string()))); } fn assert_stream_equal(src: &str, @@ -3424,7 +3455,7 @@ mod tests { r#"{ "foo":"bar", "array" : [0, 1, 2, 3, 4, 5], "idents":[null,true,false]}"#, vec![ (ObjectStart, vec![]), - (StringValue("bar".into_string()), vec![Key("foo")]), + (StringValue("bar".to_string()), vec![Key("foo")]), (ArrayStart, vec![Key("array")]), (U64Value(0), vec![Key("array"), Index(0)]), (U64Value(1), vec![Key("array"), Index(1)]), @@ -3515,7 +3546,7 @@ mod tests { (F64Value(1.0), vec![Key("a")]), (ArrayStart, vec![Key("b")]), (BooleanValue(true), vec![Key("b"), Index(0)]), - (StringValue("foo\nbar".into_string()), vec![Key("b"), Index(1)]), + (StringValue("foo\nbar".to_string()), vec![Key("b"), Index(1)]), (ObjectStart, vec![Key("b"), Index(2)]), (ObjectStart, vec![Key("b"), Index(2), Key("c")]), (NullValue, vec![Key("b"), Index(2), Key("c"), Key("d")]), @@ -3648,7 +3679,7 @@ mod tests { assert!(stack.last_is_index()); assert!(stack.get(0) == Index(1)); - stack.push_key("foo".into_string()); + stack.push_key("foo".to_string()); assert!(stack.len() == 2); assert!(stack.is_equal_to(&[Index(1), Key("foo")])); @@ -3660,7 +3691,7 @@ mod tests { assert!(stack.get(0) == Index(1)); assert!(stack.get(1) == Key("foo")); - stack.push_key("bar".into_string()); + stack.push_key("bar".to_string()); assert!(stack.len() == 3); assert!(stack.is_equal_to(&[Index(1), Key("foo"), Key("bar")])); @@ -3721,8 +3752,8 @@ mod tests { assert_eq!(f64::NAN.to_json(), Null); assert_eq!(true.to_json(), Boolean(true)); assert_eq!(false.to_json(), Boolean(false)); - assert_eq!("abc".to_json(), String("abc".into_string())); - assert_eq!("abc".into_string().to_json(), String("abc".into_string())); + assert_eq!("abc".to_json(), String("abc".to_string())); + assert_eq!("abc".to_string().to_json(), String("abc".to_string())); assert_eq!((1u, 2u).to_json(), array2); assert_eq!((1u, 2u, 3u).to_json(), array3); assert_eq!([1u, 2].to_json(), array2); @@ -3734,8 +3765,8 @@ mod tests { tree_map.insert("b".into_string(), 2); assert_eq!(tree_map.to_json(), object); let mut hash_map = HashMap::new(); - hash_map.insert("a".into_string(), 1u); - hash_map.insert("b".into_string(), 2); + hash_map.insert("a".to_string(), 1u); + hash_map.insert("b".to_string(), 2); assert_eq!(hash_map.to_json(), object); assert_eq!(Some(15i).to_json(), I64(15)); assert_eq!(Some(15u).to_json(), U64(15)); @@ -3778,7 +3809,7 @@ mod tests { } fn big_json() -> string::String { - let mut src = "[\n".into_string(); + let mut src = "[\n".to_string(); for _ in range(0i, 500) { src.push_str(r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \ [1,2,3]},"#); diff --git a/src/libserialize/lib.rs b/src/libserialize/lib.rs index e700d102fef..4a2bbbeec03 100644 --- a/src/libserialize/lib.rs +++ b/src/libserialize/lib.rs @@ -15,7 +15,7 @@ Core encoding and decoding interfaces. */ #![crate_name = "serialize"] -#![experimental] +#![unstable = "deprecated in favor of rustc-serialize on crates.io"] #![crate_type = "rlib"] #![crate_type = "dylib"] #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", @@ -32,6 +32,7 @@ extern crate test; #[phase(plugin, link)] extern crate log; +extern crate unicode; extern crate collections; @@ -44,3 +45,7 @@ mod collection_impls; pub mod base64; pub mod hex; pub mod json; + +mod rustc_serialize { + pub use serialize::*; +} diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs index 00c5158309e..558f9e603e1 100644 --- a/src/libserialize/serialize.rs +++ b/src/libserialize/serialize.rs @@ -308,13 +308,13 @@ impl<E, S:Encoder<E>> Encodable<S, E> for str { impl<E, S:Encoder<E>> Encodable<S, E> for String { fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(self.as_slice()) + s.emit_str(self[]) } } impl<E, D:Decoder<E>> Decodable<D, E> for String { fn decode(d: &mut D) -> Result<String, E> { - Ok(String::from_str(try!(d.read_str()).as_slice())) + d.read_str() } } |
