Skip to content

Comparison operators for custom types #2036

Open
@MoonModules

Description

@MoonModules

Description

  1. The program uses ArduinoJson 6
  2. The issue happens at compile time
  3. The error is not in the list

See reproduction code below. Using a custom defined struct works for assignments (using Converter<>) as described in https://arduinojson.org/news/2021/05/04/version-6-18-0/

But I cannot get it to work for comparison operators (like var["value"] != value in below example).

I get this error:
.pio/libdeps/esp32dev/ArduinoJson/src/ArduinoJson/Variant/VariantCompare.hpp:203:15: error: 'ArduinoJson::V6214PB2::detail::Comparer<Coordinate, void> comparer' has incomplete type
Comparer comparer(rhs);

Tried to add Comparer like functions but all resulted in compile errors.

Help / code example really appreciated.

Thx
Ewoud

Troubleshooter's report

  1. The program uses ArduinoJson 6
  2. The issue happens at compile time
  3. The error is not in the list

Environment

  • Microcontroller: ESP32
  • Core/Framework: Expressif 3.20014.0
  • IDE: VSCode 1.85 PIO v3.3.2

Reproduction code

This code:

template <typename Type>
JsonObject setValue(const char * id, Type value) {
  JsonObject var = findVar(id);
  if (var["value"].isNull() || var["value"] != value) {
    var["value"] = value;
  }
}

works for:

mdl->setValue<int>("var1", 10);

but not for:

Coordinate xyz;
xyz.x = 1;
xyz.y = 2; 
xyz.z = 3;
mdl->setValue<Coordinate>("var2", xyz, 0);

This code is present:

template <>
struct Converter<Coordinate> {
  static bool toJson(const Coordinate& src, JsonVariant dst) {
    dst["x"] = src.x;
    dst["y"] = src.y;
    dst["z"] = src.z;
    return true;
  }

  static Coordinate fromJson(JsonVariantConst src) {
    return Coordinate{src["x"], src["y"], src["z"]};
  }

  static bool checkJson(JsonVariantConst src) {
    return src["x"].is<int>() && src["y"].is<int>() && src["z"].is<int>();
  }
};


Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions