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 Reply Children
  • 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?

  • 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

Related