- How does
json_extract() deal with dynamically typed JSON? For example:
var foo=0;
json_extract(payload, sizeof(payload), "(var):d", &foo);
Here foo expects that the type of var is of a integer. What is going to happen if, for example, var ends up being a string? Will the program crash, or will the assignment be ignored and move on? If the program crashes, I think a more desirable behavior would be to compare the jsmn type with the specifier provided. In this example we have a d specifier which corresponds to a int type, so the comparison would end up being:
// don't crash, but ignore
if (jsmn_tok->type == JSMN_PRIMITIVE && a->_.builtin == B_INT) {
...
}
Crashing is not ideal when dealing with APIs that don't "respect" their docs specifications. Any non-conforming JSON is enough to crash the program.
json_extract()deal with dynamically typed JSON? For example:Here
fooexpects that the type ofvaris of a integer. What is going to happen if, for example,varends up being a string? Will the program crash, or will the assignment be ignored and move on? If the program crashes, I think a more desirable behavior would be to compare the jsmn type with the specifier provided. In this example we have adspecifier which corresponds to ainttype, so the comparison would end up being:Crashing is not ideal when dealing with APIs that don't "respect" their docs specifications. Any non-conforming JSON is enough to crash the program.