Skip to content

Commit fc4491b

Browse files
committed
No breakage with 0.10.1, PrettyWriteGenerator to take variable number
of spaces (#88, #89)
1 parent c6faf0d commit fc4491b

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/codegen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ pub struct PrettyWriterGenerator<'a, W: 'a + Write> {
316316
}
317317

318318
impl<'a, W> PrettyWriterGenerator<'a, W> where W: 'a + Write {
319-
pub fn new(writer: &'a mut W) -> Self {
319+
pub fn new(writer: &'a mut W, spaces: u16) -> Self {
320320
PrettyWriterGenerator {
321321
writer: writer,
322322
dent: 0,
323-
spaces_per_indent: 4,
323+
spaces_per_indent: spaces,
324324
}
325325
}
326326
}

src/value/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,24 @@ impl JsonValue {
102102
gen.consume()
103103
}
104104

105-
/// Dumps the JSON as byte stream into an instance of `std::io::Write`.
106-
pub fn to_writer<W: Write>(&self, writer: &mut W) -> io::Result<()> {
105+
/// Writes the JSON as byte stream into an implementor of `std::io::Write`.
106+
///
107+
/// This method is deprecated as it will panic on io errors, use `write` instead.
108+
#[deprecated(since="0.10.2", note="use `JsonValue::write` instead")]
109+
pub fn to_writer<W: Write>(&self, writer: &mut W) {
110+
let mut gen = WriterGenerator::new(writer);
111+
gen.write_json(self).expect("Deprecated");
112+
}
113+
114+
/// Writes the JSON as byte stream into an implementor of `std::io::Write`.
115+
pub fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
107116
let mut gen = WriterGenerator::new(writer);
108117
gen.write_json(self)
109118
}
110119

111-
pub fn to_writer_pretty<W: Write>(&self, writer: &mut W) -> io::Result<()> {
112-
let mut gen = PrettyWriterGenerator::new(writer);
120+
/// Writes the JSON as byte stream into an implementor of `std::io::Write`.
121+
pub fn write_pretty<W: Write>(&self, writer: &mut W, spaces: u16) -> io::Result<()> {
122+
let mut gen = PrettyWriterGenerator::new(writer, spaces);
113123
gen.write_json(self)
114124
}
115125

tests/value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ fn writer_generator() {
517517

518518
let mut buf = Vec::new();
519519

520-
data.to_writer(&mut buf).expect("Can't fail with a Vec");
520+
data.write(&mut buf).expect("Can't fail with a Vec");
521521

522522
assert_eq!(String::from_utf8(buf).unwrap(), r#"{"foo":["bar",100,true]}"#);
523523
}
@@ -530,7 +530,7 @@ fn pretty_writer_generator() {
530530

531531
let mut buf = Vec::new();
532532

533-
data.to_writer_pretty(&mut buf).expect("Can't fail with a Vec");
533+
data.write_pretty(&mut buf, 4).expect("Can't fail with a Vec");
534534

535535
assert_eq!(String::from_utf8(buf).unwrap(), "{\n \"foo\": [\n \"bar\",\n 100,\n true\n ]\n}");
536536
}

0 commit comments

Comments
 (0)