You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, as far as I can see, there are only a couple of ways to change encoding options to a model that is JSON serialized and returned as a response:
Either pass options into the constructor of \Illuminate\Http\JsonResponse every time you need it;
extend \Illuminate\Http\JsonResponse to include a standard set of options to use.
(bonus: middleware to reserialize with the desired options)
All of them are quite inconvenient if a project requires specific serializer options (or even a custom one).
Please tell me if I missed something because I am currently opting for the horrible namespace override described above 😉.
In case I did not miss a more convenient option here is my proposal:
Similar to Illuminate\Database\Eloquent\Casts\Json introduce a cast class that can be overwritten statically if needed.
Global serializer requirements could then be set in a service provider like so:
ResponseSerializer::encodeUsing(fn ($value) => json_encode($value, JSON_UNESCAPED_UNICODE));
// and depending if response and model serialization should be different ...
ModelSerializer::encodeUsing(fn ($value) => json_encode($value, JSON_UNESCAPED_UNICODE));
ResponseSerializer etc. would be a copy of the aforementioned Illuminate\Database\Eloquent\Casts\Json.
Those two should be separate as one handles casts for the database and the other casts for the response.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, as far as I can see, there are only a couple of ways to change encoding options to a model that is JSON serialized and returned as a response:
\Illuminate\Http\JsonResponse
every time you need it;\Illuminate\Http\JsonResponse
to include a standard set of options to use.json_encode
/json_decode
functions)All of them are quite inconvenient if a project requires specific serializer options (or even a custom one).
Please tell me if I missed something because I am currently opting for the horrible namespace override described above 😉.
In case I did not miss a more convenient option here is my proposal:
Similar to
Illuminate\Database\Eloquent\Casts\Json
introduce a cast class that can be overwritten statically if needed.Global serializer requirements could then be set in a service provider like so:
ResponseSerializer
etc. would be a copy of the aforementionedIlluminate\Database\Eloquent\Casts\Json
.Those two should be separate as one handles casts for the database and the other casts for the response.
Beta Was this translation helpful? Give feedback.
All reactions