JsonCpp库使用
Jsoncpp库使用简介JSON(JavaScript Object Notation)是一个轻量级的数据交互格式。它可以表示整数、实数、字符串、有序的值序列和名称/值对的集合。 下面是JSON数据的一个示例: // Configuration options { // Default encoding for text "encoding" : "UTF-8",
// Plug-ins loaded at start-up "plug-ins" : [ "python", "c++", "ruby" ],
// Tab indent size "indent" : { "length" : 3,"use_space": true } } 特征l 读取和写入JSON文档; l 在解析期间关联C和C++风格的注释到元素; l 重写JSON文档保留原始的注释。 注:JSON中通常是支持注释的,但是为了可移植性最好删除这些注释(C注释在Python中不支持)。因为注释在配置和输入文件中是有用的,所以保留了这个特征。 代码示例Json::Value root; // will contains the root value after parsing. 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 configurationn" << reader.getFormattedErrorMessages(); return; } // Get the value of the member of root named 'encoding',return 'UTF-8' if there is no // such member. std::string encoding = root.get("encoding","UTF-8" ).asString(); // Get the value of the member of root named 'encoding',return a 'null' value if // there is no such member. const Json::Value plugins = root["plug-ins"]; for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the sequence elements. loadPlugIn( plugins[index].asString() ); 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: 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. 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. std::cin >> root["subtree"]; // And you can write to a stream,using the StyledWriter automatically. std::cout << root; 编译指令编译指令位于项目根目录下的README.txt文件中。 JsonCpp是一个简单的API,用于控制JSON的值,处理序列化和反序列化为字符串。JsonCpp使用Scons(http://www.scons.org)作为编译系统,它要求安装python环境(http://www.python.org)。 你需要从下面的URL中下载scons-local发行版: http://sourceforge.net/projects/scons/files/scons-local/1.2.0/ 解压缩该压缩文件到目录中,你可以找到README文件。Scons.py应该在与README文件同一级的目录下,然后执行下面的语句: # python scons.py platform=PLTFRM [TARGET] 其中 PLTFRM可能是下面的一个平台: suncc Sun C++ (Solaris) vacpp Visual Age C++ (AIX) mingw msvc6 Microsoft Visual Studio 6 service pack 5-6 msvc70 Microsoft Visual Studio 2002 msvc71 Microsoft Visual Studio 2003 msvc80 Microsoft Visual Studio 2005 msvc90 Microsoft Visual Studio 2008 linux-gcc Gnu C++ (linux,also reported to work for Mac OS X) 注意:如果你使用VS2008进行编译,在运行scons前你需要通过运行vcvars32.bat设置环境变量(例如:MSVC 2008命令提示符)。 增加平台相当的简单,你需要改变Sconstruct文件来实现。 TARGET可能是check: 编译库并且运行单元测试。 产生单个源文件和头文件: JsonCpp提供一个脚本来产生单个的头文件和单个源文件,这样容易包含到现有的项目中。合并的源代码可以在任何时候生成,在根目录下运行下面的命令: # python amalgamate.py 他可能需要指定头文件的名称,参见-h选项。缺省的情况下,产生下面的这些文件: - dist/jsoncpp.cpp:需要添加到你项目中的源文件; - dist/json/json.h:在项目中使用的头文件,他等价于没有合并源代码之前的json/json.h。该头文件只依赖于标准头文件 - dist/json/json-forwards.h:提供所有JsonCpp类型的向前声明。通常它需要包括在项目中以便加速编译。
版本控制中的最新版本文件的永久链接如下: http://jsoncpp.svn.sourceforge.net/viewvc/jsoncpp/trunk/jsoncpp/README.txt?view=markup 下载从SourceForge网站上可以下载到源代码,网址为: http://sourceforge.net/projects/jsoncpp/files/ 在项目的版本控制仓库中有最新的源代码版本可用,网址为: http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/ 检出源代码,参见下面的说明文档:http://sourceforge.net/scm/?type=svn&group_id=144446 更新的内容最新更改的描述可以在项目根目录下的NEWS.txt中找到。 在版本控制中最新版本的文件的永久链接为: http://svn.sourceforge.net/viewcvs.cgi/jsoncpp/README.txt?view=markup 项目链接Json-cpp主页:http://jsoncpp.sourceforge.net/ Json-cpp Sourceforge项目:http://www.sourceforge.net/projects/jsoncpp/ 相关链接l JSON说明书和可选的语言实现:http://www.json.org/ l YAML一种为人类可读而设计的数据格式:http://www.yaml.org/ l UTF-8和Unicode FAQ:http://www.cl.cam.ac.uk/~mgk25/unicode.html 许可参见项目根目录下的LICENSE文件 基本上,JsonCpp在MIT许可或者在你的司法权下认可和希望的公共领域下使用。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |