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

dev-main 2025-12-04 17:23 UTC

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.

PHP Version License GitHub Release

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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