11 #include <cppcms/defs.h> 12 #include <cppcms/string_key.h> 13 #include <booster/copy_ptr.h> 14 #include <booster/backtrace.h> 41 inline bool operator==(
null const &,
null const &) {
return true;}
42 inline bool operator!=(
null const &,
null const &) {
return false;}
47 typedef std::vector<value>
array;
51 typedef std::map<string_key,value>
object;
53 #ifdef CPPCMS_DOXYGEN_DOCS 64 static T
get(
value const &v);
68 static void set(
value &v,T
const &in);
109 bad_value_cast(std::string
const &s,json_type expected, json_type actual);
112 virtual const char* what()
const throw();
132 std::ostream CPPCMS_API &
operator<<(std::ostream &out,json_type);
146 json_type type()
const;
160 bool const &boolean()
const;
164 double const &
number()
const;
168 std::string
const &str()
const;
219 void str(std::string
const &);
255 value const &find(std::string
const &path)
const;
263 value const &find(
char const *path)
const;
272 value const &at(std::string
const &path)
const;
280 value const &at(
char const *path)
const;
288 value &at(std::string
const &path);
296 value &at(
char const *path);
301 void at(std::string
const &path,
value const &v);
305 void at(
char const *path,
value const &v);
322 json_type
type(std::string
const &path)
const 324 return find(path).
type();
331 json_type
type(
char const *path)
const 333 return find(path).
type();
340 void set(std::string
const &path,T
const &v)
348 void set(
char const *path,T
const &v)
357 std::string
get(std::string
const &path,
char const *def)
const 359 value const &v=find(path);
365 catch(std::bad_cast
const &e) {
373 std::string
get(
char const *path,
char const *def)
const 375 value const &v=find(path);
381 catch(std::bad_cast
const &e) {
391 T
get(std::string
const &path)
const 393 return at(path).get_value<T>();
400 T
get(
char const *path)
const 402 return at(path).get_value<T>();
410 T
get(
char const *path,T
const &def)
const 412 value const &v=find(path);
418 catch(std::bad_cast
const &e) {
427 T
get(std::string
const &path,T
const &def)
const 429 value const &v=find(path);
435 catch(std::bad_cast
const &e) {
447 value &operator[](std::string
const &name);
455 value const &operator[](std::string
const &name)
const;
461 value &operator[](
size_t n);
466 value const &operator[](
size_t n)
const;
471 std::string save(
int how=
compact)
const;
475 void save(std::ostream &out,
int how=
compact)
const;
486 bool load(std::istream &in,
bool full,
int *line_number=0);
500 bool load(
char const *&begin,
char const *end,
bool full,
int *line_number=0);
505 bool operator==(
value const &other)
const;
509 bool operator!=(
value const &other)
const;
551 void write(std::ostream &out,
int tabs)
const;
552 void write_value(std::ostream &out,
int tabs)
const;
555 struct CPPCMS_API copyable {
557 _data *operator->() {
return &*d; }
558 _data &operator*() {
return *d; }
559 _data
const *operator->()
const {
return &*d; }
560 _data
const &operator*()
const {
return *d; }
563 copyable(copyable
const &r);
564 copyable
const &operator=(copyable
const &r);
567 void swap(copyable &other)
575 friend struct copyable;
584 std::string CPPCMS_API
to_json(std::string
const &utf);
590 std::string CPPCMS_API
to_json(
char const *begin,
char const *end);
596 void CPPCMS_API
to_json(
char const *begin,
char const *end,std::ostream &out);
602 void CPPCMS_API
to_json(std::string
const &str,std::ostream &out);
607 template<
typename T1,
typename T2>
608 struct traits<std::pair<T1,T2> > {
609 static std::pair<T1,T2>
get(
value const &v)
616 static void set(
value &v,std::pair<T1,T2>
const &in)
619 v.set_value(
"first",in.first);
620 v.set_value(
"second",in.second);
625 struct traits<std::vector<T> > {
626 static std::vector<T>
get(
value const &v)
628 std::vector<T> result;
630 result.resize(a.size());
631 for(
unsigned i=0;i<a.size();i++)
632 result[i]=a[i].get_value<T>();
635 static void set(
value &v,std::vector<T>
const &in)
640 for(
unsigned i=0;i<in.size();i++)
641 a[i].set_value(in[i]);
646 #define CPPCMS_JSON_SPECIALIZE(type,method) \ 648 struct traits<type> { \ 649 static type get(value const &v) \ 653 static void set(value &v,type const &in)\ 659 CPPCMS_JSON_SPECIALIZE(
bool,
boolean);
660 CPPCMS_JSON_SPECIALIZE(
double,
number);
661 CPPCMS_JSON_SPECIALIZE(std::string,str);
665 #undef CPPCMS_JSON_SPECIALIZE 667 #define CPPCMS_JSON_SPECIALIZE_INT(type) \ 669 struct traits<type> { \ 670 static type get(value const &v) \ 672 type res=static_cast<type>(v.number()); \ 673 if(res!=v.number()) \ 674 throw bad_value_cast(); \ 677 static void set(value &v,type const &in) \ 679 if(std::numeric_limits<type>::digits > \ 680 std::numeric_limits<double>::digits \ 681 && static_cast<double>(in)!=in) \ 683 throw bad_value_cast(); \ 685 v.number(static_cast<double>(in)); \ 689 CPPCMS_JSON_SPECIALIZE_INT(
char)
690 CPPCMS_JSON_SPECIALIZE_INT(
unsigned char)
691 CPPCMS_JSON_SPECIALIZE_INT(
signed char)
692 CPPCMS_JSON_SPECIALIZE_INT(
wchar_t)
693 CPPCMS_JSON_SPECIALIZE_INT(
short)
694 CPPCMS_JSON_SPECIALIZE_INT(
unsigned short)
695 CPPCMS_JSON_SPECIALIZE_INT(
int)
696 CPPCMS_JSON_SPECIALIZE_INT(
unsigned int)
697 CPPCMS_JSON_SPECIALIZE_INT(
long)
698 CPPCMS_JSON_SPECIALIZE_INT(
unsigned long)
699 CPPCMS_JSON_SPECIALIZE_INT(
long long)
700 CPPCMS_JSON_SPECIALIZE_INT(
unsigned long long)
702 #undef CPPCMS_JSON_SPECIALIZE_INT 706 static float get(
value const &v)
709 if( r < (-std::numeric_limits<float>::max())
710 || std::numeric_limits<float>::max() < r )
714 return static_cast<float>(r);
716 static void set(
value &v,
float const &in)
723 struct traits<long double> {
724 static long double get(
value const &v)
728 static void set(
value &v,
long double const &in)
730 if( in < -std::numeric_limits<double>::max()
731 || std::numeric_limits<double>::max() < in )
735 v.number(static_cast<double>(in));
749 typedef char vtype[n];
750 static void set(
value &v,vtype
const &in)
756 struct traits<char const [n]> {
757 typedef char const vtype[n];
758 static void set(
value &v,vtype
const &in)
766 struct traits<char const *> {
767 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:140
json_type
Definition: json.h:82
Print JSON values in most compact format.
Definition: json.h:94
value(value const &other)
Definition: json.h:514
json_type type(std::string const &path) const
Definition: json.h:322
Special object that is convertible to undefined json value.
Definition: json.h:37
boolean value
Definition: json.h:85
string value
Definition: json.h:87
std::map< string_key, value > object
The json::object - std::map of json::value's.
Definition: json.h:51
json::array const & array() const
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
Special object that is convertible to null json value.
Definition: json.h:33
T get_value() const
Definition: json.h:234
void set_value(T const &v)
Definition: json.h:243
value(T const &v)
Definition: json.h:312
object value
Definition: json.h:88
double const & number() const
value const & operator=(value const &other)
Definition: json.h:521
The error that is thrown in case of bad conversion of json::value to ordinary value.
Definition: json.h:104
std::string CPPCMS_API to_json(std::string const &utf)
void swap(value &other)
Definition: json.h:544
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:84
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:331
The type traits schema for converting json values to/from orinary objects i.e. serialization from JSO...
Definition: json.h:60
numeric value
Definition: json.h:86
json::object const & object() const
~value()
Definition: json.h:537
std::vector< value > array
The json::array - std::vector of json::value's.
Definition: json.h:47
Undefined value.
Definition: json.h:83
array value
Definition: json.h:89
Print JSON values in human readable format (with identention)
Definition: json.h:95
value()
Definition: json.h:529