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_object_get_boolean() returns wrong result for objects and arrays #658

Closed
plan44 opened this issue Aug 16, 2020 · 2 comments
Closed

Comments

@plan44
Copy link

plan44 commented Aug 16, 2020

Describtion of the bug
The current implementation of json_object_get_boolean(jobj) returns 0 (false) when jobj is a json_type_object or json_type_array.
This seems to be in contrast to the expected behaviour as documented, which is:

The type is coerced to a json_bool if the passed object is not a json_bool.
integer and double objects will return 0 if there value is zero
or 1 otherwise. If the passed object is a string it will return
1 if it has a non zero length. If any other object type is passed
1 will be returned if the object is not NULL.

Steps To Reproduce

#include <json-c/json.h>
#include <stdio.h>

int main()
{
  struct json_object* jobj = json_object_new_object();
  int ob = json_object_get_boolean(jobj);
  printf("bool of a json_type_object = %d (should be 1)\n", ob);

  struct json_object* jarr = json_object_new_array();
  int ab = json_object_get_boolean(jarr);
  printf("bool of a json_type_array = %d (should be 1)\n", ab);
}

Output of this is:

bool of a json_type_object = 0 (should be 1)
bool of a json_type_array = 0 (should be 1)

Version and Platform

  • json-c version: json-c-0.15 (installed via brew)
  • OS: macOS 10.15.5
hawicz added a commit that referenced this issue Aug 17, 2020
…ays returns 0), and add those cases to the test_cast test.

See also issue #658.
@hawicz
Copy link
Member

hawicz commented Aug 17, 2020

This code has always returned 0 for objects and arrays, so I'd call this a doc bug, not a code bug. Thanks for the pulll request, but I won't be merging it currently. Instead, I added an item to the proposed 1.0 changes to consider it there.
I updated the doc and added some tests in commit 2b439ea.

@hawicz hawicz closed this as completed Aug 17, 2020
@plan44
Copy link
Author

plan44 commented Aug 17, 2020

Ok, I understand the decision, given json-c has always done it that way. Now that it is documented, I'm fine ;-)

But I still support a 1.0 change that would make this to work like the documentation said until now - because that's exactly what JS's Boolean() does and thus is widely expected behaviour.

I stumbled over this after hours of debugging a case where a message could be false or a json object, and the test was just json_object_get_boolean(), which would have worked according to the docs... (Once found, it was easy to add a wrapper which tests in the way JS would)

plan44 added a commit to plan44/p44utils that referenced this issue Aug 19, 2020
… false for arrays and objects

Bug was reported on github: json-c/json-c#658
PR for fixing the bug: json-c/json-c#659
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

Successfully merging a pull request may close this issue.

2 participants