bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.7k stars 1.12k forks source link

Parser can not handle value bigger then 4,294,967,295 #2049

Closed Jercyvz closed 7 months ago

Jercyvz commented 7 months ago

Description Hi. I'm trying to parse next JSON sting below and want to extract "FrequencyHertz" value "{\"APIVersion\":\"2\",\"Data\":{\"AbsoluteAngleOfArrivalRadians\":6.981317007977318,\"AngleOfArrivalErrorRadians\":0.0,\"AngleOfArrivalRadians\":0.0,\"CorrelationKey\":\"3336226785\",\"DetectionCount\":12,\"EpochTimeMilliSeconds\":1702299450911,\"FrequencyHertz\":57000000000,\"Name\":\"FrSky\",\"Protocol\":\"ACCESS\",\"RSSI\":-67}"

Everything works fine unti value is set bigger then 4294967295. And this time parser returns "Ok" status.

Can you help with this?

Thanks, Eugene.

Troubleshooter's report

  1. The program uses ArduinoJson 7
  2. The issue happens at run time
  3. The issue concerns deserialization
  4. The program doesn't check the error
  5. deserializeJson() returns Ok
  6. Program fails to extract values from the JsonDocument
  7. Program doesn't use DeserializationOption::Filter
  8. Serialized document starts with a curly brace
  9. Issue is not a confusion between array and object

Environment

Reproduction code

 DynamicJsonDocument doc(packetSize);
    DeserializationError error = deserializeJson(doc, packetBuffer);
    Serial.print("Error=");
    Serial.println(error.c_str());
    if (error.code() != DeserializationError::Ok) {
        if (state.RelayCanBeTurnedOff())
        {
            ErrorState();
            state.currentState = CurrentState::Error;
        }
    }
    else {
        unsigned long freqValue = doc["Data"]["FrequencyHertz"];
        int rssiValue = doc["Data"]["RSSI"];

        SetRelayLedState(rssiValue, freqValue);

        Serial.print("FreqValue=");
        Serial.println(freqValue);

        Serial.print("RSSI=");
        Serial.println(rssiValue);
    }

Remarks I'm using Arduino UNO R4 minima

bblanchon commented 7 months ago

Hi @Jercyvz,

unsigned long cannot store values above 4,294,967,295. You must switch to unsigned long long.

This is a duplicate of #2024.

Best regards. Benoit

Jercyvz commented 7 months ago

OMG!!! That was silly question. Sorry for that. Thanks a lot for your fast answer. My problem resolved.

bblanchon commented 7 months ago

You're welcome, @Jercyvz. Thank you for using ArduinoJson! Don't forget to cast a star to support the project :wink: