pub mod impls;
mod minify;
pub mod parser;
mod prettify;
pub mod raw;
pub mod stream;
mod traits;
pub mod writer;
pub use impls::{FromJsonKey, ToJsonKey};
pub use minify::{minify, minify_into, minify_into_with, minify_with};
pub use parser::{JsonStr, MAX_DEPTH, Parser};
pub use prettify::{
prettify, prettify_into, prettify_into_with, prettify_value_into, prettify_with,
};
pub use raw::Raw;
pub use stream::{
Documents, Feed, Iter, Mode, from_reader, from_reader_with, to_writer, to_writer_buffered,
to_writer_buffered_with, to_writer_with,
};
pub use traits::{
Read, ReadArray, ReadAs, ReadEnum, ReadInternallyTagged, ReadKeyAs, ReadObject, ReadOwned,
ReadWrite, Write, WriteArray, WriteAs, WriteKeyAs, WriteObject,
};
pub use writer::{Writer, quoted_key};
use crate::error::{Error, ErrorCode, Result};
use crate::options::{Options, Standard};
#[inline]
pub fn from_str<'de, T>(input: &'de str) -> Result<T>
where
T: Read<'de> + Default,
{
from_str_with::<Standard, T>(input)
}
#[inline]
pub fn from_str_with<'de, O, T>(input: &'de str) -> Result<T>
where
O: Options,
T: Read<'de> + Default,
{
let mut value = T::default();
read_into_with::<O, T>(&mut value, input)?;
Ok(value)
}
#[inline]
pub fn read_into<'de, T>(value: &mut T, input: &'de str) -> Result<()>
where
T: Read<'de>,
{
read_into_with::<Standard, T>(value, input)
}
#[inline]
pub fn read_into_with<'de, O, T>(value: &mut T, input: &'de str) -> Result<()>
where
O: Options,
T: Read<'de>,
{
let mut p = Parser::<O>::with_options(input);
p.skip_ws();
match value.read(&mut p).and_then(|()| p.finish()) {
Ok(()) => Ok(()),
Err(code) => Err(Error::with_key(code, p.position(), p.error_key())),
}
}
#[inline]
pub fn from_slice<'de, T>(input: &'de [u8]) -> Result<T>
where
T: Read<'de> + Default,
{
from_slice_with::<Standard, T>(input)
}
#[inline]
pub fn from_slice_with<'de, O, T>(input: &'de [u8]) -> Result<T>
where
O: Options,
T: Read<'de> + Default,
{
let s = core::str::from_utf8(input)
.map_err(|e| Error::new(ErrorCode::InvalidUtf8, e.valid_up_to()))?;
from_str_with::<O, T>(s)
}
#[inline]
pub fn to_string<T: Write + ?Sized>(value: &T) -> String {
to_string_with::<Standard, T>(value)
}
#[inline]
pub fn to_string_with<O: Options, T: Write + ?Sized>(value: &T) -> String {
let mut w = Writer::<O>::new();
value.write(&mut w);
w.into_string()
}
#[inline]
pub fn write_into<T: Write + ?Sized>(value: &T, out: &mut String) {
write_into_with::<Standard, T>(value, out);
}
#[inline]
pub fn write_into_with<O: Options, T: Write + ?Sized>(value: &T, out: &mut String) {
let buf = core::mem::take(out).into_bytes();
let mut w = Writer::<O>::from_vec(buf);
value.write(&mut w);
*out = w.into_string();
}
#[inline]
pub fn append<T: Write + ?Sized>(value: &T, out: &mut Vec<u8>) {
append_with::<Standard, T>(value, out);
}
#[inline]
pub fn append_with<O: Options, T: Write + ?Sized>(value: &T, out: &mut Vec<u8>) {
writer::append_in_place(out, Writer::<O>::appending, |w| value.write(w));
}
#[inline]
pub fn to_vec<T: Write + ?Sized>(value: &T) -> Vec<u8> {
to_vec_with::<Standard, T>(value)
}
#[inline]
pub fn to_vec_with<O: Options, T: Write + ?Sized>(value: &T) -> Vec<u8> {
let mut w = Writer::<O>::new();
value.write(&mut w);
w.into_vec()
}