11 #include <cppcms/defs.h>
12 #include <cppcms/string_key.h>
13 #include <booster/copy_ptr.h>
14 #include <booster/backtrace.h>
42 inline bool operator!=(undefined
const &,undefined
const &) {
return false;}
43 inline bool operator==(null
const &,null
const &) {
return true;}
44 inline bool operator!=(null
const &,null
const &) {
return false;}
49 typedef std::vector<value>
array;
53 typedef std::map<string_key,value>
object;
55 #ifdef CPPCMS_DOXYGEN_DOCS
66 static T
get(
value const &v);
70 static void set(
value &v,T
const &in);
114 virtual const char* what()
const throw();
162 bool const &boolean()
const;
166 double const &
number()
const;
170 std::string
const &str()
const;
221 void str(std::string
const &);
257 value const &find(std::string
const &path)
const;
265 value const &find(
char const *path)
const;
274 value const &at(std::string
const &path)
const;
282 value const &at(
char const *path)
const;
290 value &at(std::string
const &path);
298 value &at(
char const *path);
303 void at(std::string
const &path,
value const &v);
307 void at(
char const *path,
value const &v);
326 return find(path).type();
335 return find(path).type();
342 void set(std::string
const &path,T
const &v)
350 void set(
char const *path,T
const &v)
359 std::string
get(std::string
const &path,
char const *def)
const
361 value const &v=find(path);
367 catch(std::bad_cast
const &e) {
375 std::string
get(
char const *path,
char const *def)
const
377 value const &v=find(path);
383 catch(std::bad_cast
const &e) {
393 T
get(std::string
const &path)
const
395 return at(path).get_value<T>();
402 T
get(
char const *path)
const
404 return at(path).get_value<T>();
412 T
get(
char const *path,T
const &def)
const
414 value const &v=find(path);
420 catch(std::bad_cast
const &e) {
429 T
get(std::string
const &path,T
const &def)
const
431 value const &v=find(path);
437 catch(std::bad_cast
const &e) {
449 value &operator[](std::string
const &name);
457 value const &operator[](std::string
const &name)
const;
463 value &operator[](
size_t n);
468 value const &operator[](
size_t n)
const;
473 std::string save(
int how=
compact)
const;
477 void save(std::ostream &out,
int how=
compact)
const;
488 bool load(std::istream &in,
bool full,
int *line_number=0);
493 bool operator==(
value const &other)
const;
497 bool operator!=(
value const &other)
const;
539 void write(std::ostream &out,
int tabs)
const;
540 void write_value(std::ostream &out,
int tabs)
const;
543 struct CPPCMS_API copyable {
545 _data *operator->() {
return &*d; }
546 _data &operator*() {
return *d; }
547 _data
const *operator->()
const {
return &*d; }
548 _data
const &
operator*()
const {
return *d; }
551 copyable(copyable
const &r);
552 copyable
const &operator=(copyable
const &r);
555 void swap(copyable &other)
563 friend struct copyable;
570 template<
typename T1,
typename T2>
571 struct traits<std::pair<T1,T2> > {
572 static std::pair<T1,T2>
get(value
const &v)
574 if(v.object().size()!=2)
575 throw bad_value_cast(
"Object with two members expected");
576 std::pair<T1,T2> pair(v.get_value<T1>(
"first"),v.get_value<T2>(
"second"));
579 static void set(value &v,std::pair<T1,T2>
const &in)
582 v.set_value(
"first",in.first);
583 v.set_value(
"second",in.second);
588 struct traits<std::vector<T> > {
589 static std::vector<T>
get(value
const &v)
591 std::vector<T> result;
593 result.resize(a.size());
594 for(
unsigned i=0;i<a.size();i++)
595 result[i]=a[i].get_value<T>();
598 static void set(value &v,std::vector<T>
const &in)
603 for(
unsigned i=0;i<in.size();i++)
604 a[i].set_value(in[i]);
609 #define CPPCMS_JSON_SPECIALIZE(type,method) \
611 struct traits<type> { \
612 static type get(value const &v) \
616 static void set(value &v,type const &in)\
622 CPPCMS_JSON_SPECIALIZE(
bool,
boolean);
623 CPPCMS_JSON_SPECIALIZE(
double,number);
624 CPPCMS_JSON_SPECIALIZE(std::string,str);
628 #undef CPPCMS_JSON_SPECIALIZE
630 #define CPPCMS_JSON_SPECIALIZE_INT(type) \
632 struct traits<type> { \
633 static type get(value const &v) \
635 type res=static_cast<type>(v.number()); \
636 if(res!=v.number()) \
637 throw bad_value_cast(); \
640 static void set(value &v,type const &in) \
642 if(std::numeric_limits<type>::digits > \
643 std::numeric_limits<double>::digits \
644 && static_cast<double>(in)!=in) \
646 throw bad_value_cast(); \
648 v.number(static_cast<double>(in)); \
652 CPPCMS_JSON_SPECIALIZE_INT(
char)
653 CPPCMS_JSON_SPECIALIZE_INT(
unsigned char)
654 CPPCMS_JSON_SPECIALIZE_INT(
signed char)
655 CPPCMS_JSON_SPECIALIZE_INT(
wchar_t)
656 CPPCMS_JSON_SPECIALIZE_INT(
short)
657 CPPCMS_JSON_SPECIALIZE_INT(
unsigned short)
658 CPPCMS_JSON_SPECIALIZE_INT(
int)
659 CPPCMS_JSON_SPECIALIZE_INT(
unsigned int)
660 CPPCMS_JSON_SPECIALIZE_INT(
long)
661 CPPCMS_JSON_SPECIALIZE_INT(
unsigned long)
662 CPPCMS_JSON_SPECIALIZE_INT(
long long)
663 CPPCMS_JSON_SPECIALIZE_INT(
unsigned long long)
665 #undef CPPCMS_JSON_SPECIALIZE_INT
668 struct traits<float> {
669 static float get(value
const &v)
672 if( r < std::numeric_limits<float>::min()
673 || std::numeric_limits<float>::max() < r )
675 throw bad_value_cast();
677 return static_cast<float>(r);
679 static void set(value &v,
float const &in)
686 struct traits<long double> {
687 static long double get(value
const &v)
691 static void set(value &v,
long double const &in)
693 if( in < std::numeric_limits<double>::min()
694 || std::numeric_limits<double>::max() < in )
696 throw bad_value_cast();
698 v.number(static_cast<double>(in));
703 struct traits<json::null> {
704 static void set(value &v,json::null
const &)
711 struct traits<char[n]> {
712 typedef char vtype[n];
713 static void set(value &v,vtype
const &in)
719 struct traits<char const [n]> {
720 typedef char const vtype[n];
721 static void set(value &v,vtype
const &in)
729 struct traits<char const *> {
730 static void set(value &v,
char const *
const &in)
static T get(value const &v)
This class is central representation of json objects.
Definition: json.h:142
json_type
Definition: json.h:84
Print JSON values in most compact format.
Definition: json.h:96
void set(char const *path, T const &v)
Definition: json.h:350
value(value const &other)
Definition: json.h:502
json_type type(std::string const &path) const
Definition: json.h:324
Special object that is convertible to undefined json value.
Definition: json.h:39
boolean value
Definition: json.h:87
string value
Definition: json.h:89
std::map< string_key, value > object
The json::object - std::map of json::value's.
Definition: json.h:53
Special object that is convertible to null json value.
Definition: json.h:35
T get_value() const
Definition: json.h:236
void set_value(T const &v)
Definition: json.h:245
date_time_period operator*(period::period_type f, T v)
Definition: date_time.h:335
value(T const &v)
Definition: json.h:314
object value
Definition: json.h:90
value const & operator=(value const &other)
Definition: json.h:509
The error that is thrown in case of bad conversion of json::value to ordinary value.
Definition: json.h:106
void swap(value &other)
Definition: json.h:532
bool is_undefined() const
std::ostream CPPCMS_API & operator<<(std::ostream &out, value const &v)
std::istream CPPCMS_API & operator>>(std::istream &in, value &v)
null value
Definition: json.h:86
std::ios_base & number(std::ios_base &ios)
Definition: formatting.h:292
static void set(value &v, T const &in)
Same as std::bad_cast but records stack trace.
Definition: backtrace.h:151
json_type type(char const *path) const
Definition: json.h:333
The type traits schema for converting json values to/from orinary objects i.e. serialization from JSO...
Definition: json.h:62
numeric value
Definition: json.h:88
~value()
Definition: json.h:525
std::vector< value > array
The json::array - std::vector of json::value's.
Definition: json.h:49
Undefined value.
Definition: json.h:85
array value
Definition: json.h:91
Print JSON values in human readable format (with identention)
Definition: json.h:97
void set(std::string const &path, T const &v)
Definition: json.h:342
value()
Definition: json.h:517