-
Notifications
You must be signed in to change notification settings - Fork 1
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
Inconsistent result from single object to multiple objects of same element #2
Comments
Try fiddling with this line. If you flick the decode to treat convert objects into associative arrays you might get the desired result... I'm unsure if this is a deep conversion though, it might affect |
I can understand why I get this result. Internally the SimpleXMLElement object looks like this:
Compared to this when there is more than 1 "entry" element:
I've tried a few combinations with I'm not sure it's quite possible to achieve what I want by manipulating the json_encode/decode process. Might have to perform a transformation on the array before using encode. |
Possible solution https://gist.github.com/pointybeard/aad76a7cc71f9694b19141516fcb9563 |
Pushed a potential fix. Also adds new feature "Transformers" (ce15331) |
… Transformation classes. Updated CHANGELOG and README to reflect this change. Fixes #2
The resultant JSON differs depending if there is a single object or multiple of the same object.When there is only a single object (e.g. there is only 1 entry from a data-source), the result is a single object. However, when there is more than object of the same element (more than just 1 result from a data-source) the resultant JSON is an array.
Steps to reproduce
Actual output
From XML like this:
We get JSON like this:
However, remove 1 entry (so only 1 remains) and the output looks like this:
Notice that the second example is not a JSON array.
Expected output
In both examples, it should be an array.
Possible solutions
It is possible the first behaviour is desirable since there will only ever be a single entry, this an array is unnecessary. One solution might be to have attributes on the XML e.g.
forceArray="true"
or similar which tells the JSON handler to behave in a certain way.A stop-gap solution is to always include an empty element which tricks the parser into thinking it's an array. e.g.
However, this is not ideal.
The text was updated successfully, but these errors were encountered: