1

I've been trying to use the following code, taken from an example, I've had to change json_object_object_get(struct json_object *obj, const char *key) to json_object_get_ex(struct json_object *obj, const char *key, struct json_object **value)

I'm sorry I've already posted similar questions as I've been trying to find a way to parse json from a socket for days and I'm getting desperate, but I've done some more work and research and I think this is much close. The error I'm getting from compiling the below is:

server4.c: In function ‘main’: server4.c:62: error: dereferencing pointer to incomplete type server4.c:68: warning: assignment makes pointer from integer without a cast

    struct json_object *jobj, *val_jobj, *value;                                        
    char const *val;                                                              
    char buf[50];                                                                              
    fgets(buf, sizeof(buf), stdin);                                               
    printf("Input JSON : %s", buf);         
    char const *val;
    *jobj = json_tokener_parse(buf);
    if (is_error(jobj))    
    return (-1);  
    printf("Received JSON in String format : %s\n",    
    json_object_to_json_string(jobj));

  //Get the value for the key "name"      
    val_jobj = json_object_object_get_ex(jobj, "name", &value);    
    printf("Extracted value for command : %s\n",    

    //Get the string from the value of the key "name"                                         
    val = json_object_get_string(val_jobj);                                       
    printf("String value returned : %s\n", val);      

I can't see what is wrong and I don't fully understand how json-c works, I'm also more familiar with c++, though of course I use pointers there too. Either way, reading through some json parsers for c++, I've found them way easier to understand.

Thanks in advance

jewfro
  • 253
  • 1
  • 5
  • 15
  • Can you break this out into something easier to read? As in, a smaller test function that exhibits the same problem. – bentank Apr 08 '15 at 04:46
  • 1
    The compile error is on line 62. So you need to put line numbers in your code snippet or else point out which line is line 62. – kaylum Apr 08 '15 at 05:33

2 Answers2

1

The json_object structure is an opaque type. It is private and pointers to it can not be dereferenced by code outside the json-c library.

json_tokener_parse returns json_object* so line 62 should be:

jobj = json_tokener_parse(buf);

That is, don't dereference jobj.

kaylum
  • 13,833
  • 2
  • 22
  • 31
0

The only dereference shown in your code is

*jobj = json_tokener_parse(buf);

so this must be the line 62 to which the error message refers.

You're telling the compiler to take the value of the pointer jobj (to which you never assigned a value, so there's error number 1) and in the space pointed-to by that value to store the value returned from json_tokener_parse(buf). Trouble is, you've not told the compiler what a struct json_object is, so jobj is a pointer to a type which is incomplete - the compiler doesn't know how big it is, nor what components it's made of. I presume there's a JSON header file that you've neglected to #include.

The warning about line 68 is a separate but likely related problem, but the line which would be 68 (if my guess about 62 is correct) is a comment.

mlp
  • 809
  • 7
  • 21
  • Sorry, yes it's to `*jobj = json_tokener_parse(buf);` – jewfro Apr 08 '15 at 07:03
  • I've got `#include ` and I'm compiling with -l json. It's defo linking as it gave me the error message about the previous get_object function being deprecated – jewfro Apr 08 '15 at 07:04
  • @Alan Au I can't check that again now, as our mini processor where the server programme is being installed somewhere tomorrow, so it's offline. But I'm fairly sure I changed it to deference because before I was getting an error about casting an int to a pointer, so I thought that must be because I wasn't derereferencing the variable in `*jobj = json_tokener_parse(buf)`. – jewfro Apr 08 '15 at 12:09
  • I don't know what your other error was. If you get that post it again. But you absolutely cannot dereference jobj. Even if you didn't get a compile error you would get a runtime error. jobj is uninitialised at that point so you aboslutely cannot dereference it regardless of anything to do with json-c. – kaylum Apr 08 '15 at 20:31