Skip to content

(QUESTION) json_extract() behavior #17

@lcsmuller

Description

@lcsmuller
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions