1 #ifndef JSON_SPIRIT_VALUE
\r
2 #define JSON_SPIRIT_VALUE
\r
4 // Copyright John W. Wilkinson 2007 - 2011
\r
5 // Distributed under the MIT License, see accompanying file LICENSE.txt
\r
7 // json spirit version 4.05
\r
9 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
\r
17 #include <stdexcept>
\r
18 #include <boost/config.hpp>
\r
19 #include <boost/cstdint.hpp>
\r
20 #include <boost/shared_ptr.hpp>
\r
21 #include <boost/variant.hpp>
\r
23 // comment out the value types you don't need to reduce build times and intermediate file sizes
\r
24 #define JSON_SPIRIT_VALUE_ENABLED
\r
25 //#define JSON_SPIRIT_WVALUE_ENABLED
\r
26 #define JSON_SPIRIT_MVALUE_ENABLED
\r
27 //#define JSON_SPIRIT_WMVALUE_ENABLED
\r
29 namespace json_spirit
\r
31 enum Value_type{ obj_type, array_type, str_type, bool_type, int_type, real_type, null_type };
\r
35 template< class Config > // Config determines whether the value uses std::string or std::wstring and
\r
36 // whether JSON Objects are represented as vectors or maps
\r
41 typedef Config Config_type;
\r
42 typedef typename Config::String_type String_type;
\r
43 typedef typename Config::Object_type Object;
\r
44 typedef typename Config::Array_type Array;
\r
45 typedef typename String_type::const_pointer Const_str_ptr; // eg const char*
\r
47 Value_impl(); // creates null value
\r
48 Value_impl( Const_str_ptr value );
\r
49 Value_impl( const String_type& value );
\r
50 Value_impl( const Object& value );
\r
51 Value_impl( const Array& value );
\r
52 Value_impl( bool value );
\r
53 Value_impl( int value );
\r
54 Value_impl( boost::int64_t value );
\r
55 Value_impl( boost::uint64_t value );
\r
56 Value_impl( double value );
\r
58 template< class Iter >
\r
59 Value_impl( Iter first, Iter last ); // constructor from containers, e.g. std::vector or std::list
\r
61 template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >
\r
62 Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant ); // constructor for compatible variant types
\r
64 Value_impl( const Value_impl& other );
\r
66 bool operator==( const Value_impl& lhs ) const;
\r
68 Value_impl& operator=( const Value_impl& lhs );
\r
70 Value_type type() const;
\r
72 bool is_uint64() const;
\r
73 bool is_null() const;
\r
75 const String_type& get_str() const;
\r
76 const Object& get_obj() const;
\r
77 const Array& get_array() const;
\r
78 bool get_bool() const;
\r
79 int get_int() const;
\r
80 boost::int64_t get_int64() const;
\r
81 boost::uint64_t get_uint64() const;
\r
82 double get_real() const;
\r
87 template< typename T > T get_value() const; // example usage: int i = value.get_value< int >();
\r
88 // or double d = value.get_value< double >();
\r
90 static const Value_impl null;
\r
94 void check_type( const Value_type vtype ) const;
\r
96 typedef boost::variant< boost::recursive_wrapper< Object >, boost::recursive_wrapper< Array >,
\r
97 String_type, bool, boost::int64_t, double, Null, boost::uint64_t > Variant;
\r
101 class Variant_converter_visitor : public boost::static_visitor< Variant >
\r
105 template< typename T, typename A, template< typename, typename > class Cont >
\r
106 Variant operator()( const Cont< T, A >& cont ) const
\r
108 return Array( cont.begin(), cont.end() );
\r
111 Variant operator()( int i ) const
\r
113 return static_cast< boost::int64_t >( i );
\r
117 Variant operator()( const T& t ) const
\r
126 template< class Config >
\r
129 typedef typename Config::String_type String_type;
\r
130 typedef typename Config::Value_type Value_type;
\r
136 Pair_impl( const String_type& name, const Value_type& value );
\r
138 bool operator==( const Pair_impl& lhs ) const;
\r
144 #if defined( JSON_SPIRIT_VALUE_ENABLED ) || defined( JSON_SPIRIT_WVALUE_ENABLED )
\r
145 template< class String >
\r
146 struct Config_vector
\r
148 typedef String String_type;
\r
149 typedef Value_impl< Config_vector > Value_type;
\r
150 typedef Pair_impl < Config_vector > Pair_type;
\r
151 typedef std::vector< Value_type > Array_type;
\r
152 typedef std::vector< Pair_type > Object_type;
\r
154 static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )
\r
156 obj.push_back( Pair_type( name , value ) );
\r
158 return obj.back().value_;
\r
161 static String_type get_name( const Pair_type& pair )
\r
166 static Value_type get_value( const Pair_type& pair )
\r
168 return pair.value_;
\r
173 // typedefs for ASCII
\r
175 #ifdef JSON_SPIRIT_VALUE_ENABLED
\r
176 typedef Config_vector< std::string > Config;
\r
178 typedef Config::Value_type Value;
\r
179 typedef Config::Pair_type Pair;
\r
180 typedef Config::Object_type Object;
\r
181 typedef Config::Array_type Array;
\r
184 // typedefs for Unicode
\r
186 #if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
\r
187 typedef Config_vector< std::wstring > wConfig;
\r
189 typedef wConfig::Value_type wValue;
\r
190 typedef wConfig::Pair_type wPair;
\r
191 typedef wConfig::Object_type wObject;
\r
192 typedef wConfig::Array_type wArray;
\r
197 #if defined( JSON_SPIRIT_MVALUE_ENABLED ) || defined( JSON_SPIRIT_WMVALUE_ENABLED )
\r
198 template< class String >
\r
201 typedef String String_type;
\r
202 typedef Value_impl< Config_map > Value_type;
\r
203 typedef std::vector< Value_type > Array_type;
\r
204 typedef std::map< String_type, Value_type > Object_type;
\r
205 typedef std::pair< String_type, Value_type > Pair_type;
\r
207 static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )
\r
209 return obj[ name ] = value;
\r
212 static String_type get_name( const Pair_type& pair )
\r
217 static Value_type get_value( const Pair_type& pair )
\r
219 return pair.second;
\r
224 // typedefs for ASCII
\r
226 #ifdef JSON_SPIRIT_MVALUE_ENABLED
\r
227 typedef Config_map< std::string > mConfig;
\r
229 typedef mConfig::Value_type mValue;
\r
230 typedef mConfig::Object_type mObject;
\r
231 typedef mConfig::Array_type mArray;
\r
234 // typedefs for Unicode
\r
236 #if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
\r
237 typedef Config_map< std::wstring > wmConfig;
\r
239 typedef wmConfig::Value_type wmValue;
\r
240 typedef wmConfig::Object_type wmObject;
\r
241 typedef wmConfig::Array_type wmArray;
\r
244 ///////////////////////////////////////////////////////////////////////////////////////////////
\r
248 inline bool operator==( const Null&, const Null& )
\r
253 template< class Config >
\r
254 const Value_impl< Config > Value_impl< Config >::null;
\r
256 template< class Config >
\r
257 Value_impl< Config >::Value_impl()
\r
262 template< class Config >
\r
263 Value_impl< Config >::Value_impl( const Const_str_ptr value )
\r
264 : v_( String_type( value ) )
\r
268 template< class Config >
\r
269 Value_impl< Config >::Value_impl( const String_type& value )
\r
274 template< class Config >
\r
275 Value_impl< Config >::Value_impl( const Object& value )
\r
280 template< class Config >
\r
281 Value_impl< Config >::Value_impl( const Array& value )
\r
286 template< class Config >
\r
287 Value_impl< Config >::Value_impl( bool value )
\r
292 template< class Config >
\r
293 Value_impl< Config >::Value_impl( int value )
\r
294 : v_( static_cast< boost::int64_t >( value ) )
\r
298 template< class Config >
\r
299 Value_impl< Config >::Value_impl( boost::int64_t value )
\r
304 template< class Config >
\r
305 Value_impl< Config >::Value_impl( boost::uint64_t value )
\r
310 template< class Config >
\r
311 Value_impl< Config >::Value_impl( double value )
\r
316 template< class Config >
\r
317 Value_impl< Config >::Value_impl( const Value_impl< Config >& other )
\r
322 template< class Config >
\r
323 template< class Iter >
\r
324 Value_impl< Config >::Value_impl( Iter first, Iter last )
\r
325 : v_( Array( first, last ) )
\r
329 template< class Config >
\r
330 template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >
\r
331 Value_impl< Config >::Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant )
\r
332 : v_( boost::apply_visitor( Variant_converter_visitor(), variant) )
\r
336 template< class Config >
\r
337 Value_impl< Config >& Value_impl< Config >::operator=( const Value_impl& lhs )
\r
339 Value_impl tmp( lhs );
\r
341 std::swap( v_, tmp.v_ );
\r
346 template< class Config >
\r
347 bool Value_impl< Config >::operator==( const Value_impl& lhs ) const
\r
349 if( this == &lhs ) return true;
\r
351 if( type() != lhs.type() ) return false;
\r
353 return v_ == lhs.v_;
\r
356 template< class Config >
\r
357 Value_type Value_impl< Config >::type() const
\r
364 return static_cast< Value_type >( v_.which() );
\r
367 template< class Config >
\r
368 bool Value_impl< Config >::is_uint64() const
\r
370 return v_.which() == null_type + 1;
\r
373 template< class Config >
\r
374 bool Value_impl< Config >::is_null() const
\r
376 return type() == null_type;
\r
379 template< class Config >
\r
380 void Value_impl< Config >::check_type( const Value_type vtype ) const
\r
382 if( type() != vtype )
\r
384 std::ostringstream os;
\r
386 os << "value type is " << type() << " not " << vtype;
\r
388 throw std::runtime_error( os.str() );
\r
392 template< class Config >
\r
393 const typename Config::String_type& Value_impl< Config >::get_str() const
\r
395 check_type( str_type );
\r
397 return *boost::get< String_type >( &v_ );
\r
400 template< class Config >
\r
401 const typename Value_impl< Config >::Object& Value_impl< Config >::get_obj() const
\r
403 check_type( obj_type );
\r
405 return *boost::get< Object >( &v_ );
\r
408 template< class Config >
\r
409 const typename Value_impl< Config >::Array& Value_impl< Config >::get_array() const
\r
411 check_type( array_type );
\r
413 return *boost::get< Array >( &v_ );
\r
416 template< class Config >
\r
417 bool Value_impl< Config >::get_bool() const
\r
419 check_type( bool_type );
\r
421 return boost::get< bool >( v_ );
\r
424 template< class Config >
\r
425 int Value_impl< Config >::get_int() const
\r
427 check_type( int_type );
\r
429 return static_cast< int >( get_int64() );
\r
432 template< class Config >
\r
433 boost::int64_t Value_impl< Config >::get_int64() const
\r
435 check_type( int_type );
\r
439 return static_cast< boost::int64_t >( get_uint64() );
\r
442 return boost::get< boost::int64_t >( v_ );
\r
445 template< class Config >
\r
446 boost::uint64_t Value_impl< Config >::get_uint64() const
\r
448 check_type( int_type );
\r
452 return static_cast< boost::uint64_t >( get_int64() );
\r
455 return boost::get< boost::uint64_t >( v_ );
\r
458 template< class Config >
\r
459 double Value_impl< Config >::get_real() const
\r
461 if( type() == int_type )
\r
463 return is_uint64() ? static_cast< double >( get_uint64() )
\r
464 : static_cast< double >( get_int64() );
\r
467 check_type( real_type );
\r
469 return boost::get< double >( v_ );
\r
472 template< class Config >
\r
473 typename Value_impl< Config >::Object& Value_impl< Config >::get_obj()
\r
475 check_type( obj_type );
\r
477 return *boost::get< Object >( &v_ );
\r
480 template< class Config >
\r
481 typename Value_impl< Config >::Array& Value_impl< Config >::get_array()
\r
483 check_type( array_type );
\r
485 return *boost::get< Array >( &v_ );
\r
488 template< class Config >
\r
489 Pair_impl< Config >::Pair_impl( const String_type& name, const Value_type& value )
\r
495 template< class Config >
\r
496 bool Pair_impl< Config >::operator==( const Pair_impl< Config >& lhs ) const
\r
498 if( this == &lhs ) return true;
\r
500 return ( name_ == lhs.name_ ) && ( value_ == lhs.value_ );
\r
503 // converts a C string, ie. 8 bit char array, to a string object
\r
505 template < class String_type >
\r
506 String_type to_str( const char* c_str )
\r
508 String_type result;
\r
510 for( const char* p = c_str; *p != 0; ++p )
\r
520 namespace internal_
\r
522 template< typename T >
\r
523 struct Type_to_type
\r
527 template< class Value >
\r
528 int get_value( const Value& value, Type_to_type< int > )
\r
530 return value.get_int();
\r
533 template< class Value >
\r
534 boost::int64_t get_value( const Value& value, Type_to_type< boost::int64_t > )
\r
536 return value.get_int64();
\r
539 template< class Value >
\r
540 boost::uint64_t get_value( const Value& value, Type_to_type< boost::uint64_t > )
\r
542 return value.get_uint64();
\r
545 template< class Value >
\r
546 double get_value( const Value& value, Type_to_type< double > )
\r
548 return value.get_real();
\r
551 template< class Value >
\r
552 typename Value::String_type get_value( const Value& value, Type_to_type< typename Value::String_type > )
\r
554 return value.get_str();
\r
557 template< class Value >
\r
558 typename Value::Array get_value( const Value& value, Type_to_type< typename Value::Array > )
\r
560 return value.get_array();
\r
563 template< class Value >
\r
564 typename Value::Object get_value( const Value& value, Type_to_type< typename Value::Object > )
\r
566 return value.get_obj();
\r
569 template< class Value >
\r
570 bool get_value( const Value& value, Type_to_type< bool > )
\r
572 return value.get_bool();
\r
576 template< class Config >
\r
577 template< typename T >
\r
578 T Value_impl< Config >::get_value() const
\r
580 return internal_::get_value( *this, internal_::Type_to_type< T >() );
\r