A simple JSON parser written in Rust that converts JSON text into Rust data structures. The parser handles all standard JSON types including null, booleans, numbers, strings, arrays, and objects.
This library implements a JSON parser with the following features:
- Parses JSON text into structured Rust data types
- Provides error messages for invalid JSON
The parser supports the following JSON data types:
- Null:
null - Booleans:
true,false - Numbers: integers and floating-point (e.g.,
123,-45.67,1.23e-4) - Strings: text enclosed in double quotes, with escape sequences (e.g.,
"hello","line\nbreak") - Arrays: ordered lists of values (e.g.,
[1, 2, 3]) - Objects: collections of key-value pairs (e.g.,
{"name": "John", "age": 30})
The parser works by:
- Looking at the first character of the input to determine the type
- Calling the appropriate parsing method based on the type
- Building a
JsonValueenum variant that represents the parsed data - Handling any errors that might occur during parsing
The parser uses Rust's Peekable iterator, which is a key for effective parsing:
Peekableallows looking at the next character without consuming it- This "look-ahead" capability is essential for deciding how to parse different elements
- For example, when encountering a
{character, the parser knows to callparse_object() - The parser can check for delimiters (like commas and closing brackets) without removing them prematurely
For more details on using Peekable, refer to the Rust standard library documentation.