akheron / jansson

C library for encoding, decoding and manipulating JSON data
http://www.digip.org/jansson/
Other
3.05k stars 808 forks source link

json_object_foreach(obj, key, value) - mixed objects with sub-objects error #636

Closed DennisDNguy closed 1 year ago

DennisDNguy commented 1 year ago

I have noticed that the json_object_foreach(obj, key, value) function could not detect sub-objects if the sub-objects were at the middle of the objects. For example:


{
    "deviceIdentifier": "1B35D979",
    "payload": {
        "name": "startScan",
        "continuous": true,
        "reportNewTagsOnly": false,
        "powerLevel": 5,
        "interval": 6,
        "duration": 10
    },
    "timestamp": "2022-10-19T11:23:31Z"
}

The function failed to parse the "payload" sub-objects.

Any advice is appreciated
Thanks
ayin1551 commented 1 year ago

Hi I tried this by following code and get no error...

#include <jansson.h>

int main(){
    const char *key;
        json_t *inobj, *outobj, *value;

    // load json from file
    FILE *infile, *outfile;
    infile = fopen("/home/jansson-test/load.json", "r");
    json_error_t error;
    inobj = json_loadf(infile, JSON_INDENT(4), &error);

    // parse
    outobj = json_object();
    json_object_foreach(inobj, key, value) json_object_set(outobj, key, value);

    // dump result
    outfile = fopen("/home/jansson-test/jansson.json", "w+");
    json_dumpf(outobj, outfile, JSON_INDENT(4));

    fclose(infile);
    fclose(outfile);
    json_decref(inobj);
    json_decref(outobj);
    return 0;
}
DennisDNguy commented 1 year ago

Thanks @ayin1551 Probably the new v2.14 version fixed the issue.