Struct Decoder

Source
pub struct Decoder<I: Iterator> { /* private fields */ }
Expand description

JSON decoder over char iterators.

Implementations§

Source§

impl<I: Iterator<Item = char>> Decoder<I>

Source

pub fn new(c: Config, i: I) -> Decoder<I>

Source

pub fn default(i: I) -> Decoder<I>

Create decoder with default Config.

Source

pub fn is_end(&mut self) -> bool

Have we exhausted the iterator?

Source

pub fn into_iter(self) -> I

Source

pub fn iter_mut(&mut self) -> &mut I

Source

pub fn from_json<T: FromJson>(&mut self) -> DecodeResult<T>

Generic conversion from JSON to an instance of FromJson.

Source

pub fn decode(&mut self) -> DecodeResult<Json>

Decode into Json AST value.

Source

pub fn skip(&mut self) -> DecodeResult<()>

Decode as generic Json but ignore the result.

Semantically this method is equivalent to Decoder::decode but potentially more efficient as it tries to avoid allocating intermediate values.

Source

pub fn u8(&mut self) -> DecodeResult<u8>

Source

pub fn u16(&mut self) -> DecodeResult<u16>

Source

pub fn u32(&mut self) -> DecodeResult<u32>

Source

pub fn u64(&mut self) -> DecodeResult<u64>

Source

pub fn u128(&mut self) -> DecodeResult<u128>

Source

pub fn usize(&mut self) -> DecodeResult<usize>

Source

pub fn i8(&mut self) -> DecodeResult<i8>

Source

pub fn i16(&mut self) -> DecodeResult<i16>

Source

pub fn i32(&mut self) -> DecodeResult<i32>

Source

pub fn i64(&mut self) -> DecodeResult<i64>

Source

pub fn i128(&mut self) -> DecodeResult<i128>

Source

pub fn isize(&mut self) -> DecodeResult<isize>

Source

pub fn null(&mut self) -> DecodeResult<()>

Source

pub fn bool(&mut self) -> DecodeResult<bool>

Source

pub fn string(&mut self) -> DecodeResult<String>

Source

pub fn str( &mut self, b: &mut Utf8Buffer<'_>, overflow_err: bool, ) -> DecodeResult<()>

Decode a JSON string into the given Utf8Buffer.

If the actual string does not fit into the provided buffer and overflow_err is true, DecodeError::BufferOverflow is returned immediately. If overflow_err is false we continue decoding the string, but the buffer will only contain as much as fits into it.

Source

pub fn optional<A, F>(&mut self, f: F) -> DecodeResult<Option<A>>
where F: FnMut(&mut Decoder<I>) -> DecodeResult<A>,

Decode null (which is mapped to None) or some other JSON value (mapped to Some).

Source

pub fn has_more(&mut self) -> DecodeResult<bool>

When parsing JSON arrays and objects this needs to be called in between to check if the array / object end has been reached. E.g.

use json_codec_wasm::{Decoder, DecodeResult};

fn foo<I: Iterator<Item=char>>(d: &mut Decoder<I>) -> DecodeResult<Vec<u8>> {
    d.array()?;
    let mut v = Vec::new();
    while d.has_more()? {
        v.push(d.u8()?)
    }
    Ok(v)
}
Source

pub fn array(&mut self) -> DecodeResult<()>

Begin parsing a JSON array.

Before each element is parsed, Decoder::has_more needs to be queried.

Source

pub fn array_iter<A: FromJson>(&mut self) -> DecodeResult<ArrayIter<'_, A, I>>

Create a JSON array iterator to decode a homogenous array.

use json_codec_wasm::Decoder;

let mut d = Decoder::default("[1,2,3,4]".chars());
let mut v: Vec<u8> = Vec::new();

for i in d.array_iter().unwrap() {
    v.push(i.unwrap())
}
Source

pub fn object(&mut self) -> DecodeResult<()>

Begin parsing a JSON object.

Before each key is parsed, Decoder::has_more needs to be queried.

Source

pub fn key(&mut self) -> DecodeResult<String>

Decode a JSON object key.

Source

pub fn key_str( &mut self, b: &mut Utf8Buffer<'_>, overflow_err: bool, ) -> DecodeResult<()>

Decode a JSON object key into the given buffer.

Auto Trait Implementations§

§

impl<I> Freeze for Decoder<I>
where I: Freeze, <I as Iterator>::Item: Freeze,

§

impl<I> RefUnwindSafe for Decoder<I>

§

impl<I> Send for Decoder<I>
where I: Send, <I as Iterator>::Item: Send,

§

impl<I> Sync for Decoder<I>
where I: Sync, <I as Iterator>::Item: Sync,

§

impl<I> Unpin for Decoder<I>
where I: Unpin, <I as Iterator>::Item: Unpin,

§

impl<I> UnwindSafe for Decoder<I>
where I: UnwindSafe, <I as Iterator>::Item: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.