almhdy24 / jsonella
A lightweight, modern JSON library for PHP with clean API and excellent error handling.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/almhdy24/jsonella
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2026-01-04 18:05:48 UTC
README
A lightweight, modern JSON library for PHP with clean API and excellent error handling.
A lightweight, elegant JSON handling library for PHP with clean API, excellent error handling, and zero dependencies.
โจ Features
- ๐ Clean API - Simple static methods
- ๐ก๏ธ Exception-based error handling - No silent failures
- ๐ File operations - Read/write JSON files
- โ Validation - Check if strings are valid JSON
- ๐จ Formatting - Pretty printing & minification
- ๐ Merging - Deep merge capabilities
- ๐ชถ Zero dependencies - Pure PHP
- ๐ฏ 100% compatible with PHP 7.4+
๐ฆ Installation
Using Composer
composer require almhdy/jsonella
Manual Installation
Download and include the library:
require_once 'src/JSON.php';
๐ Quick Start
<?php require_once 'vendor/autoload.php'; use Almhdy24\jsonella\JSON; // Encode data to JSON $data = ['name' => 'John', 'age' => 30, 'skills' => ['PHP', 'JSON']]; $json = JSON::encode($data); // Decode JSON string $array = JSON::decode($json); // Pretty print for debugging echo JSON::pretty($data); // Read JSON from file $config = JSON::readFile('config.json'); // Write JSON to file JSON::writeFile('data.json', $data, true);
๐ Usage Examples
Basic Encoding/Decoding
// Encode with pretty print $prettyJson = JSON::encode($data, true); // Decode as object $object = JSON::decode($json, false); // Check if valid JSON if (JSON::isValid($jsonString)) { // Process JSON }
File Operations
// Read and write files $users = JSON::readFile('users.json'); $users[] = ['name' => 'New User']; JSON::writeFile('users.json', $users, true);
Advanced Features
// Merge multiple JSON arrays $json1 = '{"a": 1, "b": 2}'; $json2 = '{"b": 3, "c": 4}'; $merged = JSON::merge([$json1, $json2], true); // Minify JSON $minified = JSON::minify($prettyJson); // Flatten array $flat = JSON::flatten(['user' => ['name' => 'John', 'age' => 30]]); // Result: ['user.name' => 'John', 'user.age' => 30]
Error Handling
try { $data = JSON::decode('{invalid json}'); } catch (InvalidArgumentException $e) { echo 'Error: ' . $e->getMessage(); } try { JSON::writeFile('/invalid/path/data.json', $data); } catch (RuntimeException $e) { echo 'File error: ' . $e->getMessage(); }
๐ API Reference
Static Methods
| Method | Description |
|---|---|
JSON::encode($data, $pretty = false) |
Encode data to JSON |
JSON::decode($json, $assoc = true) |
Decode JSON string |
JSON::isValid($string) |
Validate JSON string |
JSON::readFile($filepath, $assoc = true) |
Read JSON from file |
JSON::writeFile($filepath, $data, $pretty = false) |
Write JSON to file |
JSON::pretty($data) |
Pretty print data |
JSON::minify($json) |
Minify JSON string |
JSON::merge($jsonItems, $mergeArrays = false) |
Merge JSON data |
JSON::lastError() |
Get last JSON error |
JSON::toArray($data) |
Convert object to array |
JSON::flatten($array, $prefix = '') |
Flatten array with dot notation |
๐งช Testing
Run the test suite:
composer test # or php tests/JSONTest.php
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Thanks to all contributors
- Inspired by the need for better JSON handling in PHP
- Built with โค๏ธ by almhdy24
Made with โค๏ธ by almhdy24