PaulB
PaulB

Reputation: 73

cJSON_Print dosen't show updated values

Im using cJSON by Dave Gamble and have the following problem. If I change the value within a cJSON struct and then use the cJSON_Print command I dont get the updated values, instead I still get the default ones.

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

void main(){
    cJSON *test = cJSON_Parse("{\"type\":       \"rect\", \n\"width\":      1920, \n\"height\":     1080, \n\"interlace\":  false,\"frame rate\": 24\n}");
    printf("cJSONPrint: %s\n  cJSONvalueint: %d\n",cJSON_Print(test), cJSON_GetObjectItem(test,"frame rate")->valueint);
    cJSON_GetObjectItem(test,"frame rate")->valueint=15;
    printf("cJSONPrint: %s\n  cJSONvalueint: %d\n",cJSON_Print(test), cJSON_GetObjectItem(test,"frame rate")->valueint);
}

this is the code I used for a small test and it gives me those results:

cJSONPrint: {
    "type": "rect",
    "width":    1920,
    "height":   1080,
    "interlace":    false,
    "frame rate":   24
}

cJSONvalueint: 24
cJSONPrint: {
    "type": "rect",
    "width":    1920,
    "height":   1080,
    "interlace":    false,
    "frame rate":   24
}
cJSONvalueint: 15

Does anybody know what I'm doing wrong, and how to get the correct values with the cJSON_Print command ?

Upvotes: 3

Views: 1776

Answers (1)

sig11
sig11

Reputation: 539

I think the proper call you need to use the cJSON_SetIntValue macro.

It sets the valueint and the valuedouble on the object instead of just the valueint.

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

void main(){
    cJSON *test = cJSON_Parse("{\"type\":       \"rect\", \n\"width\":      1920, \n\"height\":     1080, \n\"interlace\":  false,\"frame rate\": 24\n}");    
    printf("cJSONPrint: %s\n  cJSONvalueint: %d\n",cJSON_Print(test), cJSON_GetObjectItem(test,"frame rate")->valueint);

    cJSON_SetIntValue(cJSON_GetObjectItem(test, "frame rate"), 15);

    printf("cJSONPrint: %s\n  cJSONvalueint: %d\n",cJSON_Print(test), cJSON_GetObjectItem(test,"frame rate")->valueint);
}

That will return:

$ ./test
cJSONPrint: {
        "type": "rect",
        "width":        1920,
        "height":       1080,
        "interlace":    false,
        "frame rate":   24
}
  cJSONvalueint: 24
cJSONPrint: {
        "type": "rect",
        "width":        1920,
        "height":       1080,
        "interlace":    false,
        "frame rate":   15
}
  cJSONvalueint: 15

Upvotes: 4

Related Questions