Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A ArduinoJson
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 24
    • Issues 24
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Benoît Blanchon
  • ArduinoJson
  • Issues
  • #1256
Closed
Open
Issue created May 03, 2020 by Administrator@rootContributor

De/Serialize enum type with JSON

Created by: lloydrichards

Greetings,

I'm trying to set up a state machine using an esp32 and its deep sleep functionality. I want to save the current state to the SPIFFS using JSON and then read it again after wake up. I'm doing this already with other variables but when I try to include my SENSOR_STATUS and BATTERY_STATUS which are enum types in my library I get errors about the type.

I've been trying to figure out the JsonVariant::as but to no avail.

Basically, I have an enum:

enum STATUS_ERROR
{
  ERROR_READ = -3,
  ERROR_WRITE = -2,
  ERROR_UNDEFINED = -1,
  UNMOUNTED = 0,
  SUCCESS = 1
};

And i want to save it as a JSON so i can store and retrieve the values. I recreated two functions

void MimirTesting::initConfig()
{
    if (SPIFFS.begin())
    {

        Serial.println("mounted file system");
        if (SPIFFS.exists("/config.json"))
        {
            //file exists, reading and loading
            Serial.println("reading config file");
            fs::File configFile = SPIFFS.open("/config.json", "r");
            if (configFile)
            {
                Serial.println("opened config file");
                size_t size = configFile.size();
                // Allocate a buffer to store contents of the file.
                std::unique_ptr<char[]> buf(new char[size]);

                configFile.readBytes(buf.get(), size);
                DynamicJsonDocument configJson(1024);
                DeserializationError error = deserializeJson(configJson, buf.get());
                if (error)
                {
                    Serial.println("failed to load json config");
                    return;
                }
                Serial.println("\nparsed json");
                serializeJson(configJson, Serial);

                JsonVariant _SENSOR_STATUS = configJson["SENSOR_STATUS"];
                SENSOR_STATUS = _SENSOR_STATUS.as<STATUS_ERROR>();
                JsonVariant _WIFI_STATUS = configJson["WIFI_STATUS"];
                WIFI_STATUS = _WIFI_STATUS.as<STATUS_ERROR>();
                JsonVariant _SERVER_STATUS = configJson["SERVER_STATUS"];
                SERVER_STATUS = _SERVER_STATUS.as<STATUS_ERROR>();
                JsonVariant _MICROSD_STATUS = configJson["MICROSD_STATUS"];
                MICROSD_STATUS = _MICROSD_STATUS.as<STATUS_ERROR>();
                JsonVariant _SHT31D_L_STATUS = configJson["SHT31D_L_STATUS"];
            }
            else
            {
                Serial.println("failed to load json config");
            }
        }
    }
    else
    {
        Serial.println("failed to mount FS");
    }
}

and

void MimirTesting::saveConfig()
{
    DynamicJsonDocument newConfigJson(1024);
    newConfigJson["SENSOR_STATUS"] = SENSOR_STATUS;
    newConfigJson["WIFI_STATUS"] = WIFI_STATUS;
    newConfigJson["SERVER_STATUS"] = SERVER_STATUS;
    newConfigJson["MICROSD_STATUS"] = MICROSD_STATUS;

fs::File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile)
    {
        Serial.println("failed to open config file for writing");
    }

    //serializeJson(newConfigJson, Serial);
    serializeJson(newConfigJson, configFile);
    configFile.close();
}

But running this I get this error:

lib/MimirTesting/MimirTesting.cpp:357:65:   required from here
.pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Variant/VariantRef.hpp:259:24: error: no matching function for call to 'variantAs(ArduinoJson6150_0000010::VariantData* const&)'
     return variantAs<T>(_data);
                        ^
In file included from .pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Operators/VariantOr.hpp:9:0,
                 from .pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Operators/VariantOperators.hpp:9,
                 from .pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Variant/VariantRef.hpp:12,
                 from .pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Array/ArrayIterator.hpp:8,
                 from .pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Array/ArrayRef.hpp:8,
                 from .pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson.hpp:17,
                 from .pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson.h:9,
                 from lib/MimirTesting/MimirTesting.cpp:16:
.pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Variant/VariantAs.hpp:55:59: note: candidate: template<class T> typename ArduinoJson6150_0000010::enable_if<ArduinoJson6150_0000010::is_integral<T>::value, T>::type ArduinoJson6150_0000010::variantAs(const ArduinoJson6150_0000010::VariantData*)
 inline typename enable_if<is_integral<T>::value, T>::type variantAs(
                                                           ^
.pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Variant/VariantAs.hpp:55:59: note:   template argument deduction/substitution failed:
.pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Variant/VariantAs.hpp: In substitution of 'template<class T> typename ArduinoJson6150_0000010::enable_if<ArduinoJson6150_0000010::is_integral<T>::value, T>::type ArduinoJson6150_0000010::variantAs(const ArduinoJson6150_0000010::VariantData*) [with T = STATUS_ERROR]':
.pio/libdeps/esp32dev/ArduinoJson_ID64/src/ArduinoJson/Variant/VariantRef.hpp:259:24:   required from 'typename ArduinoJson6150_0000010::enable_if<(((! ArduinoJson6150_0000010::is_same<T, ArduinoJson6150_0000010::ArrayRef>::value) && (! ArduinoJson6150_0000010::is_same<T, ArduinoJson6150_0000010::ObjectRef>::value)) && (! ArduinoJson6150_0000010::is_same<T, ArduinoJson6150_0000010::VariantRef>::value)), typename ArduinoJson6150_0000010::VariantAs<T>::type>::type ArduinoJson6150_0000010::VariantRef::as() const [with T = STATUS_ERROR; typename ArduinoJson6150_0000010::enable_if<(((! ArduinoJson6150_0000010::is_same<T, ArduinoJson6150_0000010::ArrayRef>::value) && (! ArduinoJson6150_0000010::is_same<T, ArduinoJson6150_0000010::ObjectRef>::value)) && (! ArduinoJson6150_0000010::is_same<T, ArduinoJson6150_0000010::VariantRef>::value)), typename ArduinoJson6150_0000010::VariantAs<T>::type>::type = STATUS_ERROR]'

I'm a little lost now and not sure where to go so if anyone has any suggestions on what I'm missing, I would be very grateful!

Thanks

Assignee
Assign to
Time tracking