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 parse_jsonc(
text: &'text str,
) -> Result<(Self, Vec<Range<usize>>), JsonParseError>
pub fn parse_jsonc( text: &'text str, ) -> Result<(Self, Vec<Range<usize>>), JsonParseError>
Parses a JSONC (JSON with Comments) string into a RawJson 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.
§Example
let text = r#"{
"name": "John", // This is a comment
"age": 30,
/* This is a block comment */
"city": "New York", // Trailing comma is allowed
}"#;
let (json, comment_ranges) = RawJson::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<'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");Sourcepub fn into_owned(self) -> RawJsonOwned
pub fn into_owned(self) -> RawJsonOwned
Converts this borrowed RawJson into an owned RawJsonOwned.
This method creates an owned copy of the JSON data, allowing it to be used
beyond the lifetime of the original text. The resulting RawJsonOwned
contains its own copy of the JSON text and can be moved freely.
§Example
let text = r#"{"name": "John", "age": 30}"#;
let json = RawJson::parse(text)?;
let owned_json = json.into_owned();
// The owned version can outlive the original text
drop(text);
assert_eq!(owned_json.text(), r#"{"name": "John", "age": 30}"#);