JsonParserGeneratorRK char instead of String

Hi All,

I’m hoping to use JsonParserGeneratorRK to parse some json data coming into the particle via ble. I would like to use a char instead of a String:

    char strValue[20];
    int intValue;
    bool boolValue;
    parser1.parse();
    if(parser1.getOuterValueByKey("EMAIL", strValue)) {
      memcpy(config_data.email, strValue, sizeof(strValue));
    } else if...

My ignorance around Strings is still causing me issues. I was hoping to just drop in char strValue[20] in place of String strValue as used in the library examples. But I get the following compile error. Does anyone have any suggestions or explanation on the error?

lib/JsonParserGeneratorRK/src/JsonParserGeneratorRK.h:417:24: error: cannot bind non-const lvalue reference of type 'String&' to an rvalue of type 'String'
  417 |    return getTokenValue(value, result);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~

You cannot do that because the library would not know the length of the string and could overwrite the end. Change it to:

JSONParserString js(strValue, sizeof(strValue));
if(parser1.getOuterValueByKey("EMAIL", js)) {
2 Likes

Thanks Rick

I got it to compile with the following code:

    JsonParserString js(strValue, sizeof(strValue));
    parser1.parse();
    if(parser1.getOuterValueByKey("EMAIL", js)) {
      memcpy(config_data.email, strValue, sizeof(strValue));
    } else ...

However when I use memcpy, I am now getting a bunch of garbage at the end of the received string, and the last character of config_data.email is missing. I assume I am not copying my received value to config_data.email correctly.

1 Like

What is the declaration for the type of config_data?

1 Like

I think it was due to my

char strValue[20]

being incorrectly sized. The variable config_data is a struct and email is as follows:

char email[40];

I have changed strValue size to 40, to match my config_data.email, and everything seems to be behaving now. Thanks for your help.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.