2 stable releases
new 1.1.0 | Nov 16, 2024 |
---|---|
1.0.0 | Nov 16, 2024 |
#7 in #pest-grammar
34KB
386 lines
json_parser_with_pest
https://docs.rs/json_parser_with_pest/latest/json_parser_with_pest/
https://crates.io/crates/json_parser_with_pest
Overview
json_parser_with_pest is a JSON processing tool written in Rust, capable of parsing JSON files, validating schemas, and converting JSON to various formats like YAML and XML. Built with Pest for grammar parsing, this parser supports the manipulation of JSON files with a command-line interface (CLI).
Features
- Parse and validate JSON files against a provided schema.
- Extract specific JSON sections by key.
- Edit JSON data and save changes back to the file.
- Convert JSON to YAML or XML.
- Handle large JSON files in chunks.
- Search for values in JSON data.
- Access JSON elements by path.
- Minify JSON by removing whitespace.
- Display the structural hierarchy of JSON data.
Technical Description
The parser utilizes a custom-defined Pest grammar file (json.pest
) to interpret JSON structures. Key parsing rules are defined for JSON objects, arrays, strings, numbers, booleans, and null values, enabling support for typical JSON formats with whitespace tolerance and escape sequences in strings.
Grammar Rules
- WHITESPACE: Defines whitespace characters (space, tab, newline) and ignores them across the JSON structure.
- value: The primary rule that includes objects, arrays, strings, numbers, booleans, and null values.
- object: Defines a JSON object structure with key-value pairs, with optional commas between pairs.
- array: Defines a JSON array structure with optional commas between values.
JSON Structure Diagram
This diagram illustrates the general structure of JSON supported by this parser:
json
├── object
│ ├── pair
│ │ ├── string (key)
│ │ ├── value
│ │ │ ├── object
│ │ │ ├── array
│ │ │ ├── string
│ │ │ ├── number
│ │ │ ├── boolean
│ │ │ └── null
│ │ └── ...
├── array
│ ├── value
│ └── ...
└── other basic JSON values (string, number, boolean, null)
Parsing Logic
- Objects: Parsed as collections of key-value pairs enclosed in braces
{}
, where keys are strings and values can be any JSON type. - Arrays: Parsed as ordered collections of values enclosed in square brackets
[]
, allowing mixed types. - Strings: Parsed with support for escape sequences (e.g.,
\n
,\t
, Unicode). - Numbers: Parsed to support integer, float, and scientific notation formats.
- Booleans and Null: Parsed as literals
true
,false
, andnull
.
CLI Usage
Run the parser through CLI commands, such as:
$ zaporozhets-json-parser validate <input> <schema>
$ zaporozhets-json-parser parse-partial <input> <key>
$ zaporozhets-json-parser edit <input> <key> <value>
Use --help
for full command options.
Example Commands
validate
: Validates JSON against a schema.parse-partial
: Extracts a specified key's value.edit
: Updates a key in the JSON.convert
: Converts JSON to YAML or XML.large-file
: Parses large JSON files in chunks.
Setup
- Ensure Rust is installed: Rust Installation
- Clone the repository and run:
cargo build
- Use CLI commands as described.
Error Handling
Error handling is implemented with anyhow
for flexible context-based error reporting, and thiserror
for custom error types like JsonParseError
and SchemaValidationError
.
Testing and Quality Assurance
- Unit Tests: Located in the
tests
directory, covering each grammar rule. - Formatting and Linting: Run
cargo fmt
andcargo clippy
to maintain code quality.
Makefile
The Makefile
includes commands to simplify running, testing, formatting, and linting the project:
run:
cargo run
release:
cargo run --release
build:
cargo build
build-release:
cargo build --release
test:
cargo test
format:
cargo fmt
lint:
cargo clippy -- -D warnings
precommit: format lint test
clean:
cargo clean
update:
cargo update
Dependencies
~8–18MB
~258K SLoC