pub mod json {
use crate::{Context, Parse};
#[cfg(feature = "parse-impl")]
pub use ::serde_json::{de, error, map, ser, value};
use serde::{Deserialize, Serialize};
pub use serde_json::{Map, Number, Value};
pub struct Json<T>(std::marker::PhantomData<T>);
impl<T: for<'a> Deserialize<'a>> Parse for Json<T> {
type Output = T;
fn parse_borrowed(x: &str) -> crate::Result<Self::Output> {
parse(x)
}
fn parse_read(x: impl std::io::Read) -> crate::Result<Self::Output> {
serde_json::from_reader(x).with_context(|| {
format!(
"failed to parse input as json into {}",
std::any::type_name::<T>()
)
})
}
}
#[inline(always)]
pub fn parse<'a, T: Deserialize<'a>>(x: &'a str) -> crate::Result<T> {
serde_json::from_str(x).with_context(|| {
format!(
"failed to parse input as json into {}",
std::any::type_name::<T>()
)
})
}
#[inline(always)]
pub fn read<T: for<'a> Deserialize<'a>>(x: impl std::io::Read) -> crate::Result<T> {
Json::<T>::parse_read(x)
}
#[cfg(feature = "coroutine")]
pub async fn co_read<T: for<'a> Deserialize<'a>>(
x: impl tokio::io::AsyncRead + Unpin,
) -> crate::Result<T> {
crate::co_read::<Json<T>>(x).await
}
pub fn from_value<T: for<'a> Deserialize<'a>>(x: Value) -> crate::Result<T> {
use crate::Context;
crate::check!(
serde_json::from_value(x),
"failed to convert json value to {}",
std::any::type_name::<T>()
)
}
pub fn to_value<T: ?Sized + Serialize>(x: &T) -> crate::Result<Value> {
use crate::Context;
crate::check!(
serde_json::to_value(x),
"failed to convert {} to json value",
std::any::type_name::<T>()
)
}
pub fn stringify<T: ?Sized + Serialize>(x: &T) -> crate::Result<String> {
use crate::Context;
crate::check!(
serde_json::to_string(x),
"failed to serialize {} to json",
std::any::type_name::<T>()
)
}
pub fn stringify_pretty<T: ?Sized + Serialize>(x: &T) -> crate::Result<String> {
use crate::Context;
crate::check!(
serde_json::to_string_pretty(x),
"failed to serialize {} to json",
std::any::type_name::<T>()
)
}
pub fn write<T: ?Sized + Serialize>(w: impl std::io::Write, x: &T) -> crate::Result<()> {
use crate::Context;
crate::check!(
serde_json::to_writer(w, x),
"failed to write serialized {} as json",
std::any::type_name::<T>()
)
}
pub fn write_pretty<T: ?Sized + Serialize>(w: impl std::io::Write, x: &T) -> crate::Result<()> {
use crate::Context;
crate::check!(
serde_json::to_writer_pretty(w, x),
"failed to write serialized {} as json",
std::any::type_name::<T>()
)
}
}
#[cfg_attr(any(docsrs, feature = "nightly"), doc(cfg(feature = "json")))]
pub use serde_json::json;