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;
517 d=std::move(other.d);
567 void write(std::ostream &out,
int tabs)
const;
568 void write_value(std::ostream &out,
int tabs)
const;
571 struct CPPCMS_API copyable {
573 _data *operator->() {
return &*d; }
574 _data &operator*() {
return *d; }
575 _data
const *operator->()
const {
return &*d; }
576 _data
const &operator*()
const {
return *d; }
579 copyable(copyable
const &r);
580 copyable(copyable &&);
581 copyable &operator=(copyable &&r);
582 copyable
const &operator=(copyable
const &r);
585 void swap(copyable &other)
593 friend struct copyable;
602 std::string CPPCMS_API
to_json(std::string
const &utf);
608 std::string CPPCMS_API
to_json(
char const *begin,
char const *end);
614 void CPPCMS_API
to_json(
char const *begin,
char const *end,std::ostream &out);
620 void CPPCMS_API
to_json(std::string
const &str,std::ostream &out);
625 template<
typename T1,
typename T2>
626 struct traits<std::pair<T1,T2> > {
627 static std::pair<T1,T2>
get(
value const &v)
634 static void set(
value &v,std::pair<T1,T2>
const &in)
637 v.set_value(
"first",in.first);
638 v.set_value(
"second",in.second);
643 struct traits<std::vector<T> > {
644 static std::vector<T>
get(
value const &v)
646 std::vector<T> result;
648 result.resize(a.size());
649 for(
unsigned i=0;i<a.size();i++)
650 result[i]=a[i].get_value<T>();
653 static void set(
value &v,std::vector<T>
const &in)
658 for(
unsigned i=0;i<in.size();i++)
659 a[i].set_value(in[i]);
664 #define CPPCMS_JSON_SPECIALIZE(type,method) \ 666 struct traits<type> { \ 667 static type get(value const &v) \ 671 static void set(value &v,type const &in)\ 677 CPPCMS_JSON_SPECIALIZE(
bool,
boolean);
678 CPPCMS_JSON_SPECIALIZE(
double,
number);
679 CPPCMS_JSON_SPECIALIZE(std::string,str);
683 #undef CPPCMS_JSON_SPECIALIZE 685 #define CPPCMS_JSON_SPECIALIZE_INT(type) \ 687 struct traits<type> { \ 688 static type get(value const &v) \ 690 type res=static_cast<type>(v.number()); \ 691 if(res!=v.number()) \ 692 throw bad_value_cast(); \ 695 static void set(value &v,type const &in) \ 697 if(std::numeric_limits<type>::digits > \ 698 std::numeric_limits<double>::digits \ 699 && static_cast<double>(in)!=in) \ 701 throw bad_value_cast(); \ 703 v.number(static_cast<double>(in)); \ 707 CPPCMS_JSON_SPECIALIZE_INT(
char)
708 CPPCMS_JSON_SPECIALIZE_INT(
unsigned char)
709 CPPCMS_JSON_SPECIALIZE_INT(
signed char)
710 CPPCMS_JSON_SPECIALIZE_INT(
wchar_t)
711 CPPCMS_JSON_SPECIALIZE_INT(
short)
712 CPPCMS_JSON_SPECIALIZE_INT(
unsigned short)
713 CPPCMS_JSON_SPECIALIZE_INT(
int)
714 CPPCMS_JSON_SPECIALIZE_INT(
unsigned int)
715 CPPCMS_JSON_SPECIALIZE_INT(
long)
716 CPPCMS_JSON_SPECIALIZE_INT(
unsigned long)
717 CPPCMS_JSON_SPECIALIZE_INT(
long long)
718 CPPCMS_JSON_SPECIALIZE_INT(
unsigned long long)
720 #undef CPPCMS_JSON_SPECIALIZE_INT 724 static float get(
value const &v)
727 if( r < (-std::numeric_limits<float>::max())
728 || std::numeric_limits<float>::max() < r )
732 return static_cast<float>(r);
734 static void set(
value &v,
float const &in)
741 struct traits<long double> {
742 static long double get(
value const &v)
746 static void set(
value &v,
long double const &in)
748 if( in < -std::numeric_limits<double>::max()
749 || std::numeric_limits<double>::max() < in )
753 v.number(static_cast<double>(in));
767 typedef char vtype[n];
768 static void set(
value &v,vtype
const &in)
774 struct traits<char const [n]> {
775 typedef char const vtype[n];
776 static void set(
value &v,vtype
const &in)
784 struct traits<char const *> {
785 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:530
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
value(value &&other)
Definition: json.h:523
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:537
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:560
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:553
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 & operator=(value &&other)
Definition: json.h:515
value()
Definition: json.h:545