24 const std::string
key;
36 virtual const char*
what()
const throw()
50 inline parsing_error(
const char *message) : std::invalid_argument(message) { }
77 jtype detect(
const char *input);
91 const char*
tlws(
const char *start);
147 template <
typename T>
151 std::sscanf(input, format, &result);
162 template <
typename T>
165 std::vector<char> cstr(6);
166 int remainder = std::snprintf(&cstr[0], cstr.size(), format, number);
168 return std::string();
169 }
else if(remainder >= (
int)cstr.size()) {
170 cstr.resize(remainder + 1);
171 std::snprintf(&cstr[0], cstr.size(), format, number);
173 std::string result(&cstr[0]);
183 std::vector<std::string>
parse_array(
const char *input);
187 typedef std::pair<std::string, std::string>
kvp;
205 std::vector<kvp> data;
225 array_flag(other.array_flag)
238 inline size_t size()
const {
return this->data.size(); }
241 inline void clear() { this->data.resize(0); }
255 this->array_flag = rhs.array_flag;
256 this->data = rhs.data;
267 if(this->array_flag && other.first !=
"")
throw json::parsing_error(
"Array cannot have key");
269 this->data.push_back(other);
276 if(this->array_flag != other.array_flag)
throw json::parsing_error(
"Array/object mismatch");
278 for (
size_t i = 0; i < copy.
size(); i++) {
316 output =
parse(input);
331 inline bool has_key(
const std::string &key)
const
333 if(this->array_flag)
return false;
334 for (
size_t i = 0; i < this->
size(); i++)
if (this->data.at(i).first == key)
return true;
345 void set(
const std::string &key,
const std::string &value);
352 inline std::string
get(
const size_t index)
const
354 return this->data.at(index).second;
363 inline std::string
get(
const std::string &key)
const
366 for (
size_t i = 0; i < this->
size(); i++)
if (this->data.at(i).first == key)
return this->
get(i);
375 void remove(
const std::string &key);
383 this->data.erase(this->data.begin() + index);
394 virtual const std::string&
ref()
const = 0;
405 return json::parsing::get_number<T>(this->
ref().c_str(), format);
418 std::vector<T> result;
419 for (
size_t i = 0; i < numbers.size(); i++)
421 result.push_back(json::parsing::get_number<T>(numbers[i].c_str(), format));
436 inline operator std::string()
const
442 bool operator== (
const std::string other)
const {
return ((std::string)(*
this)) == other; }
445 bool operator!= (
const std::string other)
const {
return !(((std::string)(*
this)) == other); }
448 operator int()
const;
451 operator unsigned int()
const;
454 operator long()
const;
457 operator unsigned long()
const;
460 operator char()
const;
463 operator float()
const;
466 operator double()
const;
484 operator std::vector<int>()
const;
487 operator std::vector<unsigned int>()
const;
490 operator std::vector<long>()
const;
493 operator std::vector<unsigned long>()
const;
496 operator std::vector<char>()
const;
499 operator std::vector<float>()
const;
502 operator std::vector<double>()
const;
505 operator std::vector<json::jobject>()
const
508 std::vector<json::jobject> results;
509 for (
size_t i = 0; i < objs.size(); i++) results.push_back(
json::jobject::parse(objs[i].c_str()));
523 return (std::vector<T>)(*this);
530 return (result.
type == json::jtype::jbool && result.
value ==
"true");
552 inline const std::string&
ref()
const
606 inline const std::string&
ref()
const
608 for (
size_t i = 0; i < this->source.
size(); i++)
if (this->source.data.at(i).first ==
key)
return this->source.data.at(i).second;
620 if(source.array_flag)
throw std::logic_error(
"Source cannot be an array");
632 const char *value = this->
ref().c_str();
633 if(json::jtype::detect(value) != json::jtype::jarray)
634 throw std::invalid_argument(
"Input is not an array");
668 void set_array(
const std::vector<std::string> &values,
const bool wrap =
false);
679 std::vector<std::string> numbers;
680 for (
size_t i = 0; i < values.size(); i++)
733 this->sink.
set(
key, (std::string)input);
763 std::vector<std::string> objs;
764 for (
size_t i = 0; i < input.size(); i++)
766 objs.push_back((std::string)input[i]);
777 if (value) this->sink.
set(
key,
"true");
778 else this->sink.
set(
key,
"false");
784 this->sink.
set(
key,
"null");
831 operator std::string()
const;
838 return this->
operator std::string();
846 std::string
pretty(
unsigned int indent_level = 0)
const;
Exception used for invalid JSON keys.
Definition: json.h:21
virtual ~invalid_key()
Destructor.
Definition: json.h:33
invalid_key(const std::string &key)
Constructor.
Definition: json.h:30
const std::string key
The key used that was invalid.
Definition: json.h:24
virtual const char * what() const
Returns the invalid key.
Definition: json.h:36
Represents an entry as a constant proxy to the value.
Definition: json.h:596
const_value array(size_t index) const
Returns another constant value from this array.
Definition: json.h:630
const std::string & ref() const
Returns a reference to the value.
Definition: json.h:606
const std::string key
The key for the referenced value.
Definition: json.h:603
const_proxy(const jobject &source, const std::string key)
Constructor.
Definition: json.h:618
Represents an entry as a constant value.
Definition: json.h:542
const_value(std::string value)
Constructs a proxy with the provided value.
Definition: json.h:562
const std::string & ref() const
Reference to the entry data.
Definition: json.h:552
const_value array(const size_t index) const
Returns another constant value from this array.
Definition: json.h:585
const_value get(const std::string &key) const
Returns another constant value from this object.
Definition: json.h:573
Representation of a value in the object.
Definition: json.h:388
bool operator==(const std::string other) const
Comparison operator.
Definition: json.h:442
T get_number(const char *format) const
Converts an serialzed value to a numeric value.
Definition: json.h:403
std::string as_string() const
Returns a string representation of the value.
Definition: json.h:428
bool is_null() const
Returns true if the value is a null value.
Definition: json.h:534
virtual const std::string & ref() const =0
A method for reference the entry's value.
json::jobject as_object() const
Casts the value as a JSON object.
Definition: json.h:472
bool is_true() const
Returns true if the value is a boolean and set to true.
Definition: json.h:527
std::vector< T > as_array() const
Casts an array.
Definition: json.h:521
bool operator!=(const std::string other) const
Comparison operator.
Definition: json.h:445
std::vector< T > get_number_array(const char *format) const
Converts a serialized array of numbers to a vector of numbers.
Definition: json.h:415
A proxy that allows modification of the value.
Definition: json.h:645
void operator=(const std::vector< unsigned int > input)
Assigns an array of unsigned integers.
Definition: json.h:740
proxy(jobject &source, const std::string key)
Constructor.
Definition: json.h:692
void set_array(const std::vector< std::string > &values, const bool wrap=false)
Stores an array of values.
Definition: json.cpp:431
void operator=(const std::vector< int > input)
Assigns an array of integers.
Definition: json.h:737
void set_null()
Definition: json.h:782
void operator=(const std::vector< json::jobject > input)
Assigns an array of JSON objects.
Definition: json.h:761
void operator=(const unsigned long input)
Assigns a long unsigned integer.
Definition: json.h:719
void operator=(const std::vector< long > input)
Assigns an array of long integers.
Definition: json.h:743
void operator=(const std::vector< unsigned long > input)
Assigns an array of unsigned long integers.
Definition: json.h:746
void operator=(const long input)
Assigns a long integer.
Definition: json.h:716
void operator=(const std::vector< std::string > input)
Assigns an array of strings.
Definition: json.h:758
void set_number(const T value, const char *format)
Sets a number value in the parent object.
Definition: json.h:658
void operator=(json::jobject input)
Assigns a JSON object or array.
Definition: json.h:731
void operator=(const char input)
Assigns an character.
Definition: json.h:722
void operator=(const std::vector< char > input)
Assigns an array of characters.
Definition: json.h:749
void operator=(const double input)
Assigns an double floating-point integer
Definition: json.h:725
void set_boolean(const bool value)
Sets a boolean value.
Definition: json.h:775
void set_number_array(const std::vector< T > &values, const char *format)
Stores an array of numbers.
Definition: json.h:677
void operator=(const std::vector< float > input)
Assigns an array of floating-point numbers.
Definition: json.h:752
void operator=(const unsigned int input)
Assigns an unsigned integer.
Definition: json.h:713
void operator=(const std::vector< double > input)
Assigns an array of double floating-point numbers.
Definition: json.h:755
void operator=(const std::string value)
Assigns a string value.
Definition: json.h:698
void clear()
Definition: json.h:788
void operator=(const float input)
Assigns an floating-point integer
Definition: json.h:728
void operator=(const int input)
Assigns an integer.
Definition: json.h:710
The class used for manipulating JSON objects and arrays.
Definition: json.h:202
static jobject parse(const char *input)
Parses a serialized JSON string.
Definition: json.cpp:444
void set(const std::string &key, const std::string &value)
Sets the value assocaited with the key.
Definition: json.cpp:501
static bool tryparse(const char *input, jobject &output)
Definition: json.h:312
virtual ~jobject()
Destructor.
Definition: json.h:229
bool operator!=(const json::jobject other) const
Comparison operator.
Definition: json.h:250
const jobject::const_value array(const size_t index) const
Returns the value of an element in an array.
Definition: json.h:825
bool is_array() const
Flag for differentiating objects and arrays.
Definition: json.h:235
virtual const jobject::const_proxy operator[](const std::string key) const
Returns an element of the JSON object.
Definition: json.h:812
size_t size() const
Returns the number of entries in the JSON object or array.
Definition: json.h:238
void clear()
Clears the JSON object or array.
Definition: json.h:241
std::string get(const size_t index) const
Returns the serialized value at a given index.
Definition: json.h:352
static jobject parse(const std::string input)
Parses a serialized JSON string.
Definition: json.h:304
jobject & operator+=(const jobject &other)
Appends one JSON object to another.
Definition: json.h:274
jobject(const jobject &other)
Copy constructor.
Definition: json.h:223
jobject(bool array=false)
Default constructor.
Definition: json.h:218
jobject & operator+=(const kvp &other)
Appends a key-value pair to a JSON object.
Definition: json.h:264
void remove(const size_t index)
Removes the entry at the specified index.
Definition: json.h:381
std::string pretty(unsigned int indent_level=0) const
Returns a pretty (multi-line indented) serialzed representation of the object or array.
Definition: json.cpp:554
bool has_key(const std::string &key) const
Determines if an object contains a key.
Definition: json.h:331
std::string get(const std::string &key) const
Returns the serialized value associated with a key.
Definition: json.h:363
std::string as_string() const
Serialzes the object or array.
Definition: json.h:836
void remove(const std::string &key)
Removes the entry associated with the key.
Definition: json.cpp:518
jobject & operator=(const jobject rhs)
Assignment operator.
Definition: json.h:253
jobject operator+(jobject &other)
Merges two JSON objects.
Definition: json.h:285
bool operator==(const json::jobject other) const
Comparison operator.
Definition: json.h:247
virtual jobject::proxy operator[](const std::string key)
Returns an element of the JSON object.
Definition: json.h:800
Exception used when invalid JSON is encountered.
Definition: json.h:44
parsing_error(const char *message)
Constructor.
Definition: json.h:50
virtual ~parsing_error()
Destructor.
Definition: json.h:53
jtype
Descriptor for the type of JSON data.
Definition: json.h:60
@ jarray
JSON array.
Definition: json.h:64
@ jnull
Null value.
Definition: json.h:66
@ not_valid
Value does not conform to JSON standard.
Definition: json.h:67
@ jstring
String value.
Definition: json.h:61
@ jbool
Boolean value.
Definition: json.h:65
@ jnumber
Number value.
Definition: json.h:62
const char * tlws(const char *start)
(t)rims (l)eading (w)hite (s)pace
Definition: json.cpp:50
std::string read_digits(const char *input)
Reads a set of digits from a string.
Definition: json.cpp:97
parse_results parse(const char *input)
Parses the first value encountered in a JSON string.
Definition: json.cpp:165
std::vector< std::string > parse_array(const char *input)
Parses a JSON array.
Definition: json.cpp:386
std::string unescape_quotes(const char *input)
Removes the escape charater from quotes.
Definition: json.cpp:145
T get_number(const char *input, const char *format)
Template for reading a numeric value.
Definition: json.h:148
std::string get_number_string(const T &number, const char *format)
Converts a number to a string.
Definition: json.h:163
std::string escape_quotes(const char *input)
Escape quotes in a string.
Definition: json.cpp:130
Base namespace for simpleson.
Definition: json.h:18
std::pair< std::string, std::string > kvp
(k)ey (v)alue (p)air
Definition: json.h:187
Structure for capturing the results of parsing.
Definition: json.h:121
std::string value
The parsed value encountered.
Definition: json.h:126
const char * remainder
A pointer to the first character after the parsed value.
Definition: json.h:129
jtype::jtype type
The type of value encountered while parsing.
Definition: json.h:123