【问题标题】:How to parse json file with std::optional< std::variant > type in C++?如何使用 C++ 中的 std::optional< std::variant > 类型解析 json 文件?
【发布时间】:2020-03-11 20:07:54
【问题描述】:

如何在 C++ 中解析嵌套的 json?我正在寻找一个能够解析嵌套 json 的 json 解析器。尤其是下面示例 json 中的这个字段:

optional&lt;variant&lt;bool, Secondary&gt;&gt; secondary;

optionalvariant的类型组合。

虽然只有完整的示例才能更清楚地显示问题,但最小的起点示例应该是这个:

    [
      {},
      {
        "secondary": false
      },
      {
    
        "secondary": {
          "chance": 10,
          "boosts": {
            "spd": -1
          }
        }
      },
      {
        "secondary": {
          "chance": 30,
          "volatileStatus": "flinch"
        }
      },
      {
        "secondary": {
          "chance": 30
        }
      },
      {
        "secondary": {
          "chance": 10,
          "self": {
            "boosts": {
              "atk": 1,
              "def": 1,
              "spa": 1,
              "spd": 1,
              "spe": 1
            }
          }
        }
      },
      {
        "secondary": {
          "chance": 10,
          "status": "brn"
        }
      },
      {
        "secondary": {
          "chance": 50,
          "self": {
            "boosts": {
              "def": 2
            }
          }
        }
      },
      {
        "secondary": {
          "chance": 100,
          "self": {}
        }
      },
      {
        "secondary": {
          "chance": 50,
          "boosts": {
            "accuracy": -1
          }
        }
      }
    ]

这是我已经拥有的:

struct Boosts {
    optional<int> atk;
    optional<int> def;
};

struct Self {
    optional<Boosts> boosts;
};
struct Secondary {
    optional<int> chance;
    optional<string> volatileStatus;
    optional<Boosts> boosts;
    optional<Self> self;
    optional<string> status;
};

struct move_t {
    int num;
    variant<bool, int> accuracy;
    int basePower;
    string category;
    optional<string> desc = std::nullopt;
    string shortDesc;
    string id;
    string name;
    optional<variant<bool, Secondary>> secondary;

};

【问题讨论】:

    标签: c++ json optional variant


    【解决方案1】:

    我宁愿(ab)使用 'std::monostate' 作为 'std::variant' 的第一个类型 arg,而不是在 'std::variant' 上使用 'std::optional'。如果变体保持单态,则意味着空变体。 顺便说一句,为什么要在 'boost::property_tree' 可用时重新发明轮子?

    https://www.boost.org/doc/libs/1_72_0/doc/html/property_tree.html

    【讨论】:

    • 查看需要解析的完整json,我没有看到其他方式。这种类型使用 scala 和 haskell 工作,不确定它在 cpp 中是否可行
    • 我想,我对你的意图有点误解。但是为 boost 库提供的链接应该没问题。看一看。您可以使用相同的库和类来解析 XML、INI 和鲜为人知的 INFO 格式。
    • 这是一个使用 nlohmann json 的持续尝试:github.com/nlohmann/json/issues/1910
    • 我检查了property_tree,它很适合提供toString() 方法。但我认为它不适合解析 json 并在编译时提供静态类型支持,例如能够在类定义中按字段访问元素,而不是动态访问字段,这可能会导致错误。
    • @Red.Wave 我同意 cpchung 的观点,即 property_tree 不检查引用的键是否实际上在 json 中。如果属性,比如battle 在json 对象j 中,我想使用j.battle 而不是j["battle"] 来引用它
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2017-02-21
    • 1970-01-01
    • 2019-10-11
    • 2021-09-20
    相关资源
    最近更新 更多