Skip to content
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

Closed
tommorris opened this issue Sep 15, 2013 · 3 comments
Closed

No rels leads to empty array rather than empty object in output #29

tommorris opened this issue Sep 15, 2013 · 3 comments

Comments

@tommorris
Copy link
Contributor

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_encoded 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.

@barnabywalters
Copy link
Collaborator

Thanks for filing, might be fixable by using new stdclass (empty generic object) instead of an array if there are no rel values.

barnabywalters added a commit that referenced this issue Apr 7, 2014
* 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
@barnabywalters
Copy link
Collaborator

Full timeline of this issue (for reference):

2013-09-15: Tom Morris reports the issue. Parser incorrectly serialises empty rels.
2013-09-15: Barnaby Walters closes the issue with 91c171b, a hard-coded change of an empty rels array to a new stdClass. Parser correctly serialises empty rels.
Version 0.2.5: previous hard-coded change removed to prevent errors when using empty rel stdClasses in code. Parser incorrectly serialises empty rels.
2014-04-07 (version 0.2.6): Barnaby Walters adds JSON-mode with 28bd4d5, allowing people who want to parse microformats specifically for JSON serialisation to have the switch performed. Parser can correctly serialise empty rels if JSON-mode enabled.

@kylewm
Copy link
Contributor

kylewm commented Apr 9, 2016

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants