Search code examples
yamlyaml-cpp

How to determine the built in tag of a node in yaml-cpp?


In yaml-cpp, the function YAML::Node::Tag() seems to only return anything other than "?" when an application-defined tag is used in the YAML source. Surely it should return (for example) "!!map", "!!int" or other built in tags. Is this desired behaviour, and how can I determine, for example, the particular type tag of a scalar node, e.g. null, boolean, integer, float, string etc.? The only approach I can see is to try YAML::Node::as<T>() with each type until an exception is not thrown.


Solution

  • To get the type of a node, call YAML::Node::Type(). The return value is one of:

    YAML::NodeType::Undefined
    YAML::NodeType::Null
    YAML::NodeType::Scalar
    YAML::NodeType::Sequence
    YAML::NodeType::Map
    

    yaml-cpp does not do more specific automatic tag detection, e.g., it will not validate !!int vs. !!float.