A simple JSON parser implementation in PHP that parses JSON strings into PHP data structures. This project demonstrates how to build a JSON parser from scratch without relying on PHP's built-in json_decode()
function.
If you like to build your own JSON parser and you need to understand how it works, this project is for you.
- β Complete JSON compliance - Passes the official JSON.org test suite
- β
Full data type support:
- Strings with escape sequences (
\"
,\\
,\/
,\b
,\f
,\n
,\r
,\t
) - Numbers (integers, floats, scientific notation:
1e3
,5.5e-1
) - Booleans (
true
/false
) null
values- Arrays (nested and empty)
- Objects (nested and empty)
- Strings with escape sequences (
- β
Advanced string parsing:
- Unicode escape sequences (
\uXXXX
) - Control character validation (prevents unescaped control chars)
- Unicode escape sequences (
- β
Robust number validation:
- Prevents invalid leading zeros (e.g.,
013
) - Supports scientific notation (
1e3
,2e+00
,5.5e-1
) - Handles negative numbers and decimals
- Prevents invalid leading zeros (e.g.,
- PHP 8.4+
- Composer
- Clone the repository:
git clone https://github.com/DanielHemmati/json-parser-in-php
cd json-parser-in-php
- Install dependencies:
composer install
- Run the tests:
./vendor/bin/pest
Contributions are welcome! Please feel free to submit a pull request.
- Add benchmark to the project. (thanks to @mamazu)
- Add suppport for not loading all of the json into memory at once (see read json-machine for study, also mlebkowski showed his parser Good place to get deeper into parser)
- Add pint & phpstan
This project is licensed under the MIT License - see the LICENSE file for details.