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.

  • Hi,

    I set UART_TX_BUF_SIZE is 8192. Can this make the uart send out nearly 48k without error? on one time?

Reply Children
  • Depends on how you've implemented it. You can allocate a very small buffer, then send your data fragment by fragment at APP_UART_TX_EMPTY event (if you use app_uart_fifo). WIth nrfx_uarte library you don't need any intermediate buffer, transmission is done directly from buffer you've passed to tx function (just make sure not to free it until it transferred completely). nRF52840 can transfer up to 65535 bytes with nrfx_uarte, but if you require compatibility with lower series, you have to split transmission by 255 bytes.

  • OK. I will try that. But now there is another problem. When I use the cJSON_CreateNumber function, the system will crash. DEBUG found that when this function was called, it crashed in HardFault_Handler.

    Further tracking reveals that as soon as I pass the external numeric data into the valuedouble of the cJSON structure, ie executing item->valuedouble=num(cJSON *item) will crash in HardFault_Handler

  • When I don't call cJSON_CreateNumber everything works fine, as soon as I call it, it will crash

  • How can I solve this problem?

  • Another new problem is that if I mask nrf_mem_init and cJSON_Init(). use malloc instead of nrf_malloc. Then cJSON_CreateNumber can be called normally, the item->valuedouble=num; in the function can be executed normally, if you use nrf_malloc, it will crash. (Excludes the extra effects, only calling cJSON_CreateNumber(2) in the main function.)