且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JsonCpp Documentation

更新时间:2022-08-12 21:16:19

                   JsonCpp Documentation
                       0.6.0-rc2
说明:
  1. 本文内容来自:http://jsoncpp.sourceforge.net/old.html
  2. 这是JsonCpp Documentation使用说明文档;
  3. 内容基本包括了JSON的基本操作。

 

一、Introduction
  JSON (JavaScript Object Notation) is a lightweight data-interchange format. It can represent integer, real number, string, an ordered sequence of value, and a collection of name/value pairs.
  JSON是一种一种轻量级的数据交换格式,它可以表示整数、实数、字符串值的有序序列、名称/值对的集合。

  Here is an example of JSON data:
  下面是一个JSON数据的例子

    // Configuration options
    // 配置选项
    {
        // Default encoding for text
        // 文本的默认编码
        "encoding" : "UTF-8",
        
        // Plug-ins loaded at start-up
        // 启动时加载的插件
        "plug-ins" : [      // 这里是一个数组
            "python",
            "c++",
            "ruby"
            ],
            
        // Tab indent size
        // Tab缩进字节
        "indent" : { "length" : 3, "use_space": true }
    }

二、Features

  1. read and write JSON document
    读写JSON文件
  2. attach C and C++ style comments to element during parsing
    在解析的数据中允许存在C、C++注释
  3. rewrite JSON document preserving original comments
    重写JSON文档保存原始评论

  Notes: Comments used to be supported in JSON but where removed for portability (C like comments are not supported in Python). Since comments are useful in configuration/input file, this feature was preserved.
  说明:注释用于支持JSON,但删除可移植性(C不支持评论在Python)。因为评论是有用的配置/输入文件,这个功能被保留

三、Code example

    Json::Value root;   // will contains the root value after parsing.
                        // root将保存解析数据的根节点
    Json::Reader reader;
    bool parsingSuccessful = reader.parse( config_doc, root );
    if ( !parsingSuccessful )   // 解析是否正确
    {
        // report to the user the failure and their locations in the document.
        std::cout  << "Failed to parse configuration\n"
                   << reader.getFormattedErrorMessages();
        return;
    }

    // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
    // such member.
    // 获取root中名称叫"encoding"成员的值,如果该成员不存在,那么就返回'UTF-8' 
    // 这里演示的是get的使用方法
    std::string encoding = root.get("encoding", "UTF-8" ).asString();

    // Get the value of the member of root named 'plug-ins', return a 'null' value if
    // there is no such member.
    // 获取root中名称叫"plug-ins"成员的值,如果该成员不存在,那么就返回'null' 
    // 这里演示的数组的使用方法
    const Json::Value plugins = root["plug-ins"];
    for ( int index = 0; index < plugins.size(); ++index )  // Iterates over the sequence elements.
       loadPlugIn( plugins[index].asString() );             // 迭代元素
       
    // 这里演示的是Json对象中的Json对象的使用方法
    setIndentLength( root["indent"].get("length", 3).asInt() );
    setIndentUseSpace( root["indent"].get("use_space", true).asBool() );

    // ...
    // At application shutdown to make the new configuration document:
    // Since Json::Value has implicit constructor for all value types, it is not
    // necessary to explicitly construct the Json::Value object:
    // 在应用程序关闭时,使新的配置文档
    // Json::Value对于所有的值类型有隐式构造函数,不需要显式构造Json::Value对象
    root["encoding"] = getCurrentEncoding();
    root["indent"]["length"] = getCurrentIndentLength();
    root["indent"]["use_space"] = getCurrentIndentUseSpace();

    Json::StyledWriter writer;
    // Make a new JSON document for the configuration. Preserve original comments.
    // 生成一个新的JSON配置文档,保留原来的评论
    // 这里保留原来的注释,个人感觉貌似是因为这里是直接使用root根节点,如果换了一个
    // Json::Value对象,应该就没有注释了。
    std::string outputConfig = writer.write( root );

    // You can also use streams.  This will put the contents of any JSON
    // stream at a particular sub-value, if you'd like.
    // 直接从terminal获取值,可以任何的JSON值
    std::cin >> root["subtree"];

    // And you can write to a stream, using the StyledWriter automatically.
    // 最后使用StyledWriter作为输出流,写出Json对象到terminal
    std::cout << root;