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

[9.x] Allow jsonSerialize() to return other types #42133

Merged
merged 9 commits into from
Apr 27, 2022
Merged

Conversation

mikewuu
Copy link
Contributor

@mikewuu mikewuu commented Apr 26, 2022

This PR updates jsonSerialize()'s return type to mixed, matching PHP's definition.

Use case

Allows models to override jsonSerialize, and serialize into strings rather than always having to be an JSON object.

Example

We have a simple model, Permission, who's only attribute is name:

class Permission extends Model
{
    protected $fillable = [
        'name',
    ];
}

When we fetch all permissions, the collection is automatically serialized into JSON objects.

Controller

class PermissionsController
{
  public function list()
  {
     return Permission::all();
  }
}

Would currently return

[
  {name: "foo"},
  {name: "bar"},
  {name: "baz"},
]

With this change, Permission, can override jsonSerialize, and return a string.

Updated response after this PR:

["foo", "bar", "baz"]

@driesvints
Copy link
Member

Ping @X-Coder264 this is what I meant on that pr your sent in ^

Copy link
Member

@GrahamCampbell GrahamCampbell left a comment

Choose a reason for hiding this comment

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

This change looks wrong to me. Having an implementation return a more specific type than that required by the interface is totally OK, and actually desirable in this case. https://3v4l.org/CYkJl

@mikewuu
Copy link
Contributor Author

mikewuu commented Apr 26, 2022

Updated it to be a union type with string instead. JsonSerializable only specifies that the return type has to be compatible with json_encode, which a string is.

@dennisprudlo
Copy link
Contributor

With array|string it won't be possible to override jsonSerialize and return an integer, a float or a boolean, right? json_encode's $value can have any type but a resource.

@driesvints
Copy link
Member

@X-Coder264 my ping was more about you being okay with this change?

@X-Coder264
Copy link
Contributor

X-Coder264 commented Apr 26, 2022

@driesvints There's nothing intrinsically wrong with this change. From my perspective though it's a bit weird for a collection (which is just arrays on steroids) to be JSON serialized as anything else but an array. But if people want to do that I guess there's no reason from a framework perspective to block that so on that level I'm fine with the proposed change.

@mikewuu
Copy link
Contributor Author

mikewuu commented Apr 27, 2022

@X-Coder264

it's a bit weird for a collection (which is just arrays on steroids) to be JSON serialized as anything else but an array.

It's actually the Collection item's jsonSerialize that I'm hoping to override. The collection can (and should?) still expect an array to be returned.

I've updated it to still return an array for collection in: 464837d

@driesvints
Copy link
Member

If we do this then I suggest we move to the mixed return type like the PHP interface and not bother with union types.

@taylorotwell taylorotwell merged commit 12a9134 into laravel:9.x Apr 27, 2022
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.

6 participants