Skip to content

Conversation

mtarld
Copy link
Contributor

@mtarld mtarld commented Aug 29, 2025

Q A
Branch? 7.3
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #61559
License MIT

Because a list<T> is a subtype of array<T>, an iterable<T> cannot be a list.

This means that, as said here:

There is no way to be sure that keys of an iterable are successive (even if they are integers).
And if keys are not successive, the resulting JSON structure is an object and not an array.
According, to the JSON specification, object' keys must be strings, and not integers.

This imply that it is right now impossible to "yield" items from [{"itemId": 1, "modificationDate": "2025-10-10"}] - it can only be yielded from {"0": {"itemId": 1, "modificationDate": "2025-10-10"}}.

I think that in the real world, a lot of people want to be able to yield items from [...], even though it does not stick with the JSON specification.

This PR is doing the trade-off of considering iterable<int, Anything> as an "iterable list" (like list<Anything> but not cast to array).

@mtarld mtarld force-pushed the fix/json-streamer-iterable-list branch from fd4737d to 4dc00f4 Compare September 1, 2025 07:14
@mtarld mtarld force-pushed the fix/json-streamer-iterable-list branch from 4dc00f4 to 2b9b801 Compare September 1, 2025 07:15
@nicolas-grekas
Copy link
Member

Thank you @mtarld.

@nicolas-grekas nicolas-grekas merged commit 6e42b36 into symfony:7.3 Sep 1, 2025
9 of 11 checks passed
@mtarld mtarld deleted the fix/json-streamer-iterable-list branch September 1, 2025 09:30
fabpot added a commit that referenced this pull request Sep 3, 2025
This PR was merged into the 7.3 branch.

Discussion
----------

[JsonStreamer] Fix encoding iterable lists

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

Sibling of #61561.

Handle any iterable with `int` as key as a list during encoding (which mean being encoded with brackets).

Commits
-------

2057bc8 [JsonStreamer] Fix encoding iterable lists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants