Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ rand = { version = "0.9.0", optional = true }
sha2 = { version = "0.10.9", optional = true }

[dev-dependencies]
serde_json = "1.0.89"
serde_json = { version = "1.0.89", features = ["unbounded_depth"] }
bytes-lit = "0.0.5"
rand = "0.9.0"

Expand Down
38 changes: 38 additions & 0 deletions src/generated/type_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13226,6 +13226,11 @@ impl Type {
Self::from_json(v, r)
}

/// Reads a value of the type from JSON.
///
/// Uses a serde_json deserializer with its default recursion limit, which
/// rejects JSON nested more than 128 levels deep. To read deeper JSON, see
/// [`Type::deserialize_json`].
#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self, Error> {
Expand Down Expand Up @@ -14544,6 +14549,28 @@ impl Type {
}
}

/// Reads a value of the type from JSON using the deserializer.
///
/// serde_json deserializers reject JSON nested more than 128 levels deep by
/// default. To read deeper JSON, enable serde_json's `unbounded_depth`
/// feature and disable the recursion limit on the deserializer:
///
/// ```
/// use stellar_xdr::{Type, TypeVariant};
///
/// let depth = 100;
/// let json = format!("{}\"void\"{}", r#"{"vec":["#.repeat(depth), "]}".repeat(depth));
///
/// let mut de = serde_json::Deserializer::from_str(&json);
/// de.disable_recursion_limit();
/// let t = Type::deserialize_json(TypeVariant::ScVal, &mut de)?;
/// de.end()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// **Warning:** Without the limit, values nested deeply enough can overflow
/// the stack while being read, and also when being cloned, compared, or
/// dropped.
#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(
Expand Down Expand Up @@ -16063,6 +16090,12 @@ impl Type {
}
}

/// Reads a value of the type from JSON, collecting the paths of any fields
/// in the JSON that were ignored.
///
/// Uses a serde_json deserializer with its default recursion limit, which
/// rejects JSON nested more than 128 levels deep. To read deeper JSON, see
/// [`Type::deserialize_json_collecting_ignored_fields`].
#[cfg(feature = "serde_ignored")]
pub fn from_json_collecting_ignored_fields(
v: TypeVariant,
Expand All @@ -16074,6 +16107,11 @@ impl Type {
Ok(result)
}

/// Reads a value of the type from JSON using the deserializer, collecting
/// the paths of any fields in the JSON that were ignored.
///
/// To read JSON nested deeper than serde_json's default recursion limit,
/// disable the limit on the deserializer, see [`Type::deserialize_json`].
#[cfg(feature = "serde_ignored")]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json_collecting_ignored_fields<'r, R: serde_json::de::Read<'r>>(
Expand Down
38 changes: 38 additions & 0 deletions xdr-generator-rust/generator/templates/type_enum.rs.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ impl Type {
Self::from_json(v, r)
}

/// Reads a value of the type from JSON.
///
/// Uses a serde_json deserializer with its default recursion limit, which
/// rejects JSON nested more than 128 levels deep. To read deeper JSON, see
/// [`Type::deserialize_json`].
#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn from_json(v: TypeVariant, r: impl Read) -> Result<Self, Error> {
Expand All @@ -306,6 +311,28 @@ impl Type {
}
}

/// Reads a value of the type from JSON using the deserializer.
///
/// serde_json deserializers reject JSON nested more than 128 levels deep by
/// default. To read deeper JSON, enable serde_json's `unbounded_depth`
/// feature and disable the recursion limit on the deserializer:
///
/// ```
/// use stellar_xdr::{Type, TypeVariant};
///
/// let depth = 100;
/// let json = format!("{}\"void\"{}", r#"{"vec":["#.repeat(depth), "]}".repeat(depth));
///
/// let mut de = serde_json::Deserializer::from_str(&json);
/// de.disable_recursion_limit();
/// let t = Type::deserialize_json(TypeVariant::ScVal, &mut de)?;
/// de.end()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// **Warning:** Without the limit, values nested deeply enough can overflow
/// the stack while being read, and also when being cloned, compared, or
/// dropped.
#[cfg(all(feature = "std", feature = "serde_json"))]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<Self, Error> {
Expand All @@ -319,6 +346,12 @@ impl Type {
}
}

/// Reads a value of the type from JSON, collecting the paths of any fields
/// in the JSON that were ignored.
///
/// Uses a serde_json deserializer with its default recursion limit, which
/// rejects JSON nested more than 128 levels deep. To read deeper JSON, see
/// [`Type::deserialize_json_collecting_ignored_fields`].
#[cfg(feature = "serde_ignored")]
pub fn from_json_collecting_ignored_fields(v: TypeVariant, r: impl Read) -> Result<(Self, Vec<String>), Error> {
let mut de = serde_json::Deserializer::from_reader(r);
Expand All @@ -327,6 +360,11 @@ impl Type {
Ok(result)
}

/// Reads a value of the type from JSON using the deserializer, collecting
/// the paths of any fields in the JSON that were ignored.
///
/// To read JSON nested deeper than serde_json's default recursion limit,
/// disable the limit on the deserializer, see [`Type::deserialize_json`].
#[cfg(feature = "serde_ignored")]
#[allow(clippy::too_many_lines)]
pub fn deserialize_json_collecting_ignored_fields<'r, R: serde_json::de::Read<'r>>(v: TypeVariant, r: &mut serde_json::de::Deserializer<R>) -> Result<(Self, Vec<String>), Error> {
Expand Down
Loading