This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

cJSON in Mesh SDK?

Hi all,

I have some issues to print and parse JSON file using cJSON features. My code works without problems in SDK, but when I try the same code in Mesh part, I am not able to print and parse the file. It seems that file is to big, because when I remove some objects from my array, I can see the output. Do you maybe know how can I fix this? 

Best regards,

Adnan.

Parents
  • Hi,

    Could you provide more details regarding the issue? Do you get any kind of error? How does it fail? Any kind of logs will also be helpful.

  • Hi Mttrinh,

    Well, I do not get any kind of error...I use the same code for printing out JSON file in SDK and Mesh SDK. In SDK, it works without problems and I can see the output, but when I move to Mesh SDK I am not able to see the output...when I remove one part of the code and make the JSON quite shorter it works also in Mesh SDK. What can be the possible issue there? Can you suggest me how to fix it?

    Also, I send you my code with outputs for both SDKs.

    Code:

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    //#include "nrf_delay.h"
    //#include "boards.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "cJSON.h"
    
    char *create_JSON(){
    
        char *out;
        cJSON *root, *cars, *car;
    
        /* create root node and array */
        root = cJSON_CreateObject();
        cars = cJSON_CreateArray();
    
        /* add cars array to root */
        cJSON_AddItemToObject(root, "cars", cars);
    
        /* add 1st car to cars array */
        cJSON_AddItemToArray(cars, car = cJSON_CreateObject());
        cJSON_AddItemToObject(car, "CarType1", cJSON_CreateString("BMW"));
        cJSON_AddItemToObject(car, "carID1", cJSON_CreateString("bmw123"));
    
        /* add 2nd car to cars array */
        cJSON_AddItemToArray(cars, car = cJSON_CreateObject());
        cJSON_AddItemToObject(car, "CarType2", cJSON_CreateString("mercedes"));
        cJSON_AddItemToObject(car, "carID2", cJSON_CreateString("mercedes123"));
    
        cJSON_AddItemToArray(cars, car = cJSON_CreateObject());
        cJSON_AddItemToObject(car, "CarType", cJSON_CreateString("VW"));
        cJSON_AddItemToObject(car, "carID", cJSON_CreateString("vw123"));
    
        /* print everything */
        out = cJSON_Print(root);
    
        /* free all objects under root and root itself */
        cJSON_Delete(root);
    
        return out;
    }
    
    int main(){
    
        char *out = create_JSON();
        printf("%s\n", out);
        free(out);
        return 0;
    }
    
    
    
    
    
    
    
    

    SDK output:

    {
    	"cars":	[{
    			"CarType1":	"BMW",
    			"carID1":	"bmw123"
    		}, {
    			"CarType2":	"mercedes",
    			"carID2":	"mercedes123"
    		}, {
    			"CarType":	"VW",
    			"carID":	"vw123"
    		}]
    }

    Mesh SDK output:

    But as I mentioned before, when I comment one part of the code (in this case VW part), this is the output in Mesh SDK:


    {
    	"cars":	[{
    			"CarType":	"BMW",
    			"carID":	"bmw123"
    		}, {
    			"CarType":	"mercedes",
    			"carID":	"mercedes123"
    		}]
    }

    I hope that I provided you a bit more information about my problem.

    Best regards,

    Adnan.

Reply
  • Hi Mttrinh,

    Well, I do not get any kind of error...I use the same code for printing out JSON file in SDK and Mesh SDK. In SDK, it works without problems and I can see the output, but when I move to Mesh SDK I am not able to see the output...when I remove one part of the code and make the JSON quite shorter it works also in Mesh SDK. What can be the possible issue there? Can you suggest me how to fix it?

    Also, I send you my code with outputs for both SDKs.

    Code:

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    //#include "nrf_delay.h"
    //#include "boards.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "cJSON.h"
    
    char *create_JSON(){
    
        char *out;
        cJSON *root, *cars, *car;
    
        /* create root node and array */
        root = cJSON_CreateObject();
        cars = cJSON_CreateArray();
    
        /* add cars array to root */
        cJSON_AddItemToObject(root, "cars", cars);
    
        /* add 1st car to cars array */
        cJSON_AddItemToArray(cars, car = cJSON_CreateObject());
        cJSON_AddItemToObject(car, "CarType1", cJSON_CreateString("BMW"));
        cJSON_AddItemToObject(car, "carID1", cJSON_CreateString("bmw123"));
    
        /* add 2nd car to cars array */
        cJSON_AddItemToArray(cars, car = cJSON_CreateObject());
        cJSON_AddItemToObject(car, "CarType2", cJSON_CreateString("mercedes"));
        cJSON_AddItemToObject(car, "carID2", cJSON_CreateString("mercedes123"));
    
        cJSON_AddItemToArray(cars, car = cJSON_CreateObject());
        cJSON_AddItemToObject(car, "CarType", cJSON_CreateString("VW"));
        cJSON_AddItemToObject(car, "carID", cJSON_CreateString("vw123"));
    
        /* print everything */
        out = cJSON_Print(root);
    
        /* free all objects under root and root itself */
        cJSON_Delete(root);
    
        return out;
    }
    
    int main(){
    
        char *out = create_JSON();
        printf("%s\n", out);
        free(out);
        return 0;
    }
    
    
    
    
    
    
    
    

    SDK output:

    {
    	"cars":	[{
    			"CarType1":	"BMW",
    			"carID1":	"bmw123"
    		}, {
    			"CarType2":	"mercedes",
    			"carID2":	"mercedes123"
    		}, {
    			"CarType":	"VW",
    			"carID":	"vw123"
    		}]
    }

    Mesh SDK output:

    But as I mentioned before, when I comment one part of the code (in this case VW part), this is the output in Mesh SDK:


    {
    	"cars":	[{
    			"CarType":	"BMW",
    			"carID":	"bmw123"
    		}, {
    			"CarType":	"mercedes",
    			"carID":	"mercedes123"
    		}]
    }

    I hope that I provided you a bit more information about my problem.

    Best regards,

    Adnan.

Children
No Data
Related