pub struct RawJsonOwned { /* private fields */ }Expand description
Owned version of RawJson.
Implementations§
Source§impl RawJsonOwned
impl RawJsonOwned
Sourcepub fn parse<T>(text: T) -> Result<Self, JsonParseError>
pub fn parse<T>(text: T) -> Result<Self, JsonParseError>
Parses a JSON string into a RawJsonOwned instance.
This validates the JSON syntax without converting values to Rust types.
Unlike RawJson::parse, this creates an owned version that doesn’t
borrow from the input string.
§Example
let text = r#"{"name": "John", "age": 30}"#;
let json = RawJsonOwned::parse(text)?;Sourcepub fn parse_jsonc<T>(
text: T,
) -> Result<(Self, Vec<Range<usize>>), JsonParseError>
pub fn parse_jsonc<T>( text: T, ) -> Result<(Self, Vec<Range<usize>>), JsonParseError>
Parses a JSONC (JSON with Comments) string into a RawJsonOwned instance.
This validates the JSONC syntax and strips out comments, returning both
the parsed JSON structure and the byte ranges where comments were found
in the original text. Comments can be either line comments (//) or
block comments (/* */). Additionally, this parser allows trailing commas
in objects and arrays.
Unlike RawJson::parse_jsonc, this creates an owned version that doesn’t
borrow from the input string.
§Example
let text = r#"{
"name": "John", // This is a comment
"age": 30,
/*
* This is a multi-line
* block comment
*/
"city": "New York", // Trailing comma is allowed
}"#;
let (json, comment_ranges) = RawJsonOwned::parse_jsonc(text)?;
// The parsed JSON works normally
let name: String = json.value().to_member("name")?.required()?.try_into()?;
assert_eq!(name, "John");
// Comment ranges indicate where comments were found in the original text
assert_eq!(comment_ranges.len(), 3); // Three comments found
// You can extract the comment text if needed
let first_comment = &text[comment_ranges[0].clone()];
assert!(first_comment.contains("This is a comment"));Sourcepub fn value(&self) -> RawJsonValue<'_, '_>
pub fn value(&self) -> RawJsonValue<'_, '_>
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 = RawJsonOwned::parse(text).unwrap();
let value = json.value();Sourcepub fn get_value_by_position(
&self,
position: usize,
) -> Option<RawJsonValue<'_, '_>>
pub fn get_value_by_position( &self, position: usize, ) -> Option<RawJsonValue<'_, '_>>
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 = RawJsonOwned::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");Trait Implementations§
Source§impl Clone for RawJsonOwned
impl Clone for RawJsonOwned
Source§fn clone(&self) -> RawJsonOwned
fn clone(&self) -> RawJsonOwned
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more