Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong json document returned by BootstrapManager::parseQueueMsg when the payload is a numerical value #14

Open
Pronoe opened this issue Mar 2, 2022 · 5 comments
Labels
keep don't stale this issue

Comments

@Pronoe
Copy link
Contributor

Pronoe commented Mar 2, 2022

Hello,

for simple MQTT messages with a numerical value such as: Topic: SET/TEMP Payload: 21.5, the parseQueueMsg function returns a json document which contains only the string 21.5 and not as expected {"value":21.5}.
I have tested different MQTT messages:
Topic: CMD Payload: ON returns {"value":"ON"} - correct
Topic: SET Payload: {"TEMP":21.5} returns {"TEMP":21.5} - correct
Topic: SET Payload: 21.5 returns 21.5 - failed
Topic: SET Payload: t1t1 returns true - failed, and I noticed that deserializeJson function was returning no error
This strange behavior is coming from the deserializeJson function which returns no error when the payload is a numeric value or a string such as 'toto', 't1t1', 'true', 'false' ... but delivers a json object not formatted as expected.
So I have modified the code as below (see modifications in 3rd line):

 DeserializationError error = deserializeJson(jsonDoc, (const byte *)payload, length);
  // non json msg
  if (error || ((char)payload[0] != '{'))   // instead of if(error) - tests if the payload looks like a json structure
  {
    JsonObject root = jsonDoc.to<JsonObject>();
    root[VALUE] = message;
    if (DEBUG_QUEUE_MSG)
    {
      String msg = root[VALUE];
      Serial.println(msg);
    }
    return jsonDoc;
  }
  else
  { // return json doc
    if (DEBUG_QUEUE_MSG)
    {
      serializeJsonPretty(jsonDoc, Serial);
      Serial.println();
    }
    return jsonDoc;
  }

Then for the example of Topic: SET Payload: 21.5 returns 21.5, I get the result {"value":21.5} as expected.
Then I suggest to implement this modifications in a future version.
This apply also for the BootstrapManager::parseHttpMsg function.

@sblantipodi
Copy link
Owner

sblantipodi commented Mar 2, 2022

pull request will be really appreciated, in case I'll not see a pull request I'll do it in the next release :)
thank you very much for all the help and for the smart ideas you gived to this small bootsrapper. 👍

@Pronoe
Copy link
Contributor Author

Pronoe commented Mar 8, 2022

Ok, I will propose soon a pull request after some testing but I saw that a version 1.12.7 is in preparation that seems to address a similar issue. Should I wait that you release this version before ?

@sblantipodi
Copy link
Owner

I have just released the 1.2.7, I fixed a similar issue,
basically I addressed a problem where integer values was returned as null
{
"someval": 2
}

but I lack the part you pointed out... you can pull request from now on :)

@github-actions
Copy link

github-actions bot commented Apr 9, 2022

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label May 11, 2022
@sblantipodi sblantipodi reopened this May 16, 2022
@sblantipodi sblantipodi reopened this May 23, 2022
@sblantipodi sblantipodi reopened this Jun 2, 2022
@sblantipodi sblantipodi added keep don't stale this issue and removed Stale labels Jun 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep don't stale this issue
Projects
None yet
Development

No branches or pull requests

2 participants