yaml学习,yaml-cpp学习,YamlConfigReader学习

yaml是什么

什么是 YAML?

  1. YAML 是一种人类可读的数据序列化语言,通常用于编写配置文件。
  2. YAML 也是 JSON 的超集,所以 JSON 文件在 YAML 中有效。
  3. 其使用 Python 风格的缩进来确定结构并表示嵌套。为了保持跨系统的可移植性,设计时不允许使用制表符,因此改用空格(字面意义的空格字符)。
  4. 注释可以用井号或哈希符号(#)。YAML 不支持多行注释,每行都需要以井号字符为后缀。
  5. 请注意,YAML 文件的结构是映射或列表,遵循层次结构,具体取决于缩进以及定义键值的方式。它以映射来关联键值对。每个键必须是唯一的,而且顺序并不重要。

YAML 入门教程

  1. 大小写敏感
  2. YAML 支持以下几种数据类型:
    对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
    数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
    纯量(scalars):单个的、不可再分的值
  3. 复合结构
languages:
  - Ruby
  - Perl
  - Python 
websites:
  YAML: yaml.org 
  Ruby: ruby-lang.org 
  Python: python.org 
  Perl: use.perl.org

转换为 json 为:

{ 
  languages: [ 'Ruby', 'Perl', 'Python'],
  websites: {
    YAML: 'yaml.org',
    Ruby: 'ruby-lang.org',
    Python: 'python.org',
    Perl: 'use.perl.org' 
  } 
}

yaml-cpp

30分钟学会Yaml-cpp 0.6.0
Node是yaml-cpp中最重要的数据结构。Node一共有以下几种type,对应上面的数据类型:
- Null 空节点
- Sequence 序列,类似于一个Vector,对应YAML格式中的数组
- Map 类似标准库中的Map,对应YAML格式中的对象
- Scalar 标量,对应YAML格式中的常量

对rviz中的yaml解析学习

void YamlConfigReader::readStream(Config& config, std::istream& in, const QString& /*filename*/)
{
  try
  {
    YAML::Node yaml_node;
    yaml_node = YAML::Load(in);
    error_ = false;
    message_ = "";
    readYamlNode(config, yaml_node);
  }
  catch (YAML::ParserException& ex)
  {
    message_ = ex.what();
    error_ = true;
  }
}

void YamlConfigReader::readYamlNode(Config& config, const YAML::Node& yaml_node)
{
  switch (yaml_node.Type())
  {
  case YAML::NodeType::Map:
  {
    for (YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it)
    {
      std::string key;
      //将第一个键值对的键转换为std::string提取出来
      key = it->first.as<std::string>();
      //根据提取出的键,构建config键值对,目前只有键
      Config child = config.mapMakeChild(QString::fromStdString(key));
      //遍历
      readYamlNode(child, it->second);
    }
    break;
  }
  case YAML::NodeType::Sequence:
  {
    for (YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it)
    {
      Config child = config.listAppendNew();
      //解析数组
      readYamlNode(child, *it);
    }
    break;
  }
  case YAML::NodeType::Scalar:
  {
    std::string s;
    s = yaml_node.as<std::string>();
    //设置构建的config键值对的值
    config.setValue(QString::fromStdString(s));
    break;
  }
  case YAML::NodeType::Null:
  default:
    break;
  }
}

打印rviz::config

rviz::config是根据YamlConfigReader::readYamlNode解析得到的结果构建的

void printConfig(const rviz::Config& config, int depth = 0) {
    if (config.getType() == rviz::Config::Map) {
        rviz::Config::MapIterator iter = config.mapIterator();
        while (iter.isValid()) {
            for (int i = 0; i < depth; i++) {
                std::cout << "  "; // 缩进
            }
            std::cout << "Key: " << iter.currentKey().toStdString() << std::endl;

            // 递归遍历子节点
            printConfig(iter.currentChild(), depth + 1);
            iter.advance();
        }
    } else if (config.getType() == rviz::Config::List) {
        int numChildren = config.listLength();
        for (int i = 0; i < numChildren; i++) {
            for (int j = 0; j < depth; j++) {
                std::cout << "  "; // 缩进
            }
            std::cout << "Item " << i << ":" << std::endl;

            // 递归遍历子节点
            printConfig(config.listChildAt(i), depth + 1);
        }
    } else if (config.getType() == rviz::Config::Value) {
        for (int i = 0; i < depth; i++) {
            std::cout << "  "; // 缩进
        }
        std::cout << "Value: " << config.getValue().toString().toStdString() << std::endl;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值