#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![cfg_attr(not(test), no_std)]
#[cfg(feature = "alloc")]
extern crate alloc;
mod io;
mod stack;
mod string;
mod number;
mod deserializer;
pub use io::Read;
use io::PeekableRead;
pub use stack::*;
use string::*;
pub use number::{NumberSink, Number};
pub use deserializer::{Deserializer, Value};
use deserializer::*;
#[derive(Debug)]
pub enum JsonError<'read, R: Read<'read>, S: Stack> {
InternalError,
ReadError(R::Error),
StackError(S::Error),
ReusedDeserializer,
InvalidKey,
InvalidKeyValueDelimiter,
InvalidValue,
NotUtf8,
TrailingComma,
MismatchedDelimiter,
TypeError,
}
impl<'read, R: Read<'read>, S: Stack> Clone for JsonError<'read, R, S> {
#[inline(always)]
fn clone(&self) -> Self {
*self
}
}
impl<'read, R: Read<'read>, S: Stack> Copy for JsonError<'read, R, S> {}
pub enum Type {
Object,
Array,
String,
Number,
Bool,
Null,
}
#[inline(always)]
fn kind<'read, R: Read<'read>>(reader: &PeekableRead<'read, R>) -> Type {
match reader.peek() {
b'{' => Type::Object,
b'[' => Type::Array,
b'"' => Type::String,
b't' | b'f' => Type::Bool,
b'n' => Type::Null,
_ => Type::Number,
}
}
pub struct Field<'read, 'parent, R: Read<'read>, S: Stack> {
key: StringKey<'read, 'parent, R, S>,
}
#[inline(always)]
fn handle_field<'read, 'parent, R: Read<'read>, S: Stack>(
deserializer: &'parent mut Deserializer<'read, R, S>,
) -> Field<'read, 'parent, R, S> {
Field { key: StringKey(Some(String::read(deserializer))) }
}
#[inline(always)]
fn handle_string_value<'read, 'parent, R: Read<'read>, S: Stack>(
deserializer: &'parent mut Deserializer<'read, R, S>,
) -> StringValue<'read, 'parent, R, S> {
StringValue(String::read(deserializer))
}
impl<'read, 'parent, R: Read<'read>, S: Stack> Field<'read, 'parent, R, S> {
#[inline(always)]
pub fn key(
&mut self,
) -> &mut (impl use<'read, 'parent, R, S> + Iterator<Item = Result<char, JsonError<'read, R, S>>>)
{
&mut self.key
}
#[inline(always)]
pub fn value(mut self) -> Value<'read, 'parent, R, S> {
Value { deserializer: self.key.drop() }
}
}
impl<'read, 'parent, R: Read<'read>, S: Stack> Drop for Field<'read, 'parent, R, S> {
#[inline(always)]
fn drop(&mut self) {
drop(Value { deserializer: self.key.drop() });
}
}
pub struct FieldIterator<'read, 'parent, R: Read<'read>, S: Stack> {
deserializer: &'parent mut Deserializer<'read, R, S>,
done: bool,
}
impl<'read, 'parent, R: Read<'read>, S: Stack> Drop for FieldIterator<'read, 'parent, R, S> {
#[inline(always)]
fn drop(&mut self) {
if self.deserializer.error.is_some() {
return;
}
loop {
let Some(next) = self.next() else { break };
let next = next.map(|_| ());
match next {
Ok(()) => {}
Err(e) => {
self.deserializer.error = Some(e);
break;
}
}
}
}
}
impl<'read, 'parent, R: Read<'read>, S: Stack> FieldIterator<'read, 'parent, R, S> {
#[allow(clippy::type_complexity, clippy::should_implement_trait)]
pub fn next(&mut self) -> Option<Result<Field<'read, '_, R, S>, JsonError<'read, R, S>>> {
if let Some(err) = self.deserializer.error {
return Some(Err(err));
}
if self.done {
None?;
}
loop {
let result = match self.deserializer.single_step() {
Ok(SingleStepResult::Object(result)) => result,
Ok(_) => break Some(Err(JsonError::InternalError)),
Err(e) => break Some(Err(e)),
};
match result {
SingleStepObjectResult::Field => break Some(Ok(handle_field(self.deserializer))),
SingleStepObjectResult::Closed => {
self.done = true;
None?
}
}
}
}
}
pub struct ArrayIterator<'read, 'parent, R: Read<'read>, S: Stack> {
deserializer: &'parent mut Deserializer<'read, R, S>,
done: bool,
}
impl<'read, 'parent, R: Read<'read>, S: Stack> Drop for ArrayIterator<'read, 'parent, R, S> {
#[inline(always)]
fn drop(&mut self) {
if self.deserializer.error.is_some() {
return;
}
loop {
let Some(next) = self.next() else { break };
let next = next.map(|_| ());
match next {
Ok(()) => {}
Err(e) => {
self.deserializer.error = Some(e);
break;
}
}
}
}
}
impl<'read, 'parent, R: Read<'read>, S: Stack> ArrayIterator<'read, 'parent, R, S> {
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Option<Result<Value<'read, '_, R, S>, JsonError<'read, R, S>>> {
if let Some(err) = self.deserializer.error {
return Some(Err(err));
}
if self.done {
None?;
}
loop {
let result = match self.deserializer.single_step() {
Ok(SingleStepResult::Array(result)) => result,
Ok(_) => break Some(Err(JsonError::InternalError)),
Err(e) => break Some(Err(e)),
};
match result {
SingleStepArrayResult::Value => {
break Some(Ok(Value { deserializer: Some(self.deserializer) }))
}
SingleStepArrayResult::Closed => {
self.done = true;
None?
}
}
}
}
}
impl<'read, 'parent, R: Read<'read>, S: Stack> Value<'read, 'parent, R, S> {
#[inline(always)]
pub fn kind(&mut self) -> Result<Type, JsonError<'read, R, S>> {
Ok(kind(&self.deserializer.as_ref().ok_or(JsonError::InternalError)?.reader))
}
#[inline(always)]
pub fn fields(mut self) -> Result<FieldIterator<'read, 'parent, R, S>, JsonError<'read, R, S>> {
if !matches!(self.kind()?, Type::Object) {
Err(JsonError::TypeError)?
}
let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
match deserializer.single_step()? {
SingleStepResult::Unknown(SingleStepUnknownResult::ObjectOpened) => {
Ok(FieldIterator { deserializer, done: false })
}
_ => Err(JsonError::InternalError),
}
}
#[inline(always)]
pub fn iterate(mut self) -> Result<ArrayIterator<'read, 'parent, R, S>, JsonError<'read, R, S>> {
if !matches!(self.kind()?, Type::Array) {
Err(JsonError::TypeError)?
}
let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
match deserializer.single_step()? {
SingleStepResult::Unknown(SingleStepUnknownResult::ArrayOpened) => {
Ok(ArrayIterator { deserializer, done: false })
}
_ => Err(JsonError::InternalError),
}
}
#[inline(always)]
pub fn to_str(
mut self,
) -> Result<
impl use<'read, 'parent, R, S> + Iterator<Item = Result<char, JsonError<'read, R, S>>>,
JsonError<'read, R, S>,
> {
if !matches!(self.kind()?, Type::String) {
Err(JsonError::TypeError)?
}
let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
match deserializer.single_step()? {
SingleStepResult::Unknown(SingleStepUnknownResult::String) => {
Ok(handle_string_value(deserializer))
}
_ => Err(JsonError::InternalError),
}
}
#[inline(always)]
pub fn to_number(mut self) -> Result<Number, JsonError<'read, R, S>> {
if !matches!(self.kind()?, Type::Number) {
Err(JsonError::TypeError)?
}
let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
match deserializer.single_step()? {
SingleStepResult::Unknown(SingleStepUnknownResult::Number(number)) => Ok(number),
_ => Err(JsonError::InternalError),
}
}
#[inline(always)]
pub fn to_bool(mut self) -> Result<bool, JsonError<'read, R, S>> {
if !matches!(self.kind()?, Type::Bool) {
Err(JsonError::TypeError)?
}
let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
match deserializer.single_step()? {
SingleStepResult::Unknown(SingleStepUnknownResult::Bool(bool)) => Ok(bool),
_ => Err(JsonError::InternalError),
}
}
#[inline(always)]
pub fn to_null(mut self) -> Result<(), JsonError<'read, R, S>> {
if !matches!(self.kind()?, Type::Null) {
Err(JsonError::TypeError)?
}
let deserializer = self.deserializer.take().ok_or(JsonError::InternalError)?;
match deserializer.single_step()? {
SingleStepResult::Unknown(SingleStepUnknownResult::Null) => Ok(()),
_ => Err(JsonError::InternalError),
}
}
}