Download the PHP package kestrelwp/php-jsonc-parser without Composer
On this page you can find all versions of the php package kestrelwp/php-jsonc-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kestrelwp/php-jsonc-parser
More information about kestrelwp/php-jsonc-parser
Files in kestrelwp/php-jsonc-parser
Package php-jsonc-parser
Short Description Scanner and parser for JSON with comments (JSONC) - PHP port of Microsoft's node-jsonc-parser
License MIT
Homepage https://github.com/kestrelwp/php-jsonc-parser
Informations about the package php-jsonc-parser
PHP JSONC Parser
A PHP 8.4+ port of Microsoft's node-jsonc-parser - a scanner and fault-tolerant parser for JSON with Comments (JSONC).
Features
- 🔍 Scanner/Tokenizer - Character-by-character lexical analysis
- 🌳 Parser - Three parsing modes:
- SAX-style visitor pattern with event callbacks
- DOM-style tree builder with AST nodes
- Direct evaluation to PHP arrays/objects
- 💬 Comment Support - Handles
//line comments and/* */block comments - ⚡ Fault Tolerant - Continues parsing on errors and collects error information
- 🎨 Formatter - Configurable indentation, line breaks, and comment handling
- ✏️ Editor - Modify JSON with insert, update, and delete operations
- 🧭 Navigation - Find nodes by path, offset, or location matching
- 🚀 Performance - String interning for optimized formatting
Installation
Quick Start
Usage
Parsing
Direct Evaluation
Parse JSON/JSONC and convert to PHP arrays and objects:
DOM Tree
Build an Abstract Syntax Tree (AST):
Visitor Pattern
Process JSON with event callbacks (SAX-style):
Scanning/Tokenization
Low-level token scanning:
Navigation
Find nodes in the AST:
Formatting
Format JSON with configurable options:
Modification
Modify JSON documents:
Comment Handling
API Reference
JsoncParser (Facade)
Main entry point for all operations.
Scanner
createScanner(string $text, bool $ignoreTrivia = false): JsonScanner
Parser
parse(string $text, array &$errors = [], ?ParseOptions $options = null): mixedparseTree(string $text, array &$errors = [], ?ParseOptions $options = null): ?Nodevisit(string $text, JsonVisitor $visitor, ?ParseOptions $options = null): mixedstripComments(string $text, ?string $replaceCh = null): string
Navigation
getLocation(string $text, int $position): LocationfindNodeAtLocation(?Node $root, array $path): ?NodefindNodeAtOffset(Node $node, int $offset, bool $includeRightBound = false): ?NodegetNodePath(Node $node): arraygetNodeValue(Node $node): mixed
Formatting
format(string $documentText, ?Range $range, FormattingOptions $options): array
Editing
modify(string $text, array $path, mixed $value, ModificationOptions $options): arrayapplyEdits(string $text, array $edits): string
Options Classes
ParseOptions
FormattingOptions
ModificationOptions
Error Handling
The parser is fault-tolerant and continues on errors:
Examples
See the examples directory for complete working examples:
- basic_parsing.php - Parsing, scanning, and basic operations
- visitor_pattern.php - Using the visitor pattern
- modification.php - Modifying and formatting JSON
Differences from JavaScript Version
- Deletion marker: PHP doesn't distinguish
nullfromundefined, so we useRemoveMarker::instance()to indicate deletion inmodify()operations - Return type: Visitor methods return
bool|nullinstead ofvoidfor consistency with PHP's type system - Naming: Uses PSR-12 style (e.g.,
getNodeValueinstead ofgetNodeValue)
Performance
The library includes performance optimizations:
- String interning: Caches commonly used strings (spaces, line breaks)
- Lazy evaluation: Only parses what's needed
- Efficient scanning: Character-by-character with minimal allocations
Requirements
- PHP 8.4 or higher
- ext-mbstring (for UTF-8 support)
Testing
Credits
This is a PHP port of Microsoft's node-jsonc-parser, licensed under the MIT License.
License
MIT License - see LICENSE.md for details.
Contributing
Contributions are welcome! Please ensure:
- All tests pass (
composer test) - Code follows PSR-12 (
composer format) - PHPStan passes (
composer stan)
All versions of php-jsonc-parser with dependencies
ext-mbstring Version *