mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 12:13:43 +00:00
extra: Rename json constructors into *::init
This commit is contained in:
parent
5b41df4ca0
commit
f7b739c34c
@ -88,11 +88,13 @@ pub struct Encoder {
|
||||
priv wr: @mut io::Writer,
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
/// Creates a new JSON encoder whose output will be written to the writer
|
||||
/// specified.
|
||||
pub fn Encoder(wr: @mut io::Writer) -> Encoder {
|
||||
pub fn init(wr: @mut io::Writer) -> Encoder {
|
||||
Encoder { wr: wr }
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::Encoder for Encoder {
|
||||
fn emit_nil(&mut self) { write!(self.wr, "null") }
|
||||
@ -243,13 +245,15 @@ pub struct PrettyEncoder {
|
||||
priv indent: uint,
|
||||
}
|
||||
|
||||
impl PrettyEncoder {
|
||||
/// Creates a new encoder whose output will be written to the specified writer
|
||||
pub fn PrettyEncoder(wr: @mut io::Writer) -> PrettyEncoder {
|
||||
pub fn init(wr: @mut io::Writer) -> PrettyEncoder {
|
||||
PrettyEncoder {
|
||||
wr: wr,
|
||||
indent: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::Encoder for PrettyEncoder {
|
||||
fn emit_nil(&mut self) { write!(self.wr, "null") }
|
||||
@ -445,14 +449,14 @@ impl<E: serialize::Encoder> serialize::Encodable<E> for Json {
|
||||
impl Json{
|
||||
/// Encodes a json value into a io::writer. Uses a single line.
|
||||
pub fn to_writer(&self, wr: @mut io::Writer) {
|
||||
let mut encoder = Encoder(wr);
|
||||
let mut encoder = Encoder::init(wr);
|
||||
self.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: @mut io::Writer) {
|
||||
let mut encoder = PrettyEncoder(wr);
|
||||
let mut encoder = PrettyEncoder::init(wr);
|
||||
self.encode(&mut encoder)
|
||||
}
|
||||
|
||||
@ -465,14 +469,15 @@ impl Json{
|
||||
}
|
||||
|
||||
pub struct Parser<T> {
|
||||
priv rdr: ~T,
|
||||
priv rdr: T,
|
||||
priv ch: char,
|
||||
priv line: uint,
|
||||
priv col: uint,
|
||||
}
|
||||
|
||||
impl<T: Iterator<char>> Parser<T> {
|
||||
/// Decode a json value from an Iterator<char>
|
||||
pub fn Parser<T : Iterator<char>>(rdr: ~T) -> Parser<T> {
|
||||
pub fn init(rdr: T) -> Parser<T> {
|
||||
let mut p = Parser {
|
||||
rdr: rdr,
|
||||
ch: '\x00',
|
||||
@ -482,6 +487,7 @@ pub fn Parser<T : Iterator<char>>(rdr: ~T) -> Parser<T> {
|
||||
p.bump();
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Iterator<char>> Parser<T> {
|
||||
pub fn parse(&mut self) -> Result<Json, Error> {
|
||||
@ -842,13 +848,13 @@ impl<T : Iterator<char>> Parser<T> {
|
||||
/// Decodes a json value from an `&mut io::Reader`
|
||||
pub fn from_reader(rdr: &mut io::Reader) -> Result<Json, Error> {
|
||||
let s = str::from_utf8(rdr.read_to_end());
|
||||
let mut parser = Parser(~s.chars());
|
||||
let mut parser = Parser::init(s.chars());
|
||||
parser.parse()
|
||||
}
|
||||
|
||||
/// Decodes a json value from a string
|
||||
pub fn from_str(s: &str) -> Result<Json, Error> {
|
||||
let mut parser = Parser(~s.chars());
|
||||
let mut parser = Parser::init(s.chars());
|
||||
parser.parse()
|
||||
}
|
||||
|
||||
@ -857,12 +863,14 @@ pub struct Decoder {
|
||||
priv stack: ~[Json],
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
/// Creates a new decoder instance for decoding the specified JSON value.
|
||||
pub fn Decoder(json: Json) -> Decoder {
|
||||
pub fn init(json: Json) -> Decoder {
|
||||
Decoder {
|
||||
stack: ~[json]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
fn err(&self, msg: &str) -> ! {
|
||||
@ -1514,14 +1522,14 @@ mod tests {
|
||||
let animal = Dog;
|
||||
assert_eq!(
|
||||
with_str_writer(|wr| {
|
||||
let mut encoder = Encoder(wr);
|
||||
let mut encoder = Encoder::init(wr);
|
||||
animal.encode(&mut encoder);
|
||||
}),
|
||||
~"\"Dog\""
|
||||
);
|
||||
assert_eq!(
|
||||
with_str_writer(|wr| {
|
||||
let mut encoder = PrettyEncoder(wr);
|
||||
let mut encoder = PrettyEncoder::init(wr);
|
||||
animal.encode(&mut encoder);
|
||||
}),
|
||||
~"\"Dog\""
|
||||
@ -1530,14 +1538,14 @@ mod tests {
|
||||
let animal = Frog(~"Henry", 349);
|
||||
assert_eq!(
|
||||
with_str_writer(|wr| {
|
||||
let mut encoder = Encoder(wr);
|
||||
let mut encoder = Encoder::init(wr);
|
||||
animal.encode(&mut encoder);
|
||||
}),
|
||||
~"{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"
|
||||
);
|
||||
assert_eq!(
|
||||
with_str_writer(|wr| {
|
||||
let mut encoder = PrettyEncoder(wr);
|
||||
let mut encoder = PrettyEncoder::init(wr);
|
||||
animal.encode(&mut encoder);
|
||||
}),
|
||||
~"\
|
||||
@ -1553,14 +1561,14 @@ mod tests {
|
||||
fn test_write_some() {
|
||||
let value = Some(~"jodhpurs");
|
||||
let s = with_str_writer(|wr| {
|
||||
let mut encoder = Encoder(wr);
|
||||
let mut encoder = Encoder::init(wr);
|
||||
value.encode(&mut encoder);
|
||||
});
|
||||
assert_eq!(s, ~"\"jodhpurs\"");
|
||||
|
||||
let value = Some(~"jodhpurs");
|
||||
let s = with_str_writer(|wr| {
|
||||
let mut encoder = PrettyEncoder(wr);
|
||||
let mut encoder = PrettyEncoder::init(wr);
|
||||
value.encode(&mut encoder);
|
||||
});
|
||||
assert_eq!(s, ~"\"jodhpurs\"");
|
||||
@ -1570,13 +1578,13 @@ mod tests {
|
||||
fn test_write_none() {
|
||||
let value: Option<~str> = None;
|
||||
let s = with_str_writer(|wr| {
|
||||
let mut encoder = Encoder(wr);
|
||||
let mut encoder = Encoder::init(wr);
|
||||
value.encode(&mut encoder);
|
||||
});
|
||||
assert_eq!(s, ~"null");
|
||||
|
||||
let s = with_str_writer(|wr| {
|
||||
let mut encoder = Encoder(wr);
|
||||
let mut encoder = Encoder::init(wr);
|
||||
value.encode(&mut encoder);
|
||||
});
|
||||
assert_eq!(s, ~"null");
|
||||
@ -1625,15 +1633,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_decode_identifiers() {
|
||||
let mut decoder = Decoder(from_str("null").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("null").unwrap());
|
||||
let v: () = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ());
|
||||
|
||||
let mut decoder = Decoder(from_str("true").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("true").unwrap());
|
||||
let v: bool = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, true);
|
||||
|
||||
let mut decoder = Decoder(from_str("false").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("false").unwrap());
|
||||
let v: bool = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, false);
|
||||
}
|
||||
@ -1668,31 +1676,31 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_decode_numbers() {
|
||||
let mut decoder = Decoder(from_str("3").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("3").unwrap());
|
||||
let v: f64 = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, 3.0);
|
||||
|
||||
let mut decoder = Decoder(from_str("3.1").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("3.1").unwrap());
|
||||
let v: f64 = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, 3.1);
|
||||
|
||||
let mut decoder = Decoder(from_str("-1.2").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("-1.2").unwrap());
|
||||
let v: f64 = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, -1.2);
|
||||
|
||||
let mut decoder = Decoder(from_str("0.4").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("0.4").unwrap());
|
||||
let v: f64 = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, 0.4);
|
||||
|
||||
let mut decoder = Decoder(from_str("0.4e5").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("0.4e5").unwrap());
|
||||
let v: f64 = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, 0.4e5);
|
||||
|
||||
let mut decoder = Decoder(from_str("0.4e15").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("0.4e15").unwrap());
|
||||
let v: f64 = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, 0.4e15);
|
||||
|
||||
let mut decoder = Decoder(from_str("0.4e-01").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("0.4e-01").unwrap());
|
||||
let v: f64 = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, 0.4e-01);
|
||||
}
|
||||
@ -1720,39 +1728,39 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_decode_str() {
|
||||
let mut decoder = Decoder(from_str("\"\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"foo\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"foo\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"foo");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"\\\"\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\\\"\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"\"");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"\\b\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\\b\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"\x08");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"\\n\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\\n\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"\n");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"\\r\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\\r\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"\r");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"\\t\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\\t\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"\t");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"\\u12ab\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\\u12ab\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"\u12ab");
|
||||
|
||||
let mut decoder = Decoder(from_str("\"\\uAB12\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"\\uAB12\"").unwrap());
|
||||
let v: ~str = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~"\uAB12");
|
||||
}
|
||||
@ -1785,27 +1793,27 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_decode_list() {
|
||||
let mut decoder = Decoder(from_str("[]").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("[]").unwrap());
|
||||
let v: ~[()] = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~[]);
|
||||
|
||||
let mut decoder = Decoder(from_str("[null]").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("[null]").unwrap());
|
||||
let v: ~[()] = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~[()]);
|
||||
|
||||
let mut decoder = Decoder(from_str("[true]").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("[true]").unwrap());
|
||||
let v: ~[bool] = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~[true]);
|
||||
|
||||
let mut decoder = Decoder(from_str("[true]").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("[true]").unwrap());
|
||||
let v: ~[bool] = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~[true]);
|
||||
|
||||
let mut decoder = Decoder(from_str("[3, 1]").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("[3, 1]").unwrap());
|
||||
let v: ~[int] = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~[3, 1]);
|
||||
|
||||
let mut decoder = Decoder(from_str("[[3], [1, 2]]").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("[[3], [1, 2]]").unwrap());
|
||||
let v: ~[~[uint]] = Decodable::decode(&mut decoder);
|
||||
assert_eq!(v, ~[~[3], ~[1, 2]]);
|
||||
}
|
||||
@ -1907,7 +1915,7 @@ mod tests {
|
||||
{ \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] }
|
||||
]
|
||||
}";
|
||||
let mut decoder = Decoder(from_str(s).unwrap());
|
||||
let mut decoder = Decoder::init(from_str(s).unwrap());
|
||||
let v: Outer = Decodable::decode(&mut decoder);
|
||||
assert_eq!(
|
||||
v,
|
||||
@ -1921,23 +1929,23 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_decode_option() {
|
||||
let mut decoder = Decoder(from_str("null").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("null").unwrap());
|
||||
let value: Option<~str> = Decodable::decode(&mut decoder);
|
||||
assert_eq!(value, None);
|
||||
|
||||
let mut decoder = Decoder(from_str("\"jodhpurs\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"jodhpurs\"").unwrap());
|
||||
let value: Option<~str> = Decodable::decode(&mut decoder);
|
||||
assert_eq!(value, Some(~"jodhpurs"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_enum() {
|
||||
let mut decoder = Decoder(from_str("\"Dog\"").unwrap());
|
||||
let mut decoder = Decoder::init(from_str("\"Dog\"").unwrap());
|
||||
let value: Animal = Decodable::decode(&mut decoder);
|
||||
assert_eq!(value, Dog);
|
||||
|
||||
let mut decoder =
|
||||
Decoder(from_str("{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}").unwrap());
|
||||
let s = "{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}";
|
||||
let mut decoder = Decoder::init(from_str(s).unwrap());
|
||||
let value: Animal = Decodable::decode(&mut decoder);
|
||||
assert_eq!(value, Frog(~"Henry", 349));
|
||||
}
|
||||
@ -1945,7 +1953,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_decode_map() {
|
||||
let s = ~"{\"a\": \"Dog\", \"b\": {\"variant\":\"Frog\",\"fields\":[\"Henry\", 349]}}";
|
||||
let mut decoder = Decoder(from_str(s).unwrap());
|
||||
let mut decoder = Decoder::init(from_str(s).unwrap());
|
||||
let mut map: TreeMap<~str, Animal> = Decodable::decode(&mut decoder);
|
||||
|
||||
assert_eq!(map.pop(&~"a"), Some(Dog));
|
||||
@ -1982,7 +1990,7 @@ mod tests {
|
||||
match from_str(to_parse) {
|
||||
Err(e) => Some(e.to_str()),
|
||||
Ok(json) => {
|
||||
let _: T = Decodable::decode(&mut Decoder(json));
|
||||
let _: T = Decodable::decode(&mut Decoder::init(json));
|
||||
None
|
||||
}
|
||||
}
|
||||
|
@ -946,7 +946,7 @@ impl MetricMap {
|
||||
pub fn load(p: &Path) -> MetricMap {
|
||||
assert!(p.exists());
|
||||
let f = @mut File::open(p) as @mut io::Reader;
|
||||
let mut decoder = json::Decoder(json::from_reader(f).unwrap());
|
||||
let mut decoder = json::Decoder::init(json::from_reader(f).unwrap());
|
||||
MetricMap(Decodable::decode(&mut decoder))
|
||||
}
|
||||
|
||||
|
@ -191,7 +191,7 @@ impl Database {
|
||||
Err(e) => fail!("Couldn't parse workcache database (from file {}): {}",
|
||||
self.db_filename.display(), e.to_str()),
|
||||
Ok(r) => {
|
||||
let mut decoder = json::Decoder(r);
|
||||
let mut decoder = json::Decoder::init(r);
|
||||
self.db_cache = Decodable::decode(&mut decoder);
|
||||
}
|
||||
}
|
||||
@ -258,7 +258,7 @@ enum Work<'self, T> {
|
||||
|
||||
fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
|
||||
let writer = @mut MemWriter::new();
|
||||
let mut encoder = json::Encoder(writer as @mut io::Writer);
|
||||
let mut encoder = json::Encoder::init(writer as @mut io::Writer);
|
||||
t.encode(&mut encoder);
|
||||
str::from_utf8(writer.inner_ref().as_slice())
|
||||
}
|
||||
@ -267,7 +267,7 @@ fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
|
||||
fn json_decode<T:Decodable<json::Decoder>>(s: &str) -> T {
|
||||
debug!("json decoding: {}", s);
|
||||
let j = json::from_str(s).unwrap();
|
||||
let mut decoder = json::Decoder(j);
|
||||
let mut decoder = json::Decoder::init(j);
|
||||
Decodable::decode(&mut decoder)
|
||||
}
|
||||
|
||||
|
@ -283,7 +283,7 @@ fn json_input(input: &str) -> Result<Output, ~str> {
|
||||
}
|
||||
let crate = match obj.pop(&~"crate") {
|
||||
Some(json) => {
|
||||
let mut d = json::Decoder(json);
|
||||
let mut d = json::Decoder::init(json);
|
||||
Decodable::decode(&mut d)
|
||||
}
|
||||
None => return Err(~"malformed json"),
|
||||
@ -313,7 +313,7 @@ fn json_output(crate: clean::Crate, res: ~[plugins::PluginJson], dst: Path) {
|
||||
// straight to the Rust JSON representation.
|
||||
let crate_json_str = {
|
||||
let w = @mut MemWriter::new();
|
||||
crate.encode(&mut json::Encoder(w as @mut io::Writer));
|
||||
crate.encode(&mut json::Encoder::init(w as @mut io::Writer));
|
||||
str::from_utf8(*w.inner_ref())
|
||||
};
|
||||
let crate_json = match json::from_str(crate_json_str) {
|
||||
|
@ -343,7 +343,7 @@ mod test {
|
||||
|
||||
#[cfg(test)] fn to_json_str<E : Encodable<extra::json::Encoder>>(val: @E) -> ~str {
|
||||
let writer = @mut MemWriter::new();
|
||||
let mut encoder = extra::json::Encoder(writer as @mut io::Writer);
|
||||
let mut encoder = extra::json::Encoder::init(writer as @mut io::Writer);
|
||||
val.encode(&mut encoder);
|
||||
str::from_utf8(*writer.inner_ref())
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ trait JD : Decodable<json::Decoder> { }
|
||||
|
||||
fn exec<T: JD>() {
|
||||
let doc = json::from_str("").unwrap();
|
||||
let mut decoder = json::Decoder(doc);
|
||||
let mut decoder = json::Decoder::init(doc);
|
||||
let _v: T = Decodable::decode(&mut decoder);
|
||||
fail!()
|
||||
}
|
||||
|
@ -17,6 +17,6 @@ use self::extra::serialize;
|
||||
|
||||
pub fn main() {
|
||||
let json = json::from_str("[1]").unwrap();
|
||||
let mut decoder = json::Decoder(json);
|
||||
let mut decoder = json::Decoder::init(json);
|
||||
let _x: ~[int] = serialize::Decodable::decode(&mut decoder);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user