[][src]Struct nop_json::Reader

pub struct Reader<T> where
    T: Iterator<Item = u8>, 
{ /* fields omitted */ }

Implementations

impl<T> Reader<T> where
    T: Iterator<Item = u8>, 
[src]

pub fn new(iter: T) -> Reader<T>[src]

Construct new reader object, that can read values from a JSON stream, passing an object that implements Iterator<u8>. This allows to use &str as data source like this:

let source = "\"Data\"";
let mut reader = Reader::new(source.bytes());

To use &[u8] do this:

let source: &[u8] = b"\"Data\"";
let mut reader = Reader::new(source.iter().map(|i| *i));

To use std::io::Read as source, this crate has adapter object that converts std::io::Read to Iterator<u8>.

use nop_json::{Reader, ReadToIterator};
let source = std::io::stdin();
let source = source.lock(); // this implements std::io::Read
let mut reader = Reader::new(ReadToIterator::new(source));

pub fn unwrap(self) -> T[src]

Destroy this reader, unwrapping the underlying iterator.

pub fn read<U>(&mut self) -> Result<U> where
    U: TryFromJson
[src]

pub fn read_prop<U>(&mut self, prop: &'static str) -> Result<U> where
    U: TryFromJson
[src]

pub fn read_index<U>(&mut self) -> Result<U> where
    U: TryFromJson
[src]

pub fn format_error(&self, msg: &str) -> Error[src]

pub fn format_error_fmt(&self, args: Arguments<'_>) -> Error[src]

pub fn read_bytes(&mut self) -> Result<&[u8]>[src]

pub fn read_blob(&mut self) -> Result<Vec<u8>>[src]

This function allows us to exploit standard JSON containers to pass binary data (BLOBs, binary large objects, or Vec<u8>). Does JSON standard strictly prohibit us to do so? Lets understand the principle behind this criminal operation.

Mr. JSON wants us to interchange only unicode strings. But he doesn't specify what unicode it must be: utf-8, utf-16, or other. What is invalid utf-8 can be valid utf-16, and what is invalid utf-16 can be valid something else. Furthermore, passing invalid strings is normal, it happens every day. The receiver of invalid string must reject it immediately, and not use it for his profit. This library obeys to this requirement, and invalid utf-8 (only this encoding is supported) will fail conversion from Vec<u8> to String. So reading an invalid utf-8 byte sequence to a String variable will really return error. But nop-json library can optionally return you the Vec<u8> and tell you to convert it to String by yourself. And you may decide not to do so, and get your hands on this useful byte sequence.

The trouble here is only with bytes in range 80 - FF. Here is how we can encode our binary object:

  1. 00 - 1F - we can encode with the \u00xx encoding - this is the only option.
  2. 20 - 7F except " and \ - we can leave intact - they are valid utf-8 JSON, or optionally we can encode them with \u00xx.
  3. " and \ - escape with a slash.
  4. 80 - FF - if we leave them as they are, this will make our string invalid utf-8 (but valid JSON container). Ask yourself can you live with this. Another option could be to encode these characters with \u00xx sequences, but \u0080 expands to 2-byte utf-8 character.
use nop_json::Reader;

let mut reader = Reader::new(b" \"\x80\x81\" ".iter().map(|i| *i));

let data = reader.read_blob().unwrap();
assert_eq!(data, b"\x80\x81");

pub fn pipe_blob<U>(&mut self, writer: &mut U) -> Result<()> where
    U: Write
[src]

pub fn read_object<F>(&mut self, on_value: F) -> Result<bool> where
    F: FnMut(&mut Self, String) -> Result<()>, 
[src]

pub fn read_object_use_buffer<F>(&mut self, on_value: F) -> Result<bool> where
    F: FnMut(&mut Self) -> Result<()>, 
[src]

pub fn get_key(&self) -> &[u8][src]

pub fn read_array<F>(&mut self, on_value: F) -> Result<bool> where
    F: FnMut(&mut Self) -> Result<()>, 
[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Reader<T> where
    T: RefUnwindSafe

impl<T> Send for Reader<T> where
    T: Send

impl<T> Sync for Reader<T> where
    T: Sync

impl<T> Unpin for Reader<T> where
    T: Unpin

impl<T> UnwindSafe for Reader<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.