-
Notifications
You must be signed in to change notification settings - Fork 38
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
No rels leads to empty array rather than empty object in output #29
Comments
Thanks for filing, might be fixable by using |
* The previous fix (substituting a stdClass for an empty rels array) was causing looping problems, so there is now a “JSON mode” into which the parser can be put either by passing true as the third constructor parameter or by setting jsonMode = true. By default an empty rels key will be an array, but if JSON mode is set it will be an empty stdClass, ensuringcorrect serialisation. * The tests have been updated accordingly * A missing value property from one test has been added
Full timeline of this issue (for reference): 2013-09-15: Tom Morris reports the issue. Parser incorrectly serialises empty rels. |
@aaronpk suggested possibly using ArrayObject instead of stdClass (alleviating the need for a jsonMode switch). Initial testing seems like this might work as expected for both encoding and iterating. php > echo json_encode(["rels" => new ArrayObject()]);
{"rels":{}} php > foreach (new ArrayObject() as $k => $v) { echo $k; }
php > down-side is actual array_* methods don't work like php > $arr = new ArrayObject();
php > var_dump( array_keys($arr) );
PHP Warning: array_keys() expects parameter 1 to be array, object given in php shell code on line 1 |
This is probably unfixable because of PHP, but I wanted to file it so it is on record.
If you parse a document that has no rels, you get back an empty JSON array in the
json_encode
d output. If you parse a document that has rels, you get back a JSON object. The specification says you should get back an empty object.Obviously, in PHP lists and dictionaries (in Python terminology) are unified into array which can represent both list-style arrays (numerically-keyed arrays in PHP), dictionary-style arrays (associative arrays) and mixed list and dictionary style.
json_encode
lets you specify a flag for how you encode empty arrays.One possible solution to this is to
json_encode
each piece separately and then stitch them together. Or just ignore the problem and operate on the basis that PHP will never quite be spec-compliant.The text was updated successfully, but these errors were encountered: