38#include <boost/math/special_functions/fpclassify.hpp>
39#include <boost/lexical_cast.hpp>
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;
205 void write(std::ostream &out)
const;
236 void read(std::istream &in);
256 std::string
toString(
bool onlyTopLevel =
false)
const;
444 return static_cast<T
>(*this);
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;
515 bool has(
const char *key)
const;
527 void erase(
const size_t &indx);
534 void erase(
const std::string &key);
601 const_array_reverse_iterator
array_rend()
const;
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); }
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); }
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); }
677 std::map<std::string, JSON> 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); }
701 std::vector<JSON> 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); }
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); }
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); }
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));
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;
std::string toString(bool onlyTopLevel=false) const
const_object_iterator object_begin() const
void resize_array(size_t desired_size)
void readFromString(const std::string &jstr)
const_object_iterator object_end() const
const_array_reverse_iterator array_rbegin() const
bool has(const T &indx) const
bool operator==(const JSON &other) const
const_array_iterator array_begin() const
static JSON parse(const std::string &str)
void push_back(const JSON &j)
void write(std::ostream &out) const
static double getEpsilon()
const JSON & operator[](const size_t &indx) const
const_array_reverse_iterator array_rend() const
bool operator!=(const JSON &other) const
const_array_iterator array_end() const
void erase(const size_t &indx)
void read(std::istream &in)
JSON & operator=(const T &rhs)