38 #include <boost/math/special_functions/fpclassify.hpp> 39 #include <boost/lexical_cast.hpp> 41 #include "utf8/utf8.h" 62 const char* what()
const throw() {
63 return (
const char*)err.c_str();
72 template<
typename T>
void assertValidityOfNumericType(
const T &value) {
73 assert (std::numeric_limits<T>::is_specialized);
74 if (boost::math::isnan(value) || !boost::math::isfinite(value)) {
75 throw JSONException(
"Illegal floating point value. JSON spec do not allow NaN/Inifnity values for numbers. Value provided = '" + boost::lexical_cast<std::string>(value) +
"'");
118 virtual void write(std::ostream &out)
const = 0;
119 virtual Value* returnMyNewCopy()
const = 0;
120 virtual void read(std::istream &in) = 0;
121 virtual bool isEqual(
const Value* other)
const = 0;
140 typedef std::map<std::string, JSON>::iterator object_iterator;
141 typedef std::map<std::string, JSON>::const_iterator const_object_iterator;
142 typedef std::vector<JSON>::iterator array_iterator;
143 typedef std::vector<JSON>::const_iterator const_array_iterator;
145 typedef std::map<std::string, JSON>::const_reverse_iterator object_reverse_iterator;
146 typedef std::map<std::string, JSON>::reverse_iterator const_object_reverse_iterator;
147 typedef std::vector<JSON>::reverse_iterator array_reverse_iterator;
148 typedef std::vector<JSON>::const_reverse_iterator const_array_reverse_iterator;
160 static double getEpsilon();
197 void clear() {
delete val; val=NULL; }
205 void write(std::ostream &out)
const;
236 void read(std::istream &in);
245 void readFromString(
const std::string &jstr);
256 std::string toString(
bool onlyTopLevel =
false)
const;
265 bool operator ==(
const JSON& other)
const;
272 bool operator !=(
const JSON& other)
const {
return !(*
this == other); }
279 const JSON& operator [](
const size_t &indx)
const;
286 const JSON& operator [](
const std::string &s)
const;
297 const JSON& operator [](
const JSON &j)
const;
304 const JSON& operator [](
const char *str)
const;
314 const JSON& operator [](
const T&x)
const;
320 JSON& operator [](
const size_t &indx);
328 JSON& operator [](
const std::string &s);
343 JSON& operator [](
const char *str);
350 JSON& operator [](
const T& x) {
return const_cast<JSON&
>( (*(
const_cast<const JSON*
>(
this)))[x]); }
360 template<
typename T>
JSON& operator =(
const T &rhs);
382 JSON& operator =(
const char &c);
389 JSON& operator =(
const std::string &s);
396 JSON& operator =(
const bool &x);
403 JSON& operator =(
const char s[]);
410 JSON& operator =(
const Null &x);
418 JSON& operator =(
const std::vector<T> &vec);
426 JSON& operator =(
const std::map<std::string, T> &m);
444 return static_cast<T
>(*this);
450 JSONValue type()
const {
return (val == NULL) ? JSON_UNDEFINED : val->type(); }
466 void resize_array(
size_t desired_size);
488 bool has(
const T &indx)
const {
return has(static_cast<size_t>(indx)); }
495 bool has(
const size_t &indx)
const;
503 bool has(
const std::string &key)
const;
508 bool has(
const JSON &j)
const;
515 bool has(
const char *key)
const;
521 void push_back(
const JSON &j);
527 void erase(
const size_t &indx);
534 void erase(
const std::string &key);
540 const_object_iterator object_begin()
const;
546 object_iterator object_begin();
552 const_array_iterator array_begin()
const;
558 array_iterator array_begin();
564 const_object_iterator object_end()
const;
570 object_iterator object_end();
576 const_array_iterator array_end()
const;
582 array_iterator array_end();
588 const_array_reverse_iterator array_rbegin()
const;
594 array_reverse_iterator array_rbegin();
601 const_array_reverse_iterator array_rend()
const;
608 array_reverse_iterator array_rend();
614 class Integer:
public Value {
619 Integer(
const int64_t &v): val(v) {}
620 void write(std::ostream &out)
const { out << val; }
621 JSONValue type()
const {
return JSON_INTEGER; }
622 size_t returnAsArrayIndex()
const {
return static_cast<size_t>(val);}
623 Value* returnMyNewCopy()
const {
return new Integer(*
this); }
624 operator const Value* () {
return this; }
625 bool isEqual(
const Value *other)
const;
626 bool operator ==(
const Integer& other)
const {
return isEqual(&other); }
627 bool operator !=(
const Integer &other)
const {
return !(*
this == other); }
631 void read(std::istream &in __attribute__ ((unused)) ) { assert(
false); }
634 class Real:
public Value {
639 Real(
const double &v) {
640 assertValidityOfNumericType(v);
643 void write(std::ostream &out)
const { out << val; }
644 JSONValue type()
const {
return JSON_REAL; }
645 size_t returnAsArrayIndex()
const {
return static_cast<size_t>(val);}
646 Value* returnMyNewCopy()
const {
return new Real(*
this); }
647 bool isEqual(
const Value *other)
const;
648 bool operator ==(
const Real& other)
const {
return isEqual(&other); }
649 bool operator !=(
const Real& other)
const {
return !(*
this == other); }
653 void read(std::istream &in __attribute__ ((unused)) ) { assert(
false); }
656 class String:
public Value {
661 String(
const std::string &v):val(v) {}
663 void write(std::ostream &out)
const;
664 JSONValue type()
const {
return JSON_STRING; }
665 std::string returnString()
const {
return val; }
666 Value* returnMyNewCopy()
const {
return new String(*
this); }
667 void read(std::istream &in);
668 bool isEqual(
const Value *other)
const;
669 bool operator ==(
const String& other)
const {
return isEqual(&other); }
670 bool operator !=(
const String& other)
const {
return !(*
this == other); }
675 class Object:
public Value {
677 std::map<std::string, JSON> val;
680 Object(
const Object &rhs): val(rhs.val) {}
683 Object(
const std::map<std::string, T> &v) {
684 val.insert(v.begin(), v.end());
687 JSON& jsonAtKey(
const std::string &s);
688 const JSON& jsonAtKey(
const std::string &s)
const;
689 void write(std::ostream &out)
const;
690 JSONValue type()
const {
return JSON_OBJECT; }
691 Value* returnMyNewCopy()
const {
return new Object(*
this); }
692 void read(std::istream &in);
693 void erase(
const std::string &key);
694 bool isEqual(
const Value *other)
const;
695 bool operator ==(
const Object& other)
const {
return isEqual(&other); }
696 bool operator !=(
const Object& other)
const {
return !(*
this == other); }
699 class Array:
public Value {
701 std::vector<JSON> val;
704 Array(
const Array& arr): val(arr.val) {}
707 Array(
const std::vector<T> &vec) {
708 for (
unsigned i = 0;i < vec.size(); i++) {
714 const JSON& jsonAtIndex(
size_t i)
const;
715 void write(std::ostream &out)
const;
716 JSONValue type()
const {
return JSON_ARRAY; }
717 Value* returnMyNewCopy()
const {
return new Array(*
this); }
718 void read(std::istream &in);
719 void push_back(
const JSON &j) {
722 void erase(
const size_t &i);
723 bool isEqual(
const Value* other)
const;
724 bool operator ==(
const Array& other)
const {
return isEqual(&other); }
725 bool operator !=(
const Array& other)
const {
return !(*
this == other); }
729 class Boolean:
public Value {
734 Boolean(
const bool &v):val(v) {}
735 JSON& jsonAtKey(
const std::string &s);
736 const JSON& jsonAtKey(
const std::string &s)
const;
737 JSONValue type()
const {
return JSON_BOOLEAN; }
738 void write(std::ostream &out)
const { out << ((val) ?
"true" :
"false"); }
739 Value* returnMyNewCopy()
const {
return new Boolean(*
this); }
740 void read(std::istream &in);
741 bool isEqual(
const Value* other)
const;
742 bool operator ==(
const Boolean& other)
const {
return isEqual(&other); }
743 bool operator !=(
const Boolean& other)
const {
return !(*
this == other); }
746 class Null:
public Value {
748 void write(std::ostream &out)
const { out <<
"null"; }
749 JSONValue type()
const {
return JSON_NULL; }
750 Value* returnMyNewCopy()
const {
return new Null(*
this); }
751 void read(std::istream &in);
752 bool isEqual(
const Value* other)
const;
753 bool operator ==(
const Null& other)
const {
return isEqual(&other); }
754 bool operator !=(
const Null& other)
const {
return !(*
this == other); }
761 *
this = operator=(x);
767 if (!std::numeric_limits<T>::is_specialized)
768 throw JSONException(
"Sorry! We do not allow creating a JSON object from " + std::string(
typeid(x).name()) +
" type.");
770 if (!std::numeric_limits<T>::is_integer)
771 assertValidityOfNumericType(x);
774 if(std::numeric_limits<T>::is_integer)
775 this->val =
new Integer(static_cast<int64_t>(x));
777 this->val =
new Real(static_cast<double>(x));
784 this->val =
new Array(vec);
791 this->val =
new Object(m);
796 JSON::operator T()
const {
798 if (typ != JSON_INTEGER && typ != JSON_REAL && typ != JSON_BOOLEAN)
799 throw JSONException(
"No typecast available for this JSON object to a Numeric/Boolean type");
801 if (!std::numeric_limits<T>::is_specialized)
802 throw JSONException(
"You cannot convert this JSON object to Numeric/Boolean type.");
806 return static_cast<T
>( ((Integer*)this->val)->val);
808 return static_cast<T
>( ((Real*)this->val)->val);
810 return static_cast<T
>( ((Boolean*)this->val)->val);
811 default: assert(
false);
817 return (*(const_cast<const JSON*>(
this)))[
static_cast<size_t>(x)];
828 inline std::string JSON::get<std::string>()
const {
829 if (this->type() != JSON_STRING)
830 throw JSONException(
"You cannot use get<std::string>/get<char*> for a non JSON_STRING value");
831 return ((String*)this->val)->val;
JSON & operator=(const T &rhs)
static JSON parse(const std::string &str)
bool has(const T &indx) const
const JSON & operator[](const size_t &indx) const
void readFromString(const std::string &jstr)