balping/json-raw-encoder
Laravel helper for encoding JSON while preserving “raw” fragments (like JS functions or pre-encoded JSON) without extra quoting/escaping. Handy for configs and API payloads when parts must stay untouched, with a simple API and reliable output.
json_encode() does not support raw JS objects (throws errors). This package bridges that gap but introduces non-standard JSON, which may conflict with:
Response handling (e.g., response()->json()).json_encode() with JSON_PRESERVE_ZERO_FRACTION or custom serialization for simpler cases. For full JS object support, evaluate WebSocket protocols (e.g., msgpack or custom binary formats).json_encode() in controllers/views where raw JS objects are serialized.App\Exceptions\Handler to customize error responses (e.g., fallback to standard JSON if the package fails).JsonSerializable interface to support mixed PHP/JS objects.JsonRawEncoderServiceProvider) and avoid scattered use statements.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Non-Standard JSON | High | Document explicitly where this JSON is used (e.g., internal tools only). Validate consumers. |
| Breaking Changes | Medium | Archive status suggests low maintenance; fork if critical. |
| Performance | Low | Benchmark against json_encode(); likely minimal overhead for small payloads. |
| Security | Medium | Raw JS objects may introduce XSS risks if injected into HTML. Sanitize outputs. |
| Dependency Bloat | Low | Lightweight package (~100 LOC), but adds complexity. |
spatie/array-to-object, nesbot/carbon, or Laravel’s collect()?JSON.stringify() or libraries like flatted for similar functionality.json.dumps() with custom encoders or orjson for extensions.json_encode() in a single controller/action handling JS objects.app/Services/JsonRawEncoder.php) that wraps the package.use Balping\JsonRawEncoder\JsonRawEncoder as RawEncoder;
class JsonRawEncoderService {
public function encode($data): string {
try {
return RawEncoder::encode($data);
} catch (\Exception $e) {
// Fallback to standard JSON
return json_encode($data);
}
}
}
json_encode globally (caution: may affect third-party libraries).
json_encode::macro(function ($data) {
return app(JsonRawEncoderService::class)->encode($data);
});
<script> tags. Use JSON.parse() carefully.JSON.parse() with a reviver function to reconstruct JS objects.composer.json to avoid unexpected updates.JSON.parse() with reviver).## Raw JS JSON Usage
- **Output**: Only use in internal tools or APIs explicitly documented to return raw JS.
- **Client-Side**: Parse with `JSON.parse(rawJson, (key, value) => { /* handle JS objects */ })`.
undefined, null, circular references).var_export() or print_r() for debugging complex objects.jsonb).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Encoder Fails | Broken responses for JS objects | Fallback to json_encode() |
| Client-Side Parse Errors | Frontend crashes | Provide parsing utilities |
| XSS via Raw JS Injection |
How can I help you explore Laravel packages today?