Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Json Laravel Package

beste/json

JSON helpers for PHP and Laravel: decode/encode, safe access, casting, and convenient manipulation of JSON strings and arrays. A lightweight utility package focused on cleaner, less error-prone JSON handling in everyday applications.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package: Use Composer: composer require beste/json.
  2. Understand the core classes: The package offers JsonEncoder and JsonDecoder classes—lightweight, static-friendly wrappers around PHP’s json_encode and json_decode, with sensible defaults (e.g., JSON_THROW_ON_ERROR, JSON_INVALID_UTF8_SUBSTITUTE).
  3. First use case: Replace ad-hoc json_encode($data, JSON_THROW_ON_ERROR) calls with JsonEncoder::encode($data) for cleaner, safer, and more readable code.

Implementation Patterns

  • Use encoder/decoder for explicit intent: In services or domain logic, prefer JsonEncoder::encode($payload) over json_encode($payload) to signal structured JSON handling.
  • Decode with type safety: Use JsonDecoder::decode(string $json, bool $associative = true, int $depth = 512, int $flags = 0) and leverage typed properties or DTOs:
    $user = JsonDecoder::decode($json, true, 512, JSON_THROW_ON_ERROR);
    $dto = UserDto::fromArray($user);
    
  • Error handling integration: Wrap with try/catch (\JsonException) where needed, or rely on JSON_THROW_ON_ERROR (enabled by default in decode methods).
  • Framework-agnostic usage: Works seamlessly in Laravel (e.g., inside job classes, service providers), Lumen, or any PSR-4 PHP project—no framework-specific bindings required.

Gotchas and Tips

  • Default flags matter: The decoder enables JSON_THROW_ON_ERROR and JSON_INVALID_UTF8_SUBSTITUTE by default—don’t override JSON_PARTIAL_OUTPUT_ON_ERROR unless you really need fallback output (strongly discouraged in production).
  • No automatic caching: Unlike some JSON helpers, this package does not cache encoding/decoding; it focuses on correctness and simplicity. For high-throughput cases, benchmark before adding layers.
  • No recursive validation: It does not validate schemas or nested structure beyond PHP’s native JSON compliance—use separate validators (e.g., webmozart/json) if strict schema enforcement is needed.
  • Extensibility via flags: While the API is minimal, you can pass custom flags to encode()/decode() for edge cases (e.g., JSON_PRETTY_PRINT for debugging logs).
  • No namespacing issues: Classes live in Beste\Json namespace—avoid use clashes with custom aliases.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport