Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce to_string and other top-level functions for serde_json drop-in replacement #9

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use jiter::JsonValue;

/// A content-less variant of the [JsonValue] enum, used for reporting errors, see [MerdeJsonError::MismatchedType].
#[derive(Debug)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum JsonFieldType {
/// The JSON value is `null`.
Expand Down Expand Up @@ -78,6 +78,9 @@ pub enum MerdeJsonError {

/// While parsing a [time::Date] or [time::Time], we got an error.
InvalidDateTimeValue,

/// An I/O error occurred.
Io(std::io::Error),
}

impl From<jiter::JsonError> for MerdeJsonError {
Expand All @@ -86,6 +89,12 @@ impl From<jiter::JsonError> for MerdeJsonError {
}
}

impl From<std::io::Error> for MerdeJsonError {
fn from(e: std::io::Error) -> Self {
MerdeJsonError::Io(e)
}
}

impl std::fmt::Display for MerdeJsonError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -120,6 +129,9 @@ impl std::fmt::Display for MerdeJsonError {
MerdeJsonError::InvalidDateTimeValue => {
write!(f, "Invalid date/time value")
}
MerdeJsonError::Io(e) => {
write!(f, "I/O error: {}", e)
}
}
}
}
Expand Down
35 changes: 31 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,16 +1189,43 @@ where
}
}

/// Parses a JSON byte string into a [JsonValue].
pub fn from_slice(data: &[u8]) -> Result<jiter::JsonValue<'_>, MerdeJsonError> {
/// Deserialize an instance of type `T` from bytes of JSON text.
pub fn from_slice(data: &[u8]) -> Result<JsonValue<'_>, MerdeJsonError> {
Ok(jiter::JsonValue::parse(data, false)?)
}

/// Parses a JSON string into a [JsonValue].
pub fn from_str(s: &str) -> Result<jiter::JsonValue<'_>, MerdeJsonError> {
/// Deserialize an instance of type `T` from a string of JSON text.
pub fn from_str(s: &str) -> Result<JsonValue<'_>, MerdeJsonError> {
from_slice(s.as_bytes())
}

/// Interpret a `JsonValue` as an instance of type `T`.
pub fn from_value<'src: 'val, 'val, T>(value: &'val JsonValue<'src>) -> Result<T, MerdeJsonError>
where
T: JsonDeserialize<'src, 'val>,
{
T::json_deserialize(Some(value))
}

/// Serialize the given data structure as a String of JSON.
pub fn to_string<T: JsonSerialize>(value: &T) -> String {
value.to_json_string()
}

/// Serialize the given data structure as a JSON byte vector.
pub fn to_vec<T: JsonSerialize>(value: &T) -> Vec<u8> {
value.to_json_bytes()
}

/// Serialize the given data structure as JSON into the I/O stream.
pub fn to_writer<W, T>(mut writer: impl std::io::Write, value: &T) -> std::io::Result<()>
where
T: JsonSerialize,
{
let bytes = value.to_json_bytes();
writer.write_all(&bytes)
}

/// Allow turning a value into an "owned" variant, which can then be
/// returned, moved, etc.
///
Expand Down