Skip to content
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

json_object_object_get_ex returning the original object #532

Closed
RudiSchloesser opened this issue Jan 10, 2020 · 5 comments
Closed

json_object_object_get_ex returning the original object #532

RudiSchloesser opened this issue Jan 10, 2020 · 5 comments

Comments

@RudiSchloesser
Copy link

I'm using json-c to parse messages provided by a device driver.
It works perfect within most of all cases (thanks, great job), but I'm facing an issue, in case larger JSON messages are parsed:
The parser correctly parses the message (see Message).
Afterwards I'm extracting the "data" object and passing it to a
handler. In case of short messages, everything works as expected.
For the larger message, the handler gets the original message object instead of "data". Extracting "data" a 2nd time solves the problem, but that's, I think, not the proper way.

static int
handle_config(struct json_object *msg)
{
    struct json_object *data, *obj;

    printf("CONFIG:\n  %s\n\n\n", json_object_to_json_string_ext(msg, JSON_C_TO_STRING_SPACED));
    json_object_object_get_ex(msg, "data", &data);
    printf("CONFIG (Date):\n  %s\n\n\n", json_object_to_json_string_ext(data, JSON_C_TO_STRING_SPACED));
    return 0;
}

static int
handle_config(struct json_object *data)
{
    printf("Event:\n  %s\n\n\n", json_object_to_json_string_ext(data, JSON_C_TO_STRING_SPACED));
    return 0;
}

void 
handle_msg(const char *json)
{
    struct json_object *msg, *data;

    msg = json_tokener_parse(json);
    printf("  %s\n", json_object_to_json_string_ext(msg, JSON_C_TO_STRING_SPACED));    
    if (msg == NULL) {
        return;
    }
    json_object_object_get_ex(msg, "type", &obj);
    if (obj == NULL) {
        goto exit;
    }
    type = json_object_get_string(obj);
    json_object_object_get_ex(msg, "data", &data);
    if (strcmp(type, "CONFIG") == 0) {
        if (data) {
            handle_config(data);
        }
    } else if (strcmp(type, "EVENT") == 0) {
        if (data) {
            handle_event(data);
        }
    }
exit:
    json_object_put(msg);    
}
Message:
  { "type": "CONFIG", "ip": "10.0.0.42", "data": { "ip": "10.0.0.42", "type": "CONFIG", "data": { "manufacturer": "atecom GmbH", "model": "Eagle", "serial-no": "253.00.01.0001.0002.592BDE31", "hardware-version": "1.1", "firmware-version": "1.3", "hostname": "Eagle1", "services": [ { "name": "gbe2", "type": "Eagle:1000BaseX", "admin_status": 2, "status": 4, "ip": "192.168.2.1", "mask": "255.255.0.0", "typ": "1000Base-X", "mac": "00:50:66:00:20:0b" }, { "name": "gbe3", "type": "Eagle:1000BaseX", "admin_status": 2, "status": 4, "ip": "192.168.3.1", "mask": "255.255.0.0", "typ": "1000Base-X", "mac": "00:50:66:00:20:0c" }, { "name": "gbe1", "type": "Eagle:1000BaseT", "admin_status": 2, "status": 4, "ip": "192.168.1.1", "mask": "255.255.255.0", "typ": "1000Base-T", "mac": "00:50:66:00:20:0a", "parents": [ { "host": "Eagle1", "service": "gbe1.4" } ] }, { "name": "gbe0", "type": "Eagle:1000BaseT", "admin_status": 2, "status": 4, "ip": "192.168.0.1", "mask": "255.255.255.0", "typ": "1000Base-T", "mac": "00:50:66:00:20:09", "parents": [ { "host": "Eagle1", "service": "gbe0.4" } ] }, { "name": "gbe0.4", "type": "Eagle:VLAN", "admin_status": 2, "status": 4, "ip": "192.168.111.1", "mask": "255.255.255.0", "typ": "1000Base-T", "vlan_id": 4, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "gbe1.4", "type": "Eagle:VLAN", "admin_status": 2, "status": 4, "ip": "172.16.13.1", "mask": "255.255.255.0", "typ": "1000Base-T", "vlan_id": 4, "parents": [ { "host": "Eagle1", "service": "gbe1" } ] }, { "name": "Mds1-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.5", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds2-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.1", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds3-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.3", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds4-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.4", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds5-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.2", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "SW-Mds1", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds1-L1-RX" } ] }, { "name": "SW-Mds2", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds2-L1-RX" } ] }, { "name": "SW-Mds3", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds3-L1-RX" } ] }, { "name": "SW-Mds4", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds4-L1-RX" } ] }, { "name": "SW-Mds5", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds5-L1-RX" } ] }, { "name": "Mds1-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 1, "parents": [ { "host": "Eagle1", "service": "SW-Mds1" } ] }, { "name": "Mds2-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 2, "parents": [ { "host": "Eagle1", "service": "SW-Mds2" } ] }, { "name": "Mds3-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 3, "parents": [ { "host": "Eagle1", "service": "SW-Mds3" } ] }, { "name": "Mds4-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 4, "parents": [ { "host": "Eagle1", "service": "SW-Mds4" } ] }, { "name": "Mds5-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 5, "parents": [ { "host": "Eagle1", "service": "SW-Mds5" } ] } ] } } }


CONFIG:
  { "ip": "10.0.0.42", "type": "CONFIG", "data": { "manufacturer": "atecom GmbH", "model": "Eagle", "serial-no": "253.00.01.0001.0002.592BDE31", "hardware-version": "1.1", "firmware-version": "1.3", "hostname": "Eagle1", "services": [ { "name": "gbe2", "type": "Eagle:1000BaseX", "admin_status": 2, "status": 4, "ip": "192.168.2.1", "mask": "255.255.0.0", "typ": "1000Base-X", "mac": "00:50:66:00:20:0b" }, { "name": "gbe3", "type": "Eagle:1000BaseX", "admin_status": 2, "status": 4, "ip": "192.168.3.1", "mask": "255.255.0.0", "typ": "1000Base-X", "mac": "00:50:66:00:20:0c" }, { "name": "gbe1", "type": "Eagle:1000BaseT", "admin_status": 2, "status": 4, "ip": "192.168.1.1", "mask": "255.255.255.0", "typ": "1000Base-T", "mac": "00:50:66:00:20:0a", "parents": [ { "host": "Eagle1", "service": "gbe1.4" } ] }, { "name": "gbe0", "type": "Eagle:1000BaseT", "admin_status": 2, "status": 4, "ip": "192.168.0.1", "mask": "255.255.255.0", "typ": "1000Base-T", "mac": "00:50:66:00:20:09", "parents": [ { "host": "Eagle1", "service": "gbe0.4" } ] }, { "name": "gbe0.4", "type": "Eagle:VLAN", "admin_status": 2, "status": 4, "ip": "192.168.111.1", "mask": "255.255.255.0", "typ": "1000Base-T", "vlan_id": 4, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "gbe1.4", "type": "Eagle:VLAN", "admin_status": 2, "status": 4, "ip": "172.16.13.1", "mask": "255.255.255.0", "typ": "1000Base-T", "vlan_id": 4, "parents": [ { "host": "Eagle1", "service": "gbe1" } ] }, { "name": "Mds1-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.5", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds2-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.1", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds3-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.3", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds4-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.4", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds5-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.2", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "SW-Mds1", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds1-L1-RX" } ] }, { "name": "SW-Mds2", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds2-L1-RX" } ] }, { "name": "SW-Mds3", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds3-L1-RX" } ] }, { "name": "SW-Mds4", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds4-L1-RX" } ] }, { "name": "SW-Mds5", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds5-L1-RX" } ] }, { "name": "Mds1-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 1, "parents": [ { "host": "Eagle1", "service": "SW-Mds1" } ] }, { "name": "Mds2-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 2, "parents": [ { "host": "Eagle1", "service": "SW-Mds2" } ] }, { "name": "Mds3-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 3, "parents": [ { "host": "Eagle1", "service": "SW-Mds3" } ] }, { "name": "Mds4-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 4, "parents": [ { "host": "Eagle1", "service": "SW-Mds4" } ] }, { "name": "Mds5-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 5, "parents": [ { "host": "Eagle1", "service": "SW-Mds5" } ] } ] } }


CONFIG (Date)
  { "manufacturer": "atecom GmbH", "model": "Eagle", "serial-no": "253.00.01.0001.0002.592BDE31", "hardware-version": "1.1", "firmware-version": "1.3", "hostname": "Eagle1", "services": [ { "name": "gbe2", "type": "Eagle:1000BaseX", "admin_status": 2, "status": 4, "ip": "192.168.2.1", "mask": "255.255.0.0", "typ": "1000Base-X", "mac": "00:50:66:00:20:0b" }, { "name": "gbe3", "type": "Eagle:1000BaseX", "admin_status": 2, "status": 4, "ip": "192.168.3.1", "mask": "255.255.0.0", "typ": "1000Base-X", "mac": "00:50:66:00:20:0c" }, { "name": "gbe1", "type": "Eagle:1000BaseT", "admin_status": 2, "status": 4, "ip": "192.168.1.1", "mask": "255.255.255.0", "typ": "1000Base-T", "mac": "00:50:66:00:20:0a", "parents": [ { "host": "Eagle1", "service": "gbe1.4" } ] }, { "name": "gbe0", "type": "Eagle:1000BaseT", "admin_status": 2, "status": 4, "ip": "192.168.0.1", "mask": "255.255.255.0", "typ": "1000Base-T", "mac": "00:50:66:00:20:09", "parents": [ { "host": "Eagle1", "service": "gbe0.4" } ] }, { "name": "gbe0.4", "type": "Eagle:VLAN", "admin_status": 2, "status": 4, "ip": "192.168.111.1", "mask": "255.255.255.0", "typ": "1000Base-T", "vlan_id": 4, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "gbe1.4", "type": "Eagle:VLAN", "admin_status": 2, "status": 4, "ip": "172.16.13.1", "mask": "255.255.255.0", "typ": "1000Base-T", "vlan_id": 4, "parents": [ { "host": "Eagle1", "service": "gbe1" } ] }, { "name": "Mds1-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.5", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds2-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.1", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds3-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.3", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds4-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.4", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "Mds5-L1-RX", "type": "Eagle:IPv4RX", "admin_status": 2, "status": 2, "itf": "gbe0", "dst_ip": "239.232.0.2", "dst_port": 5000, "src_ip": "0.0.0.0", "src_port": 0, "parents": [ { "host": "Eagle1", "service": "gbe0" } ] }, { "name": "SW-Mds1", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds1-L1-RX" } ] }, { "name": "SW-Mds2", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds2-L1-RX" } ] }, { "name": "SW-Mds3", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds3-L1-RX" } ] }, { "name": "SW-Mds4", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds4-L1-RX" } ] }, { "name": "SW-Mds5", "type": "Eagle:MPEGSwitch", "admin_status": 2, "status": 2, "parents": [ { "host": "Eagle1", "service": "Mds5-L1-RX" } ] }, { "name": "Mds1-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 1, "parents": [ { "host": "Eagle1", "service": "SW-Mds1" } ] }, { "name": "Mds2-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 2, "parents": [ { "host": "Eagle1", "service": "SW-Mds2" } ] }, { "name": "Mds3-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 3, "parents": [ { "host": "Eagle1", "service": "SW-Mds3" } ] }, { "name": "Mds4-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 4, "parents": [ { "host": "Eagle1", "service": "SW-Mds4" } ] }, { "name": "Mds5-ASIOUT", "type": "Eagle:ASIOutput", "admin_status": 2, "status": 2, "chn": 5, "parents": [ { "host": "Eagle1", "service": "SW-Mds5" } ] } ] }

Message:
  { "type": "EVENT", "ip": "10.0.0.42", "data": { "resource": "Eagle1", "event": "alert", "text": "FAN-1 failure", "severity": "Major", "dt": "2020-01-06 10:27:29.942" } }

EVENT:
  { "resource": "Eagle1", "event": "alert", "text": "FAN-1 failure", "severity": "Major", "dt": "2020-01-06 10:27:29.942" }

@dota17
Copy link
Member

dota17 commented Jan 13, 2020

I don't understand your question well. In case of short messages, the short string will be stored in char array[32], For the larger message(long string/Nested object), the json-object will store the address (*ptr/*c_object ) of string/nested object.

@ploxiln
Copy link
Contributor

ploxiln commented Jan 13, 2020

Look carefully at your original "CONFIG" message. Printing it out with indenting helps:

{
  "type": "CONFIG",
  "ip": "10.0.0.42",
  "data": {
    "ip": "10.0.0.42",
    "type": "CONFIG",
    "data": {
      "manufacturer": "atecom GmbH",
      "model": "Eagle",
      "serial-no": "253.00.01.0001.0002.592BDE31",
      "hardware-version": "1.1",
      "firmware-version": "1.3",
      "hostname": "Eagle1",
      "services": [
        {
          "name": "gbe2",
...

it is the original message that is double-nested, json_object_object_get_ex() works consistently and correctly, it's just a strange message structure.

@RudiSchloesser
Copy link
Author

Dear Pierce,
many thanks for looking into it and giving me the hint. My mistake.
Thanks again

@ploxiln
Copy link
Contributor

ploxiln commented May 4, 2021

you wrote "clusterId:" for the key to get, but it should be "clusterId"

@hawicz
Copy link
Member

hawicz commented May 4, 2021

@sohail-shareef, this is not a message forum, please don't hijack issues (especially other people's), and instead ask your questions at https://groups.google.com/g/json-c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants