Skip to content

Support variant's adjacent tagging representation with multiple names #2985

Description

@stripe2933

Consider this JSON example:

...
  "cameras" : [
    {
      "type": "perspective",
      "perspective": {
        "aspectRatio": 1.0,
        "yfov": 0.7,
        "zfar": 100,
        "znear": 0.01
      }
    },
    {
      "type": "orthographic",
      "orthographic": {
        "xmag": 1.0,
        "ymag": 1.0,
        "zfar": 100,
        "znear": 0.01
      }
    }
  ],
...

I want to serialize/deserialize each cameras with the following structs:

struct OrthographicCamera {
    float xmag;
    float ymag;
    float zfar;
    float znear;
};

struct PerspectiveCamera {
    std::optional<float> aspectRatio;
    float yfov;
    std::optional<float> zfar;
    float znear;
};

struct Camera {
    std::string_view name;
    std::variant<PerspectiveCamera, OrthographicCamera> camera;
};

template <>
struct glz::meta<std::variant<ggltf::PerspectiveCamera, ggltf::OrthographicCamera>> {
    static constexpr std::string_view tag = "type";
    static constexpr std::array ids { "perspective", "orthographic" };
//    static constexpr std::string_view content = ?;
};

But this is not possible with the current glaze variant handling mechanism: alternative tag name is determined by type, so a single fixed content cannot be specified.

It would be good if the type of content could be array of std::string_view (whose length must be as same as the ids), like:

template <>
struct glz::meta<std::variant<ggltf::PerspectiveCamera, ggltf::OrthographicCamera>> {
    static constexpr std::string_view tag = "type";
    static constexpr std::array ids { "perspective", "orthographic" };
    static constexpr std::array content { "perspective", "orthographic" };
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions