php-standard-library/json
A small PHP Standard Library package for JSON encoding and decoding with typed exceptions and sensible defaults, providing predictable error handling for safer serialization in Laravel and other PHP projects.
Pros:
JsonException with contextual details (e.g., file/line numbers) improves debugging in Laravel’s exception handler, addressing a common pain point in APIs and CLI tools where JSON parsing failures are costly.config/json.php (e.g., JSON_THROW_ON_ERROR, JSON_PRETTY_PRINT) ensures uniformity across services, which is highly valuable for Laravel’s multi-service environments or monorepos.Json::validate() method fills a gap in Laravel’s built-in validation for raw JSON payloads, enabling schema enforcement (e.g., JSON Schema) in API requests or internal tools. This is ideal for compliance-heavy industries (e.g., finance, healthcare).symfony/validator for schema validation) avoid bloat, making it a low-risk addition compared to heavier libraries like Symfony’s Serializer.Cons:
Json::encode() to Json::laravel()->encode() requires codebase-wide updates, which may disrupt CI/CD pipelines or legacy services. Mitigation: Use aliases in config/app.php or phase the migration.Json::pretty() are deprecated, forcing refactoring efforts in existing codebases. Assess the scope of usage before adoption.Json::validate()) introduce optional dependencies (symfony/validator), which may complicate dependency management or conflict with existing packages (e.g., laravel/framework).config/json.php for non-critical paths.symfony/validator) may conflict with existing packages. Test thoroughly in a staging environment to identify compatibility issues.JsonException with structured data. Failure to do so may lose debugging context in production.400 Bad Request with JSON details).config/json.php.JsonException be integrated with Laravel’s exception handler (e.g., logging, user-facing errors)?400 Bad Request with JSON details)?symfony/validator, laravel/framework)? How will we resolve them?FormRequest, Validator)?response()->json() with Json::laravel()->encode() in controllers to ensure consistent JSON formatting (e.g., flags like JSON_PRETTY_PRINT).Json::laravel()->decode() in middleware or DTOs to enforce strict validation and typed exceptions.Json::validate() with Laravel’s FormRequest or Validator for schema enforcement in API payloads.Illuminate\Cache or Illuminate\Broadcasting using Json::laravel().Json::laravel() for uniform serialization (e.g., PostgreSQL jsonb).json_encode()/json_decode() calls with Json::laravel() for consistent JSON handling in Artisan commands or scripts.json_encode with hardcoded flags, manual validation).composer require php-standard-library/json
php artisan vendor:publish --provider="PhpStandardLibrary\Json\JsonServiceProvider"
config/json.php with environment-specific defaults (e.g., JSON_THROW_ON_ERROR in production).Json::* calls to Json::laravel()->* or add aliases in AppServiceProvider:
// config/app.php
'aliases' => [
'Json' => PhpStandardLibrary\Json\Facades\Json::class,
],
Json::validate() in FormRequest or middleware for schema enforcement:
public function withValidator($validator) {
$validator->after(function ($validator) {
$schema = file_get_contents('schemas/api.json');
Json::validate($this->json()->all(), $schema);
});
}
config/json.php (e.g., enable/disable per environment).How can I help you explore Laravel packages today?