Working with json on the Host using Notecard Arduino lib does not work as expected

Hi All,

Any ideas why out would ne NULL in this simple test ?

  char *test = "{\"num\": 1}";
  J *jsn = JParse(test);
  
  char *out = JPrintUnformatted(jsn);

  if (out != NULL) {
    serialDebug.println(out);
  } else {
    serialDebug.println("json serialize error");
  }

Thanks,
Serge

Strangely the following test modified to work with the original cJSON library works just fine but does not work with the cJSON shipped with Notecard Arduino Lib. I am testing with a Swan

#include <Notecard.h>

#define serialDebug SerialUSB
static const size_t MAX_SERIAL_WAIT_MS = 10000;

J *root = NULL;
J *nmeaParseErrors = NULL;

void setup() {
  serialDebug.begin(115200);
  size_t begin_serial_wait_ms = ::millis();
  while (!serialDebug && (MAX_SERIAL_WAIT_MS > (::millis() - begin_serial_wait_ms))) {
    ; // wait for serial port to connect. Needed for native USB
  }

  root = JCreateObject();

  JAddTrueToObject(root, "boolean");
  JAddStringToObject(root, "string", "test string");
  JAddObjectToObject(root, "obj");

  nmeaParseErrors = JAddArrayToObject(root, "errorList"); 

  J *oo = JCreateObject();
  JAddNumberToObject(oo, "count", 5.0);
  JAddNumberToObject(oo, "errorCode", 1.0);
  
  JAddItemToArray(nmeaParseErrors, oo); 

}

void loop() {
  serialDebug.println(JVersion());
  serialDebug.println(JPrintUnformatted(root));

  delay(5000);
}

cJSON version


#include <cJSON.h>

#define serialDebug SerialUSB
static const size_t MAX_SERIAL_WAIT_MS = 10000;

cJSON *root = NULL;
cJSON *nmeaParseErrors = NULL;

void setup() {
  serialDebug.begin(115200);
  size_t begin_serial_wait_ms = ::millis();
  while (!serialDebug && (MAX_SERIAL_WAIT_MS > (::millis() - begin_serial_wait_ms))) {
    ; // wait for serial port to connect. Needed for native USB
  }

  root = cJSON_CreateObject();

  cJSON_AddTrueToObject(root, "boolean");
  cJSON_AddStringToObject(root, "string", "test string");
  cJSON_AddObjectToObject(root, "obj");

  nmeaParseErrors = cJSON_AddArrayToObject(root, "errorList"); 

  cJSON *oo = cJSON_CreateObject();
  cJSON_AddNumberToObject(oo, "count", 5.0);
  cJSON_AddNumberToObject(oo, "errorCode", 1.0);
  
  cJSON_AddItemToArray(nmeaParseErrors, oo); 

}

void loop() {
  serialDebug.println(cJSON_Version());
  serialDebug.println(cJSON_PrintUnformatted(root));

  delay(5000);
}

Hi there @ssozonoff!

This does seem strange! Many thanks for providing a SSCCE. I’ll take it for a spin and see if I can reproduce the problem.

I’ve reproduced your issue. I’ll try to find the root cause.

Great that you were able to reproduce this, thanks for having a look into it.

Hi there again @ssozonoff!

So, the quick fix is to instantiate Notecard and call Notecard::begin() or one of its overloads. This is necessary to initialize some internals before cjson will work.

Can I ask, what’s your use case for using cjson without the Notecard?

Hi Mat,

Thanks for your reply. The reason for doing this was actually to test the construction of some Json structures on a standalone host (Swan) since my actual setup is outside :slight_smile:

Thanks,
Serge

Ah, gotcha! Makes sense. Does using Notecard::begin() work for you in this scenario?