pub struct RawJson<'text> { /* private fields */ }Expand description
Parsed JSON text (syntactically correct, but not yet converted to Rust types).
This struct holds a JSON text in its original form (i.e., JSON integers are not converted to Rust’s integers), while ensuring the text is valid JSON syntax.
RawJson maintains index information about each JSON value in the text,
including its type (JsonValueKind) and the start and end byte positions.
You can traverse the JSON structure by accessing the top-level value
via RawJson::value(), which returns a RawJsonValue
that provides methods to explore nested elements and convert them into Rust types.
Note that, for simple use cases,
using Json, which internally uses RawJson, is a more convenient way to parse JSON text into Rust types.
Implementations§
Source§impl<'text> RawJson<'text>
impl<'text> RawJson<'text>
Sourcepub fn parse(text: &'text str) -> Result<Self, JsonParseError>
pub fn parse(text: &'text str) -> Result<Self, JsonParseError>
Sourcepub fn value(&self) -> RawJsonValue<'text, '_>
pub fn value(&self) -> RawJsonValue<'text, '_>
Returns the top-level value of the JSON.
This value can be used as an entry point to traverse the entire JSON structure and convert it to Rust types.
§Example
let text = r#"{"name": "John", "age": 30}"#;
let json = RawJson::parse(text).unwrap();
let value = json.value();Sourcepub fn get_value_by_position(
&self,
position: usize,
) -> Option<RawJsonValue<'text, '_>>
pub fn get_value_by_position( &self, position: usize, ) -> Option<RawJsonValue<'text, '_>>
Finds the JSON value at the specified byte position in the original text.
This method traverses the JSON structure to find the most specific value
that contains the given position.
It returns None if the position is outside the bounds of the JSON text.
This method is useful for retrieving the context
where a JsonParseError::InvalidValue error occurred.
§Example
let json = RawJson::parse(r#"{"name": "John", "age": 30}"#)?;
// Position at "name" key
let name_value = json.get_value_by_position(2).expect("infallible");
assert_eq!(name_value.as_raw_str(), r#""name""#);
// Position at number value
let age_value = json.get_value_by_position(25).expect("infallible");
assert_eq!(age_value.as_raw_str(), "30");