[−][src]Struct nop_json::Reader
Methods
impl<T> Reader<T> where
T: Read,
[src]
T: Read,
pub fn new(reader: T) -> Reader<T>
[src]
pub fn read<U>(&mut self) -> Result<U> where
U: TryFromJson,
[src]
U: TryFromJson,
pub fn read_prop<U>(&mut self, prop: &'static str) -> Result<U> where
U: TryFromJson,
[src]
U: TryFromJson,
pub fn read_index<U>(&mut self) -> Result<U> where
U: TryFromJson,
[src]
U: TryFromJson,
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:
00 - 1F
- we can encode with the\u00xx
encoding - this is the only option.20 - 7F
except"
and\
- we can leave intact - they are valid utf-8 JSON, or optionally we can encode them with\u00xx
."
and\
- escape with a slash.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\" "[..]); 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]
U: Write,
pub fn read_object<F>(&mut self, on_value: F) -> Result<bool> where
F: FnMut(&mut Self, String) -> Result<()>,
[src]
F: FnMut(&mut Self, String) -> Result<()>,
pub fn read_object_use_buffer<F>(&mut self, on_value: F) -> Result<bool> where
F: FnMut(&mut Self) -> Result<()>,
[src]
F: FnMut(&mut Self) -> Result<()>,
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]
F: FnMut(&mut Self) -> Result<()>,
Auto Trait Implementations
impl<T> RefUnwindSafe for Reader<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for Reader<T> where
T: Send,
T: Send,
impl<T> Sync for Reader<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Reader<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Reader<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,