egeloen/json-builder
PHP 5.6+ library to build JSON using Symfony PropertyAccess paths. Set nested values, arrays, and raw/unescaped values while retaining control over escaping. Produces JSON strings from a fluent builder API with strong test coverage.
json_encode behavior). It is particularly valuable for:
PropertyAccess component ensures type-safe path manipulation (e.g., nested arrays/objects), reducing boilerplate for complex JSON structures.json_encode() but adds escaping granularity (e.g., embedding JSON strings without double-escaping).App\Services\JsonBuilder or API response modifiers).setValues()/setValue() chaining) aligns with Laravel’s method chaining patterns (e.g., Eloquent queries).Str::of($value)->escape()) for user-provided data.json_encode() for critical paths (e.g., bulk API responses).PropertyAccess (~1MB) to the vendor tree.response()->json(), JsonResponse) uses json_encode() with default options. Identify pain points (e.g., embedding JSON strings, custom serialization).setValue('[path]', $userInput, false) → Only if $userInput is pre-sanitized.Illuminate\Http\JsonResponse or Symfony\Component\HttpFoundation\JsonResponse?return new JsonResponse($builder->build(), 200, [], JSON_FORCE_OBJECT);
"\", \n, Unicode).Http::fake()).JsonSerializable) reduce reliance on this package?['errors' => ['field' => 'message']]).json_encode() in services (e.g., App\Services\NotificationService).json_encode(): Lacks escaping control.JsonSerializable: Requires implementing interfaces; less flexible for dynamic structures.spatie/array-to-xml: Overkill for JSON.nategood/prettify-json: Focuses on formatting, not escaping.return response()->json($data) with:
$builder = app(JsonBuilder::class);
return response()->json($builder->setValues($data)->build());
setValues() for structured data (e.g., Eloquent collections).setValue() with false for unescaped values (e.g., embedding JSON strings).json_encode() logic in services.Json::builder()) to abstract the package.
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('json.builder', function () {
return new JsonBuilder();
});
}
json_encode options in config/json.php:
'default_options' => JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT,
v5.4+ (compatible with Laravel’s Symfony components).DateTime, Carbon, or custom objects via __toString().app/Services/JsonBuilderService) to wrap the package."\", \u0000).json_encode().README.^1.2.0) in composer.json.composer why-not and composer why.[user][0][address]).if (!$escape) {
Log::debug("Unescaped value at {$path}:", [$value]);
}
How can I help you explore Laravel packages today?