Skip to content

Commit

Permalink
Introduce from_str_owned in the json module
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Oct 4, 2024
1 parent 8a75181 commit a2853a9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion merde_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Error Handling and Field Type
// -------------------------------------------------------------------------

use crate::{deserialize2::EventType, CowStr, IntoStatic, Value};
use crate::{deserialize::EventType, CowStr, IntoStatic, Value};

/// A content-less variant of the [`Value`] enum, used for reporting errors, see [`MerdeError::MismatchedType`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
14 changes: 7 additions & 7 deletions merde_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ pub use with_lifetime::WithLifetime;
mod value;
pub use value::Value;

mod deserialize2;
pub use deserialize2::ArrayStart;
pub use deserialize2::Deserialize;
pub use deserialize2::DeserializeOwned;
pub use deserialize2::Deserializer;
pub use deserialize2::Event;
pub use deserialize2::EventType;
mod deserialize;
pub use deserialize::ArrayStart;
pub use deserialize::Deserialize;
pub use deserialize::DeserializeOwned;
pub use deserialize::Deserializer;
pub use deserialize::Event;
pub use deserialize::EventType;

rubicon::compatibility_check! {
("merde_core_pkg_version", env!("CARGO_PKG_VERSION")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub(crate) fn cowify<'j>(src: &'j [u8], s: &str) -> CowStr<'j> {

#[cfg(test)]
mod tests {
use crate::deserialize2::cowify;
use crate::deserialize::cowify;

use super::JsonDeserializer;
use merde_core::{Array, CowStr, Deserialize, Deserializer, Event, EventType, Map, MerdeError};
Expand Down
18 changes: 15 additions & 3 deletions merde_json/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]

mod deserialize2;
pub use deserialize2::JsonDeserializer;
mod deserialize;
pub use deserialize::JsonDeserializer;

mod jiter_lite;

use jiter_lite::errors::JiterError;
use merde_core::{Array, CowStr, Deserialize, Deserializer, IntoStatic, Map, MerdeError, Value};
use merde_core::{
Array, CowStr, Deserialize, DeserializeOwned, Deserializer, IntoStatic, Map, MerdeError, Value,
};

use std::borrow::Cow;
use std::collections::HashMap;
Expand Down Expand Up @@ -604,6 +606,16 @@ where
deser.deserialize::<T>()
}

/// Deserialize an instance of type `T` from a string of JSON text,
/// and return its static variant e.g. (CowStr<'static>, etc.)
pub fn from_str_owned<T>(s: &str) -> Result<T, MerdeJsonError<'_>>
where
T: DeserializeOwned,
{
let mut deser = JsonDeserializer::new(s);
T::deserialize_owned(&mut deser)
}

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

0 comments on commit a2853a9

Please sign in to comment.