DDC-3183: Add JsonSerializable to Collections #3944

Closed
opened 2026-01-22 14:32:07 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 22, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user gabrielbull:

Implement this:

http://www.php.net/manual/fr/class.jsonserializable.php

Can't really make this claim if Doctrine is not implementing basic interfaces for collections:

The missing (SPL) Collection/Array/OrderedMap interface.

Originally created by @doctrinebot on GitHub (Jun 22, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user gabrielbull: Implement this: http://www.php.net/manual/fr/class.jsonserializable.php Can't really make this claim if Doctrine is not implementing basic interfaces for collections: > The missing (SPL) Collection/Array/OrderedMap interface.
admin added the New FeatureWon't Fix labels 2026-01-22 14:32:07 +01:00
admin closed this issue 2026-01-22 14:32:07 +01:00
Author
Owner

@Richard87 commented on GitHub (Jan 6, 2016):

I also would like this, would make some use-cases slightly simpler :) Also, as far as I can imagine, all that is needed is this function in ArrayCollection:

function jsonSerialize()
{
    return $this->elements;
}

Obvously this would require all elements in the array to also be json serializable... Should I try to create a pull-request for this, and if so, add it sooner that v. 3?

@Richard87 commented on GitHub (Jan 6, 2016): I also would like this, would make some use-cases slightly simpler :) Also, as far as I can imagine, all that is needed is this function in `ArrayCollection`: ``` function jsonSerialize() { return $this->elements; } ``` Obvously this would require all elements in the array to also be json serializable... Should I try to create a pull-request for this, and if so, add it sooner that v. 3?
Author
Owner

@Ocramius commented on GitHub (Jan 6, 2016):

This is a basic SRP violation. Serialization (into JSON in this case) is out of the scope of the ORM, and also of the entire doctrine project at the moment.

@Ocramius commented on GitHub (Jan 6, 2016): This is a basic SRP violation. Serialization (into JSON in this case) is out of the scope of the ORM, and also of the entire doctrine project at the moment.
Author
Owner

@Richard87 commented on GitHub (Jan 6, 2016):

I see your point, but ArrayCollection is not a part of ORM(?)... And as GabrielBull commented, that it belongs in a object that seeks to replace PHPs array?

@Richard87 commented on GitHub (Jan 6, 2016): I see your point, but `ArrayCollection` is not a part of ORM(?)... And as GabrielBull commented, that it belongs in a object that seeks to replace PHPs array?
Author
Owner

@Ocramius commented on GitHub (Jan 6, 2016):

The doctrine collections library abstracts reading/writing of elements in a collection-alike structure: it doesn't abstract transforming the collection into something else (SRP still applies).

It is trivial to simply call toArray on a collection inside an entity's JsonSerializable implementation.

@Ocramius commented on GitHub (Jan 6, 2016): The doctrine collections library abstracts reading/writing of elements in a collection-alike structure: it doesn't abstract transforming the collection into something else (SRP still applies). It is trivial to simply call `toArray` on a collection inside an entity's `JsonSerializable` implementation.
Author
Owner

@Richard87 commented on GitHub (Jan 6, 2016):

Yeah, thats true... thanks for the explanation :)

@Richard87 commented on GitHub (Jan 6, 2016): Yeah, thats true... thanks for the explanation :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3944