-
-
Notifications
You must be signed in to change notification settings - Fork 863
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
Keys difference on JSON vs. JSON-LD #2066
Comments
My advice is to always use JSON-LD. "raw" JSON has no benefits over JSON-LD, on the other hand, JSON-LD adds nice capabilities like pagination metadata, robust type system, filters documentation... (that your clients are free to not use, of course). Regarding the keys it's because your associative array probably look like |
I know but I just need the pure JSON, ok? (especially for other clients that are not able to parse JSON-LD; say „thanks” to API Platform Client Generator that completely forgot about Angular2+ exists) Back to the course, it's not an associative array but an object that implements How to use custom data provider without enforcing to be associative? Is it a bug that should be fixed? I don't know APIP well so far, so I'm asking; may I do something in the wrong way. |
All "pure" JSON parsers are able to parse JSON-LD. Including
API Platform is a free, open-source, community-driven project. If someone (including you) contribute Angular 2+ support, we'll be glad to merge it. Note that there is already a Typescript definition generator in this project. I'm not sure to understand your last paragraph, can you paste the code. I'm pretty sure your issue is related to this: https://3v4l.org/7NDh3 |
Okay, I misused the word: I meant „process”. All the old processors need is an array of objects, not object with array so I want to use pure JSON. I know it's OS, moreover even you have accepted my PR but right now I have to pay with my time to something else and dunno TS so well to create this clear. To clarify, I do not mean the pure array. I'm working on Iterator in order to implement paginator interfaces. Look:
Let's say it's a data collection provider. After calling
Which — obviously — produces the second array you mentioned in 3v4l. If I change the
Regardless how many records I return. My question: is it okay for adding to Core the PR with this behavior (pseudocode): IF is_null(iterator->key) THEN return iterator->current (without associating this with key)? Thus, the result could look like:
Produced array:
So that — pure JSON array will be properly serialized into array of objects. I'm asking because I want to know if should I implement this on my custom data provider level or prepare a PR because it could be more global? |
According to http://php.net/manual/en/iterator.key.php, your Should be:
|
Ok, so what do you recommend in order to get rid of useless keys from iterator? Another interface for data collection provider to implement, let's say, |
No, you just need to fix your |
Could you provide an example how to do that with custom data provider in order to return the data from iterator without indexing records using |
If you don't want to change the keys returned by your iterator_to_array($iterator, false); |
Your issue is not related to JSON-LD. 😄 |
I know is unrelated to JSON-LD, it's related to pure JSON implementation. I'm asking to dismiss keys from iterator just like within JSON-LD it occurs. I, of course, could wrap the iterator's logic but the only solution is to put it on every data provider? Within JSON-LD the |
So I'm asking how to write this iterator in order to make this working? I'm completely stuck because docs don't contain any example, just some description. I know how to make a workaround but what's the right way (with working example, not a single line)? |
I guess I must have misunderstood your question. You're returning an |
Yes, I'm returning an |
Is it not possible for you to return 0-indexed keys from your |
In order to fix this properly, we might need to add a |
@teohhanhui — it's possible but I need to dismiss them in resulting |
@ertz If you return 0-indexed keys and with no gaps, it should work as expected. Have you tried it? |
Yes, I've tried to return numbers from iterator's
instead of
where sth is a record. This behavior doesn't occur when I use JSON-LD. |
Thanks for confirming. I think we need to add a |
I'll try later to find fix for this but I don't know APIP so far so it may be weird. Thx for confirmation. |
#1534 worth mentioning? |
My apologies, when I start from Thanks for clarification. |
@ertz It's because in the Hydra core/src/Hydra/Serializer/CollectionNormalizer.php Lines 88 to 90 in e8e4ce2
But for the |
So why don't pass pure array using |
If we try to convert everything to array, we'll cause you to lose the performance advantage you're trying to gain by using an |
How about something like „iterator proxy” capturing the |
This also happens if you use criteria to change the order of a collection: public function getItems(): Collection
{
$criteria = Criteria::create()->orderBy(['rank' => Criteria::ASC, 'id' => Criteria::ASC]);
return $this->shots->matching($criteria);
} If the order changes, the result comes back as an array rather than an object. To get around this I had to create a serializer to do array_values() on the result. |
I have a data created by custom data provider. Using JSON-LD the data looks like this:
Whereas using pure JSON produces:
How keys should look like? Implementing partial paginator enforces using iterator and yielding keys. Two questions:
The text was updated successfully, but these errors were encountered: