Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON encoding / decoding customization for JSON based cass #46552

Merged
merged 3 commits into from
Mar 24, 2023

Conversation

taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Mar 22, 2023

This PR allows the customization of json_encode and json_decode for the array and collection casts, as well as AsArrayObject, AsCollection, AsEnumArrayObject, etc.

use Illuminate\Database\Eloquent\Casts\Json;

Json::encodeUsing(fn ($value) => json_encode($value));

Json::decodeUsing(fn ($value, $associative) => json_decode($value, $associative));

This customization would typically be done in a service provider.

*
* @var callable|null
*/
protected $decoder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taylorotwell I guess these need to become static?

@morloderex
Copy link
Contributor

What if 2 columns within the same project needs different json encoding / decoding? This customization object seems to be global? Would that require a custom cast?

@taylorotwell
Copy link
Member Author

@morloderex I guess that require a custom cast. My thinking was that if you're using something like JSON_UNESCAPED_UNICODE you probably want that on all your JSON casts throughout your application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants