GNOME Bugzilla – Bug 658632
unexpected behaviour when parsing invalid booleans with json_reader_get_boolean_value
Last modified: 2012-03-13 22:08:27 UTC
When compiling and executing the following snippet, the last assert breaks (g_value_get_boolean). I would have expected the JsonReader to report an error before that. GValue yyy = { 0 }; g_value_init (&yyy, G_TYPE_BOOLEAN); static const gchar *zzz = "{ \"value\": 3.1 }"; parser = json_parser_new (); json_parser_load_from_data (parser, zzz, -1, NULL); reader = json_reader_new (json_parser_get_root (parser)); json_reader_read_member (reader, "value"); g_value_set_boolean (&yyy, json_reader_get_boolean_value (reader)); if (json_reader_get_error (reader) != NULL) { g_error ("could not read boolean value"); } g_assert (G_IS_VALUE(&yyy)); g_assert (G_VALUE_HOLDS_BOOLEAN (&yyy)); g_assert (g_value_get_boolean (&yyy)); g_value_unset (&yyy);
strictly speaking, the assert is triggered because "3.1" is interpreted to a non-boolean value, and that means json_reader_get_boolean_value() will return FALSE. JsonReader typed accessors should do more validation, though, so that json_reader_get_error() would warn.
Created attachment 196106 [details] [review] reader: Do more strict validation JsonReader should perform more strict validation, and put itself in an error state whenever the user asks for values or information on nodes that are not there, or not of the right type.
Attachment 196106 [details] pushed as 9f75475 - reader: Do more strict validation this should cause your test to exit at g_error() before reaching the g_assert().
(In reply to comment #3) > Attachment 196106 [details] pushed as 9f75475 - reader: Do more strict validation > > this should cause your test to exit at g_error() before reaching the > g_assert(). I used the patch in my code and it worked flawlessly. Wow! feels like my birthday in advance. Merci
[Fixing Default QA assignee for json-glib - see 613232#c1. Sorry for bugmail noise.]