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

json_encode on empty Map returns array #157

Closed
petr-buchin opened this issue Sep 9, 2020 · 4 comments
Closed

json_encode on empty Map returns array #157

petr-buchin opened this issue Sep 9, 2020 · 4 comments

Comments

@petr-buchin
Copy link

I was glad to see, that Map data structure can be json-encoded.
But if you run json_encode() on empty map, it gets gets encoded as an array - [].
I think this is a bug, since Map should always be encoded as an object - {}.

Thank you very much in advance!

@rtheunissen
Copy link
Member

This is unfortunately how JsonSerializable works, where jsonSerialize returns the native value to be encoded to JSON.

The method in the extension is implemented to delegate it to an array:

METHOD(jsonSerialize)
{
    PARSE_NONE;
    ds_map_to_array(THIS_DS_MAP(), return_value);
}

Actually, we can cast that to an (object) which would encode as {}. It's unfortunate that this is O(n) to convert the map to an array first, then O(n) again to encode to JSON. I'd like to look into the possibility of a toJson method that interacts directly with a JSON api of some kind.

I'll try the object cast in the meantime - thanks for brining this up.

@rtheunissen
Copy link
Member

I think this should do it. 078243f

@petr-buchin
Copy link
Author

Thank you very much!

@rtheunissen
Copy link
Member

This has now been fixed and released as part of 1.3.0

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

2 participants