1

Parameters file defines the creation of entities and stored in flash. Its structure is given, and all fields ( keys & values ) are given. In current phase - those parameters are hard-coded in a const char* cont_params, as shown below.

My goal, is when array is empty, for example RF_2entity - means that current MCU does not uses its RF abilities, and will not init any RF entities. The way RF_2entity is defined below, as empty array, [] , ArduinoJSON's isNull() function return 0 in that case.

How can I keep parameters file's structure and logic, rather that fill its values with 255 for example.

  Serial.begin(115200);
  StaticJsonDocument<1200> doc;
  const char *cont_params = "{\"entityType\": [1,1,1,1],\
                    \"SWname\": [\"SW_0\",\"SW_1\",\"SW_2\",\"SW_3\"],\
                    \"SW_buttonTypes\": [2,1,1,1],\
                    \"SW_timeout\": [10,11,12,13],\
                    \"SWvirtCMD\":[0,0,0,0],\
                    \"Winname\": [],\
                    \"WextInputs\": [],\
                    \"WinvirtCMD\":[],\
                    \"RF_2entity\": [],\
                    \"v_file\": 0.5}";
  DeserializationError err = deserializeJson(doc, cont_params);
  JsonArray array = doc["Winname"].as<JsonArray>();
  serializeJsonPretty(array,Serial);
  Serial.println(array.isNull());

output:

[]0
2
  • sorry, but I don't understand the question. what is the problem? isNull() returned 0 (false), not 1 (true)
    – Juraj
    Jun 2, 2023 at 5:08
  • @Juraj - Question edited. you are right.... isNull() returns that array is not empty.
    – guyd
    Jun 2, 2023 at 5:18

1 Answer 1

1

The method isNull() is not what you want. It tells you whether the instance holds an array at all or not. And of course, your specific instance holds an array, though an empty one. Therefore, the method correctly returns false, aka 0.

As the API documentation shows, there are more methods to use.

What you want is size(), which returns the number of elements in the array. It will even return 0 on an instance without an array.

1
  • in that case, doc["Winname"].size() will suffice. I don't really need to use an array.
    – guyd
    Jun 2, 2023 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.