#json-parser #convert-json #json-schema #validate-json #json-file #pest-grammar #parse-json

bin+lib my_Main_Parser_kma_Zaporozhets

A JSON parser created in Rust using Pest grammar

3 releases (breaking)

new 0.6.0 Nov 14, 2024
0.4.0 Nov 14, 2024
0.3.0 Nov 14, 2024

#2014 in Parser implementations

MIT license

30KB
364 lines

Zaporozhets JSON Parser

https://docs.rs/my_Main_Parser_kma_Zaporozhets/latest/my_Main_Parser_kma_Zaporozhets/

https://crates.io/crates/my_Main_Parser_kma_Zaporozhets

Overview

Zaporozhets JSON Parser 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

  1. WHITESPACE: Defines whitespace characters (space, tab, newline) and ignores them across the JSON structure.
  2. value: The primary rule that includes objects, arrays, strings, numbers, booleans, and null values.
  3. object: Defines a JSON object structure with key-value pairs, with optional commas between pairs.
  4. 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

  1. Objects: Parsed as collections of key-value pairs enclosed in braces {}, where keys are strings and values can be any JSON type.
  2. Arrays: Parsed as ordered collections of values enclosed in square brackets [], allowing mixed types.
  3. Strings: Parsed with support for escape sequences (e.g., \n, \t, Unicode).
  4. Numbers: Parsed to support integer, float, and scientific notation formats.
  5. Booleans and Null: Parsed as literals true, false, and null.

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

  1. Ensure Rust is installed: Rust Installation
  2. Clone the repository and run:
    cargo build
    
  3. 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 and cargo clippy to maintain code quality.

Makefile

The Makefile includes commands to simplify running, testing, formatting, and linting the project:

make run         # Run the project
make test        # Run all tests
make format      # Format code with cargo fmt
make lint        # Lint code with cargo clippy

Dependencies

~8–17MB
~255K SLoC