使用Boost.PropertyTree处理XML、JSON和INI数据

简介: Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的。目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0。 主要作用/应用场合 Boost.PropertyTree 提供了一种结构化的数据存储容器。

Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的。目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0。


  • 主要作用/应用场合

Boost.PropertyTree 提供了一种结构化的数据存储容器。并且提供了一系列的解释器可以将内存中的结构与具体格式相互转换 (比如: INI, XML, JSON )。

至少可以用在:

  1. 进程间通讯或者跨语言的进程间的通讯
  2. 一些配置文件的存取
  3. 网络通讯协议的格式

  • 基本用法

基本用法有 2 种场景。第一种是从 Property Tree存储到具体格式。第二种是从具体格式解析到具体的 Property Tree。其他还有一些 Property Tree 操作的方法,比如:遍历、搜索等方法。

 

以下这个 Sample 就是基本用法的测试:

先把 数据存储到 datum 中,随后输出 相应的 XML 和 JSON 到 std::cout 上。最后再从 JSON Stream 中解析输入到 ptParse 中获得相应 的数据。

#include <stdio.h>

#include <iostream>
#include <sstream>
#include <string>
#include <locale>

#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/xml_parser.hpp"

int main(int argc, char **argv)
{
    /* The data format
     * <root>
     *  <num>1</num>
     *  <str>Test</str>
     * </root>
     */
    try
    {
        /* create the property tree */
        boost::property_tree::ptree datum;
        datum.put("root.num", 100);
        datum.put("root.str", "string");

        /* output XML string */
        std::ostringstream xmlOutputStream;
        boost::property_tree::xml_parser::write_xml(xmlOutputStream,
            datum);
        std::cout << "XML format:" << std::endl;
        std::cout << xmlOutputStream.str() << std::endl;

        /* output JSON string */
        std::ostringstream jsonOutputStream;
        boost::property_tree::json_parser::write_json(jsonOutputStream,
            datum);
        std::cout << "JSON format:" << std::endl;
        std::cout << jsonOutputStream.str() << std::endl;

        /* read datum from JSON stream */
        boost::property_tree::ptree ptParse;
        std::istringstream jsonIStream;
        jsonIStream.str(jsonOutputStream.str());
        boost::property_tree::json_parser::read_json(jsonIStream,
            ptParse);
        int num = ptParse.get<int>("root.num");
        std::string strVal = ptParse.get<std::string>("root.str");
        std::cout << "Num=" << std::dec << num
            << " Str=" << strVal << std::endl << std::endl;
    }
    catch (...)
    {
        printf("create boost::property_tree::ptree failed\n");
    }

    return 0;
}

  • 关于字符集

Boost 目前是支持 UTF8 的,但是不能用 直接用 Unicode。所以,如果要存储宽字符就有点麻烦需要用到 Boost 提供的 utf8_codecvt_facet 做转换。

下面就是一个存储 wchar_t 的 Sample:

和之前的其实差不多,有 2 点主要不同。一是用了 wptree 替换了 ptree。二是增加了 utf8_codecvt_facet 在相应的 Stream 里做转换。

 
#include <stdio.h>

#include <iostream>
#include <sstream>
#include <string>
#include <locale>

#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/xml_parser.hpp"

#include "boost/program_options/detail/convert.hpp"
#include "boost/program_options/detail/utf8_codecvt_facet.hpp"

int main(int argc, char **argv)
{
    /* The data format
     * <root>
     *  <num>1</num>
     *  <str>Test</str>
     * </root>
     */
    /* test UTF-8 format */
    try
    {
        /* create boost utf8 codecvt */
        std::locale oldLocale;
        std::locale utf8Locale(oldLocale,
            new boost::program_options::detail::utf8_codecvt_facet());
        std::wcout.imbue(utf8Locale);

        /* create the wptree for save the UTF-8 data */
        boost::property_tree::wptree datum;
        datum.put(L"root.num", 100);
        datum.put(L"root.str", L"wstring");

        /* output XML string */
        std::wostringstream xmlOutputStream;
        xmlOutputStream.imbue(utf8Locale);
        boost::property_tree::xml_parser::write_xml(xmlOutputStream,
            datum);
        std::wcout << L"XML format:" << std::endl;
        std::wcout << xmlOutputStream.str() << std::endl;

        /* output JSON string */
        std::wostringstream jsonOutputStream;
        jsonOutputStream.imbue(utf8Locale);
        boost::property_tree::json_parser::write_json(jsonOutputStream,
            datum);
        std::wcout << L"JSON format:" << std::endl;
        std::wcout << jsonOutputStream.str() << std::endl;

        /* read datum from JSON stream */
        boost::property_tree::wptree wptParse;
        std::wistringstream jsonIStream;
        jsonIStream.imbue(utf8Locale);
        jsonIStream.str(jsonOutputStream.str());
        boost::property_tree::json_parser::read_json(jsonIStream,
            wptParse);
        int num = wptParse.get<int>(L"root.num");
        std::wstring wstrVal = wptParse.get<std::wstring>(L"root.str");
        std::wcout << L"Num=" << std::dec << num
            << L" Str=" << wstrVal << std::endl << std::endl;
    }
    catch (...)
    {
        printf("create boost::property_tree::wptree failed\n");
    }

    return 0;
}

  • 附录
  1. 以上的测试程序,在 Boost 1.42.0 和 MS VS 2008 上测试过。这里是打包文件 PTreeTest
  2. 在 Boot.org 上能找到更多的 PropertyTree 的操作。PropertyTree 在 Boost 1.41.0 版本的手册。最好去看新版本的如果以后更新的话。
  3. Boot.PropertyTree 用的 XML 解析器是 RapidXML 。是一个基于模板设计的 XML 操作库 ,有非常好的性能(据说)。

原文地址:http://notes.xj-labs.net/?p=52

目录
相关文章
|
27天前
|
JSON 前端开发 Java
Json格式数据解析
Json格式数据解析
|
3天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
11 0
|
5天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
11 1
|
5天前
|
JSON 数据格式 Python
Python处理JSON数据
【4月更文挑战第30天】该内容介绍了Python处理JSON数据的三个方法:1)使用`json.loads()`尝试解析字符串以验证其是否为有效JSON,通过捕获`JSONDecodeError`异常判断有效性;2)通过`json.dumps()`的`indent`参数格式化输出JSON数据,使其更易读;3)处理JSON中的日期,利用`dateutil`库将日期转换为字符串进行序列化和反序列化。
15 4
|
6天前
|
XML JSON 前端开发
【Web 前端】XML和JSON的区别?
【4月更文挑战第22天】【Web 前端】XML和JSON的区别?
【Web 前端】XML和JSON的区别?
|
8天前
|
存储 JSON 数据处理
|
9天前
|
XML JSON 中间件
中间件数据格式JSON与XML之间的转换
中间件数据格式JSON与XML之间的转换
22 3
|
10天前
|
JSON 数据可视化 定位技术
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
16 0
|
18天前
|
JSON JavaScript 数据格式
vue展示json数据,vue-json-viewer的使用
vue展示json数据,vue-json-viewer的使用
25 0
|
23天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
33 0