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

How do I use the cJSON library?

I need to use cJSON to parse the uart data. I found the file in SDK15.3 but how can I use the related function?

Parents
  • Hi,

    just add sources and headers to your project and call functions as usual... you also need to add mem_manager library from SDK. cJSON has a good README with detailed instructions how to use this library.

  • Is there a routine example of how to use the memory management library?

  • Hi,

    the only thing you need is to define some number of memory blocks in sdk_config.h, there are several sizes of memory blocks. Since I don't know neither the length and count of your json strings, nor the logic of your program, I cannot suggest you an optimal configuration. You can enable logging of memory manager in sdk_config and see how much memory is requested. Try to configure as much memory as possible, then run your  build/parse tasks and call nrf_mem_diagnose(), it will show some memory usage statistics.

    cJSON with memory manager is used by background_dfu example in SDK, you can look at its config.

  • Sorry, I still have doubts. I still don't understand the relationship between the memory management module and the cJSON library in the background_dfu example. Under extreme conditions, I need to parse 8192 bytes of data and build 47077 bytes of data (not including 3637 bytes of data in JSON format). What should I do? 

  • In addition, how to increase the uart buffer? I am increasing the TX BUFFER and RX BUFFER to 8192, but under extreme conditions, I will send the constructed 47077 bytes of JSON format data through the serial port. TX BUFFER is obviously not enough

  • Hi,

    sorry for late answer. If your MCU is 52840, there is no tight constraints on memory. You can expect about 70k for parsed data. Additionally, for printed output you have to allocate a buffer capable to hold maximum string you can build (48k). It's better to allocate this buffer as static array, not mixing with cJSON memory, and write an UART routine that will send characters directly from this array (otherwise you have to allocate ONE MORE buffer of that size as UART TX buffer...)  Or maybe in your case it worth to look for a json library with serialization, that can use callbacks instead of preallocated buffers.

Reply
  • Hi,

    sorry for late answer. If your MCU is 52840, there is no tight constraints on memory. You can expect about 70k for parsed data. Additionally, for printed output you have to allocate a buffer capable to hold maximum string you can build (48k). It's better to allocate this buffer as static array, not mixing with cJSON memory, and write an UART routine that will send characters directly from this array (otherwise you have to allocate ONE MORE buffer of that size as UART TX buffer...)  Or maybe in your case it worth to look for a json library with serialization, that can use callbacks instead of preallocated buffers.

Children
Related