Use jsoncpp Library

Source: Internet
Author: User
Tags visual studio 2002

 

Introduction to jsoncpp Library

JSON (JavaScript Object Notation) is a lightweight data interaction format. It can represent integers, real numbers, strings, ordered value sequences, and a set of name/value pairs.

The following is an example of JSON data:

// 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}

}

Features

L reading and writing JSON documents;

L c and C ++ style annotations are associated with elements during parsing;

L rewrite the JSON document to retain original comments.

NOTE: Annotations are usually supported in JSON, but it is best to delete these annotations for portability (C annotations are not supported in Python ). Because annotations are useful in configuration and input files, this feature is retained.

Sample Code
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 configuration\n"
               << 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;
Compile command

The prepared instruction is stored in the readme.txt file under the project root directory.

Jsoncpp is a simple API used to control JSON values, process serialization and deserialization as strings. Jsoncpp uses scons (http://www.scons.org) as the compilation system, which requires installation of the python environment (http://www.python.org ).

You need to download the scons-local release from the URL below:

Http://sourceforge.net/projects/scons/files/scons-local/1.2.0/

Decompress the compressed file to the directory. You can find the README file. Scons. py should be in the directory at the same level as the README file, and then execute the following statement:

# Python scons. py platform = pltfrm [target]

Pltfrm may be one of the following platforms:

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)

Note: If you use vs2008 for compilation, you need to set the environment variables (for example, msvc 2008 command prompt) by running vcvars32.bat before running scons ).

Adding a platform is quite simple. You need to change the sconstruct file.

Target may be check: Compile the library and run the unit test.

Generate a single source file and header file:

Jsoncpp provides a script to generate a single header file and a single source file, which is easily included in an existing project. The merged source code can be generated at any time. Run the following command in the root directory:

# Python amalgamate. py

You may need to specify the header file name. For more information, see the-H option. By default, the following files are generated:

-Dist/jsoncpp. cpp: source files to be added to your project;

-Dist/JSON. h: the header file used in the project. It is equivalent to the JSON/JSON. h file before the source code is merged. This header file only depends on the standard header file

-Dist/JSON/json-forwards.h: Provides forward declarations for all jsoncpp types. It usually needs to be included in the project to accelerate compilation.

 

The permanent link of the latest version in version control is as follows:

Http://jsoncpp.svn.sourceforge.net/viewvc/jsoncpp/trunk/jsoncpp/README.txt? View = Markup

Download

You can download the source code from the SourceForge website:

Http://sourceforge.net/projects/jsoncpp/files/

The latest source code version is available in the project version control repository:

Http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/

Check out the source code, see the following instructions: http://sourceforge.net/scm? Type = SVN and group_id = 144446

Updated content

You can find the latest and latest updates in news.txt under the project root directory.

The permanent link of the latest version of files in version control is:

Http://svn.sourceforge.net/viewcvs.cgi/jsoncpp/README.txt? View = Markup

Project Link

JSON-CPP homepage: http://jsoncpp.sourceforge.net/

JSON-CPP SourceForge project: http://www.sourceforge.net/projects/jsoncpp/

Related Links

L JSON specification and optional language implementation: http://www.json.org/

L yaml a data format designed for human readable: http://www.yaml.org/

L UTF-8 and Unicode FAQ: http://www.cl.cam.ac.uk /~ Mgk25/unicode.html

License

See the license file in the root directory of the project.

Basically, jsoncpp is used in a public domain that is approved and desired by MIT license or under your jurisdiction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.