Skip to main content

parse_json_checked

Function parse_json_checked 

Source
pub fn parse_json_checked<T: DeserializeOwned>(bytes: &[u8]) -> Result<T, Error>
Expand description

Deserializes bytes into T, first rejecting payloads whose JSON nesting exceeds MAX_JSON_NESTING_DEPTH (see that constant’s doc for why).

The single shared entry point for every untrusted-JSON parse site in this workspace — collapses what would otherwise be a per-crate copy of check_json_nesting_depth + serde_json::from_slice into one call, and returns a serde_json::Error so a too-deep payload converts into a caller’s DepsError exactly like any other malformed-JSON failure (via ?, .map_err(..), or .ok()).

§Errors

Returns an error if bytes nests deeper than MAX_JSON_NESTING_DEPTH, or serde_json’s own error if bytes is not valid JSON matching T.

§Examples

use deps_core::parser::parse_json_checked;

let value: serde_json::Value = parse_json_checked(br#"{"a":1}"#).unwrap();
assert_eq!(value["a"], 1);

let deeply_nested = format!("{}1{}", "[".repeat(100), "]".repeat(100));
assert!(parse_json_checked::<serde_json::Value>(deeply_nested.as_bytes()).is_err());