Document types in JSON module
//! De/Serialization of JSON
//!
-//! This modules is only available if using the `json` feature of the crate.
+//! This modules is only available when using the `json` feature of the crate.
use crate::{de::DeserializeAs, ser::SerializeAs};
use serde::{de::DeserializeOwned, Deserializer, Serialize, Serializer};
/// Serialize value as string containing JSON
///
+/// The same functionality is also available as [`serde_with::json::JsonString`][crate::json::JsonString] compatible with the `serde_as`-annotation.
+///
/// # Examples
///
/// ```
@@ -30,7 +32,6 @@ use serde::{de::DeserializeOwned, Deserializer, Serialize, Serializer};
/// };
/// assert_eq!(r#"{"other_struct":"{\"value\":10}"}"#, serde_json::to_string(&x).unwrap());
/// ```
-///
pub mod nested {
use serde::{
de::{DeserializeOwned, Deserializer, Error, Visitor},
@@ -84,6 +85,37 @@ pub mod nested {
}
}
+/// Serialize value as string containing JSON
+///
+/// The same functionality is also available as [`serde_with::json::nested`][crate::json::nested] compatible with serde's with-annotation.
+///
+/// # Examples
+///
+/// ```
+/// # #[cfg(features = "macros")] {
+/// # use serde_derive::{Deserialize, Serialize};
+/// # use serde_derive::{serde_as, json::JsonString};
+/// #
+/// #[serde_as]
+/// #[derive(Deserialize, Serialize)]
+/// struct A {
+/// #[serde_as(as = "JsonString")]
+/// other_struct: B,
+/// }
+/// #[derive(Deserialize, Serialize)]
+/// struct B {
+/// value: usize,
+/// }
+///
+/// let v: A = serde_json::from_str(r#"{"other_struct":"{\"value\":5}"}"#).unwrap();
+/// assert_eq!(5, v.other_struct.value);
+///
+/// let x = A {
+/// other_struct: B { value: 10 },
+/// };
+/// assert_eq!(r#"{"other_struct":"{\"value\":10}"}"#, serde_json::to_string(&x).unwrap());
+/// # }
+/// ```
#[derive(Copy, Clone, Debug, Default)]
pub struct JsonString;
