/// Represents any valid JSON value.
///
-/// See the `serde_json::value` module documentation for usage examples.
+/// See the [`serde_json::value` module documentation](self) for usage examples.
#[derive(Clone, Eq, PartialEq)]
pub enum Value {
/// Represents a JSON null value.
}
}
-struct WriterFormatter<'a, 'b: 'a> {
- inner: &'a mut fmt::Formatter<'b>,
-}
-
-impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
- fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
- fn io_error<E>(_: E) -> io::Error {
- // Error value does not matter because fmt::Display impl below just
- // maps it to fmt::Error
- io::Error::new(io::ErrorKind::Other, "fmt error")
- }
- let s = tri!(str::from_utf8(buf).map_err(io_error));
- tri!(self.inner.write_str(s).map_err(io_error));
- Ok(buf.len())
- }
-
- fn flush(&mut self) -> io::Result<()> {
- Ok(())
- }
-}
-
impl fmt::Display for Value {
/// Display a JSON value as a string.
///
/// "{\n \"city\": \"London\",\n \"street\": \"10 Downing Street\"\n}");
/// ```
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ struct WriterFormatter<'a, 'b: 'a> {
+ inner: &'a mut fmt::Formatter<'b>,
+ }
+
+ impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ // Safety: the serializer below only emits valid utf8 when using
+ // the default formatter.
+ let s = unsafe { str::from_utf8_unchecked(buf) };
+ tri!(self.inner.write_str(s).map_err(io_error));
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+ }
+
+ fn io_error(_: fmt::Error) -> io::Error {
+ // Error value does not matter because Display impl just maps it
+ // back to fmt::Error.
+ io::Error::new(io::ErrorKind::Other, "fmt error")
+ }
+
let alternate = f.alternate();
let mut wr = WriterFormatter { inner: f };
if alternate {
/// assert_eq!(data.pointer("/a/b/c"), None);
/// ```
pub fn pointer(&self, pointer: &str) -> Option<&Value> {
- if pointer == "" {
+ if pointer.is_empty() {
return Some(self);
}
if !pointer.starts_with('/') {
/// }
/// ```
pub fn pointer_mut(&mut self, pointer: &str) -> Option<&mut Value> {
- if pointer == "" {
+ if pointer.is_empty() {
return Some(self);
}
if !pointer.starts_with('/') {