|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionJSON is a text file format similar to XML, but less verbose. It has been called "XML lite". This article describes JSON Spirit, a C++ library that reads and writes JSON files or streams. It is written using the Boost Spirit parser generator. If you are already using Boost, you can use JSON Spirit without any additional dependencies. The JSON Spirit source code is available as a Microsoft Visual Studio C++ 2005 "solution". However, it should compile and work on any platform compatible with Boost. JSON Spirit has been built and tested with Microsoft Visual Studio C++ 2003, 2005, 2008, and g++ version 4.2.3 on Linux. It has been tested on Windows with Boost versions 1.33.1, 1.34.0 and 1.34.1. The Microsoft Visual Studio C++ solution consists of three projects:
New: Supports Unicode from version 2.0. Using the CodeThe Microsoft Visual Studio solution builds an object library. You can link to this object library or, alternatively, you can add the JSON Spirit source files directly to your project. All JSON Spirit declarations are in the namespace Reading JSONYou can read JSON data from a bool read( const std::string& s, Value& value );
bool read( const std::wstring& s, wValue& value );
bool read( std::istream& is, Value& value );
bool read( std::wistream& is, wValue& value );
For example: ifstream is( "json.txt" );
Value value;
read( is, value );
A JSON value can hold either a JSON enum Value_type{ obj_type, array_type, str_type,
bool_type, int_type, real_type, null_type };
class Value
{
public:
Value(); // creates null value
Value( const char* value );
Value( const std::string& value );
Value( const Object& value );
Value( const Array& value );
Value( bool value );
Value( int value );
Value( double value );
bool operator==( const Value& lhs ) const;
Value_type type() const;
const std::string& get_str() const;
const Object& get_obj() const;
const Array& get_array() const;
bool get_bool() const;
int get_int() const;
double get_real() const;
Object& get_obj();
Array& get_array();
template< typename > T get_value() const;
static const Value null;
private:
...
};
You obtain the The template getter function int i = value_1.get_value< int >();
double d = value_2.get_value< double >();
A top level typedef std::vector< Pair > Object;
typedef std::vector< Value > Array;
A struct Pair
{
Pair( const std::string& name, const Value& value );
bool operator==( const Pair& lhs ) const;
std::string name_;
Value value_;
};
JSON arrays and objects can themselves contain other arrays or objects forming a tree. Writing JSONTo output JSON, first create a void write ( const Value& value, std::ostream& os );
void write_formatted( const Value& value, std::ostream& os );
std::string write ( const Value& value );
std::string write_formatted( const Value& value );
std::wstring write ( const wValue& value );
std::wstring write_formatted( const wValue& value );
void write ( const wValue& value, std::wostream& os );
void write_formatted( const wValue& value, std::wostream& os );
The following example shows how to create a small JSON file containing an object with three members: Object addr_obj;
addr_obj.push_back( Pair( "house_number", 42 ) );
addr_obj.push_back( Pair( "road", "East Street" ) );
addr_obj.push_back( Pair( "town", "Newtown" ) );
ofstream os( "address.txt" );
write_formatted( addr_obj, os );
os.close();
The object {
"house_number" : 42,
"road" : "East Street",
"town" : "Newtown"
}
Unicode SupportUnicode support is provided by a typedef Value_impl< std::string > Value;
typedef Value_impl< std::wstring > wValue;
There are also Include FilesTo read JSON files, you will need to include json_spirit_reader.h and json_spirit_value.h. To generate JSON files, you will need to include json_spirit_writer.h and json_spirit_value.h. Alternatively, you can include json_spirit.h, which includes all three of the above. Using JSON Spirit with Multiple ThreadsIf you intend to use JSON Spirit in more than one thread you will need to uncomment the following line near the top of json_spirit_reader.cpp. //#define BOOST_SPIRIT_THREADSAFE
In this case, Boost Spirit will require you to link against Boost Threads. History
| ||||||||||||||||||||