22 #define JSONXX_MAJOR "0" 23 #define JSONXX_MINOR "22" 24 #define JSONXX_EXTRA "a" 25 #define JSONXX_VERSION JSONXX_MAJOR "." JSONXX_MINOR "-" JSONXX_EXTRA 26 #define JSONXX_XML_TAG "<!-- generated by jsonxx " JSONXX_VERSION " -->" 28 #if __cplusplus > 199711L 29 #define JSONXX_COMPILER_HAS_CXX11 1 30 #elif defined(_MSC_VER) && _MSC_VER > 1700 31 #define JSONXX_COMPILER_HAS_CXX11 1 33 #define JSONXX_COMPILER_HAS_CXX11 0 38 #define JSONXX_ASSERT(...) \ 40 __pragma(warning(push)) __pragma(warning(disable:4127)) \ 41 if( jsonxx::Assertions ) \ 42 __pragma(warning(pop)) \ 43 jsonxx::assertion(__FILE__,__LINE__,#__VA_ARGS__,bool(__VA_ARGS__)); \ 44 __pragma(warning(push)) __pragma(warning(disable:4127)) \ 46 __pragma(warning(pop)) 48 #define JSONXX_ASSERT(...) do { if( jsonxx::Assertions ) \ 49 jsonxx::assertion(__FILE__,__LINE__,#__VA_ARGS__,bool(__VA_ARGS__)); } while(0) 70 #pragma warning(disable:4127) 104 bool validate(
const std::string &input );
105 bool validate( std::istream &input );
106 std::string
reformat(
const std::string &input );
107 std::string
reformat( std::istream &input );
108 std::string
xml(
const std::string &input,
unsigned format =
JSONx );
109 std::string
xml( std::istream &input,
unsigned format =
JSONx );
112 void assertion(
const char *file,
int line,
const char *expression,
bool result );
120 template <
typename T>
121 bool has(
const std::string& key)
const;
125 template <
typename T>
126 T&
get(
const std::string& key);
127 template <
typename T>
128 const T&
get(
const std::string& key)
const;
130 template <
typename T>
131 const T&
get(
const std::string& key,
const typename identity<T>::type& default_value)
const;
136 const std::map<std::string, Value*>& kv_map()
const;
137 std::string json()
const;
138 std::string
xml(
unsigned format =
JSONx,
const std::string &header = std::string(),
const std::string &attrib = std::string() )
const;
139 std::string write(
unsigned format )
const;
142 bool parse(std::istream &input);
143 bool parse(
const std::string &input);
145 void import(
const Object &other );
146 void import(
const std::string &key,
const Value &value );
151 Object(
const std::string &key,
const Value &value);
160 static bool parse(std::istream& input,
Object&
object);
173 template <
typename T>
174 bool has(
unsigned int i)
const;
176 template <
typename T>
177 T&
get(
unsigned int i);
178 template <
typename T>
179 const T&
get(
unsigned int i)
const;
181 template <
typename T>
182 const T&
get(
unsigned int i,
const typename identity<T>::type& default_value)
const;
184 const std::vector<Value*>&
values()
const {
187 std::string json()
const;
188 std::string
xml(
unsigned format =
JSONx,
const std::string &header = std::string(),
const std::string &attrib = std::string() )
const;
190 std::string
write(
unsigned format )
const {
return format ==
JSON ? json() :
xml(format); }
192 bool parse(std::istream &input);
193 bool parse(
const std::string &input);
195 void append(
const Array &other);
197 void import(
const Array &other);
198 void import(
const Value &value);
206 static bool parse(std::istream& input,
Array& array);
220 void import(
const T & ) {
226 void import(
const bool &b ) {
231 #define $number(TYPE) \ 232 void import( const TYPE &n ) { \ 235 number_value_ = static_cast<long double>(n); \ 241 $number(
unsigned char )
242 $number(
unsigned int )
243 $number(
unsigned long )
244 $number(
unsigned long long )
247 $number(
long double )
249 #if JSONXX_COMPILER_HAS_CXX11 > 0 250 void import(
const std::nullptr_t & ) {
255 void import(
const Null & ) {
259 void import(
const String &s ) {
262 *( string_value_ =
new String() ) = s;
267 *( array_value_ =
new Array() ) = a;
272 *( object_value_ =
new Object() ) = o;
276 switch (other.type_) {
281 import( other.bool_value_ );
284 import( other.number_value_ );
287 import( *other.string_value_ );
290 import( *other.array_value_ );
293 import( *other.object_value_ );
315 Value(
const T&t ) : type_(INVALID_) {
import(t); }
317 Value(
const char (&t)[N] ) : type_(INVALID_) {
import( std::string(t) ); }
319 bool parse(std::istream &input);
320 bool parse(
const std::string &input);
327 const T&
get()
const;
350 static bool parse(std::istream& input,
Value& value);
353 template <
typename T>
358 Value* v = values_.at(i);
363 template <
typename T>
366 Value* v = values_.at(i);
370 template <
typename T>
373 const Value* v = values_.at(i);
377 template <
typename T>
380 const Value* v = values_.at(i);
383 return default_value;
387 template <
typename T>
389 container::const_iterator it(value_map_.find(key));
390 return it != value_map_.end() && it->second->is<T>();
393 template <
typename T>
396 return value_map_.find(key)->second->get<T>();
399 template <
typename T>
402 return value_map_.find(key)->second->get<T>();
405 template <
typename T>
408 return value_map_.find(key)->second->get<T>();
410 return default_value;
415 inline bool Value::is<Value>()
const {
420 inline bool Value::is<Null>()
const {
421 return type_ == NULL_;
425 inline bool Value::is<Boolean>()
const {
426 return type_ == BOOL_;
430 inline bool Value::is<String>()
const {
431 return type_ == STRING_;
435 inline bool Value::is<Number>()
const {
436 return type_ == NUMBER_;
440 inline bool Value::is<Array>()
const {
441 return type_ == ARRAY_;
445 inline bool Value::is<Object>()
const {
446 return type_ == OBJECT_;
455 inline const Value& Value::get<Value>()
const {
460 inline bool& Value::get<Boolean>() {
466 inline std::string& Value::get<String>() {
468 return *string_value_;
472 inline Number& Value::get<Number>() {
474 return number_value_;
480 return *array_value_;
486 return *object_value_;
490 inline const Boolean& Value::get<Boolean>()
const {
496 inline const String& Value::get<String>()
const {
498 return *string_value_;
502 inline const Number& Value::get<Number>()
const {
504 return number_value_;
508 inline const Array& Value::get<Array>()
const {
510 return *array_value_;
514 inline const Object& Value::get<Object>()
const {
516 return *object_value_;
521 *
this <<
Value(value);
Object(const char(&key)[N], const Value &value)
Definition: jsonxx.h:153
std::string xml(std::istream &input, unsigned format)
Definition: jsonxx.cc:970
long double Number
Definition: jsonxx.h:89
Value(const char(&t)[N])
Definition: jsonxx.h:317
bool unquoted_keys_are_enabled()
Definition: jsonxx.h:74
T & get(const std::string &key)
Definition: jsonxx.h:394
std::vector< Value * > container
Definition: jsonxx.h:194
String * string_value_
Definition: jsonxx.h:343
$number(char) $number(int) $number(long) $number(long long) $number(unsigned char) $number(unsigned int) $number(unsigned long) $number(unsigned long long) $number(float) $number(double) $number(long double) $ void import(const Null &)
Definition: jsonxx.h:237
T type
Definition: jsonxx.h:100
bool has(const std::string &key) const
Definition: jsonxx.h:388
Boolean bool_value_
Definition: jsonxx.h:344
Value(const T &t)
Definition: jsonxx.h:315
#define JSONXX_ASSERT(...)
Definition: jsonxx.h:48
std::map< std::string, Value * > container
Definition: jsonxx.h:144
Format
Definition: jsonxx.h:80
bool parser_is_permissive()
Definition: jsonxx.h:73
container value_map_
Definition: jsonxx.h:161
bool has(unsigned int i) const
Definition: jsonxx.h:354
std::string write(unsigned format) const
Definition: jsonxx.h:190
bool validate(std::istream &input)
Definition: jsonxx.cc:910
const std::vector< Value * > & values() const
Definition: jsonxx.h:184
Array * array_value_
Definition: jsonxx.h:345
Object * object_value_
Definition: jsonxx.h:346
std::string odd
Definition: jsonxx.h:162
container values_
Definition: jsonxx.h:207
Object & operator<<(const Value &value)
Definition: jsonxx.cc:1044
std::ostream & operator<<(std::ostream &stream, const jsonxx::Value &v)
Definition: jsonxx.cc:469
bool Boolean
Definition: jsonxx.h:90
Settings
Definition: jsonxx.h:56
std::string reformat(std::istream &input)
Definition: jsonxx.cc:940
void assertion(const char *file, int line, const char *expression, bool result)
Definition: jsonxx.cc:27
~Value()
Definition: jsonxx.h:216
void append(const Value &value)
Definition: jsonxx.h:196
bool parser_is_strict()
Definition: jsonxx.h:72
Number number_value_
Definition: jsonxx.h:342
T & get(unsigned int i)
Definition: jsonxx.h:364
std::string String
Definition: jsonxx.h:91