-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
os/json: Skip json null value when parsing json content #36332
os/json: Skip json null value when parsing json content #36332
Conversation
Signed-off-by: Eric Brinette <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON doesn't define a null. I'm not clear on what this is trying to do? What is the encoded syntax you're trying to produce from a NULL provided by the user? Or what encoded syntax do you expect to produce a NULL in the resulting parsed output?
Hello Andy, When I look at this it seems that JSON does define null. https://datatracker.ietf.org/doc/html/rfc8259 This commit simply skip the key/value parsed when I can shortly describe the case where I need it:
I noticed that the test are not passing which is expected. Now, before changing the tests, I would need to know if this modification can be accepted. Maybe, I could/should add a boolean option with default value on the json_obj_parse function in order to not break the current API/behavior? Thank you for your review =) |
Up please. I would love to get an answer when you can find time. Thank you. |
Given the fact that this is a recurring request (cf. #27600, #28905), I'd love to hear @lpereira's opinion on it. Although he left the Zephyr project, it seems like he's still working on his json library. |
The reason I never supported null in this parser is because I could never
figure out a way to communicate that a nested object was incomplete with
the return value being what it is (nth bit set if nth struct member -- in
descriptor array order -- was decoded). It's pretty tricky.
I suppose a good way to avoid problems with this would be 0-initializing
values when null is encountered, so that at least values are predictable
when nested objects are parsed (and won't have whatever garbage was in the
struct). This won't solve the nested object vs. int return issue, but seems
like a good compromise that won't break the API. Not setting the bit (like
the patch does) on null seems like a good approach.
Another thing that might be worth considering is to have a "nullable" flag
encoded in the descriptor somehow. This way you can say if a value is
absolutely necessary, and fail early if null is found where it shouldn't
occur. Since JSON is used for network stuff, it's a good idea to be strict
by default to not catch anyone by surprise. (The main reason the library is
anal about types is because of this; I originally wrote the parser for the
NATS protocol implementation.)
(Also, yes, there has been some improvements in the JSON library over the
years; some of which could be backported to Zephyr.)
…On Wed, Jul 7, 2021, 07:02 Markus Fuchs ***@***.***> wrote:
Given the fact that this is a recurring request (cf. #27600
<#27600>, #28905
<#28905>), I'd love to
hear @lpereira <https://github.com/lpereira>'s opinion on it. Although he
left the Zephyr project, it seems like he's still working on his json
library
<https://github.com/lpereira/lwan/commits/master/src/samples/techempower/json.c>
.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#36332 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAADVGNQ7EN7OOFW25GVXT3TWRM6BANCNFSM46ZVQGKQ>
.
|
I don't think however that this will help with the case where null is used
to signal that a key should be removed, like in the API you're using. You
need to know that the key is there in the JSON but has a null value; the
nth bit not being set communicates that the key wasn't there, which is
different.
…On Wed, Jul 7, 2021, 07:36 Leandro Pereira ***@***.***> wrote:
The reason I never supported null in this parser is because I could never
figure out a way to communicate that a nested object was incomplete with
the return value being what it is (nth bit set if nth struct member -- in
descriptor array order -- was decoded). It's pretty tricky.
I suppose a good way to avoid problems with this would be 0-initializing
values when null is encountered, so that at least values are predictable
when nested objects are parsed (and won't have whatever garbage was in the
struct). This won't solve the nested object vs. int return issue, but seems
like a good compromise that won't break the API. Not setting the bit (like
the patch does) on null seems like a good approach.
Another thing that might be worth considering is to have a "nullable" flag
encoded in the descriptor somehow. This way you can say if a value is
absolutely necessary, and fail early if null is found where it shouldn't
occur. Since JSON is used for network stuff, it's a good idea to be strict
by default to not catch anyone by surprise. (The main reason the library is
anal about types is because of this; I originally wrote the parser for the
NATS protocol implementation.)
(Also, yes, there has been some improvements in the JSON library over the
years; some of which could be backported to Zephyr.)
On Wed, Jul 7, 2021, 07:02 Markus Fuchs ***@***.***> wrote:
> Given the fact that this is a recurring request (cf. #27600
> <#27600>, #28905
> <#28905>), I'd love to
> hear @lpereira <https://github.com/lpereira>'s opinion on it. Although
> he left the Zephyr project, it seems like he's still working on his json
> library
> <https://github.com/lpereira/lwan/commits/master/src/samples/techempower/json.c>
> .
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#36332 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAADVGNQ7EN7OOFW25GVXT3TWRM6BANCNFSM46ZVQGKQ>
> .
>
|
Given the fact there is a struct object_t {
const char * string;
} object;
const struct json_obj_descr descriptor[] = {
JSON_OBJ_DESCR_PRIM(object_t, string, JSON_TOK_NULL),
};
const char in[] = "{\"string\":null}";
char out[strlen(in)+1];
const int ret1 = json_obj_parse((char *) in, strlen(in), descriptor, ARRAY_SIZE(descriptor), &object);
const int ret2 = json_obj_encode_buf(descriptor, ARRAY_SIZE(descriptor), &object, out, sizeof(out)); With the current version of the json library, this sample will fail with EINVAL (-22). Clearly, this doesn't really solve the original issue (where one wants to distinguish between field is missing, null, empty ("") or set ("...")), but this way you could make the parsing of simple objects double-stage by first parsing against a descriptor defining the field as
+1. The patch does not do that a the moment. With the sample code shown above,
That could be determined by the application via initializing the field prior parsing (to an invalid value) and checking if it has been set to null by the parser after the function returns. That wouldn't be the cleanest solution, but would at least give you a choice... |
According to what you suggested. |
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
Accept (by skipping) null values in JSON parser instead of stopping the parser and returning an error.
Signed-off-by: Eric Brinette [email protected]