24 const std::string
key;
36 virtual const char*
what()
const throw()
50 inline parsing_error(
const char *message) : std::invalid_argument(message) { }
70 jtype peek(
const char input);
79 jtype detect(
const char *input);
100 using std::string::length;
102 #if __GNUC__ && __GNUC__ < 11
103 inline char front()
const {
return this->at(0); }
104 inline char back()
const {
return this->at(this->length() - 1); }
107 using std::string::front;
110 using std::string::back;
126 return this->length() > 0 ? jtype::peek(this->front()) : json::jtype::not_valid;
140 inline virtual std::string
readout()
const {
return *
this; }
177 return static_cast<T
>(this->read_state);
187 this->read_state = (char)state;
254 this->_colon_read =
false;
276 virtual std::string
readout()
const;
297 const char*
tlws(
const char *start);
355 template <
typename T>
359 std::sscanf(input, format, &result);
370 template <
typename T>
373 std::vector<char> cstr(6);
374 int remainder = std::snprintf(&cstr[0], cstr.size(), format, number);
376 return std::string();
377 }
else if(remainder >= (
int)cstr.size()) {
378 cstr.resize(remainder + 1);
379 std::snprintf(&cstr[0], cstr.size(), format, number);
381 std::string result(&cstr[0]);
391 std::vector<std::string>
parse_array(
const char *input);
395 typedef std::pair<std::string, std::string>
kvp;
413 std::vector<kvp> data;
433 array_flag(other.array_flag)
446 inline size_t size()
const {
return this->data.size(); }
449 inline void clear() { this->data.resize(0); }
463 this->array_flag = rhs.array_flag;
464 this->data = rhs.data;
475 if(this->array_flag && other.first !=
"")
throw json::parsing_error(
"Array cannot have key");
477 this->data.push_back(other);
484 if(this->array_flag != other.array_flag)
throw json::parsing_error(
"Array/object mismatch");
486 for (
size_t i = 0; i < copy.
size(); i++) {
524 output =
parse(input);
539 inline bool has_key(
const std::string &key)
const
541 if(this->array_flag)
return false;
542 for (
size_t i = 0; i < this->
size(); i++)
if (this->data.at(i).first == key)
return true;
553 void set(
const std::string &key,
const std::string &value);
560 inline std::string
get(
const size_t index)
const
562 return this->data.at(index).second;
571 inline std::string
get(
const std::string &key)
const
574 for (
size_t i = 0; i < this->
size(); i++)
if (this->data.at(i).first == key)
return this->
get(i);
583 void remove(
const std::string &key);
591 this->data.erase(this->data.begin() + index);
602 virtual const std::string&
ref()
const = 0;
613 return json::parsing::get_number<T>(this->
ref().c_str(), format);
626 std::vector<T> result;
627 for (
size_t i = 0; i < numbers.size(); i++)
629 result.push_back(json::parsing::get_number<T>(numbers[i].c_str(), format));
638 return json::jtype::peek(*this->
ref().c_str()) == json::jtype::jstring ?
644 inline operator std::string()
const
650 bool operator== (
const std::string other)
const {
return ((std::string)(*
this)) == other; }
653 bool operator!= (
const std::string other)
const {
return !(((std::string)(*
this)) == other); }
656 operator int()
const;
659 operator unsigned int()
const;
662 operator long()
const;
665 operator unsigned long()
const;
668 operator char()
const;
671 operator float()
const;
674 operator double()
const;
692 operator std::vector<int>()
const;
695 operator std::vector<unsigned int>()
const;
698 operator std::vector<long>()
const;
701 operator std::vector<unsigned long>()
const;
704 operator std::vector<char>()
const;
707 operator std::vector<float>()
const;
710 operator std::vector<double>()
const;
713 operator std::vector<json::jobject>()
const
716 std::vector<json::jobject> results;
717 for (
size_t i = 0; i < objs.size(); i++) {
733 return (std::vector<T>)(*this);
752 return type == json::jtype::jobject || type == json::jtype::jarray;
771 return (result.
type == json::jtype::jbool && result.
value ==
"true");
793 inline const std::string&
ref()
const
847 inline const std::string&
ref()
const
849 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;
861 if(source.array_flag)
throw std::logic_error(
"Source cannot be an array");
873 const char *value = this->
ref().c_str();
874 if(json::jtype::peek(*value) != json::jtype::jarray)
875 throw std::invalid_argument(
"Input is not an array");
909 void set_array(
const std::vector<std::string> &values,
const bool wrap =
false);
920 std::vector<std::string> numbers;
921 for (
size_t i = 0; i < values.size(); i++)
974 this->sink.
set(
key, (std::string)input);
1004 std::vector<std::string> objs;
1005 for (
size_t i = 0; i < input.size(); i++)
1007 objs.push_back((std::string)input[i]);
1018 if (value) this->sink.
set(
key,
"true");
1019 else this->sink.
set(
key,
"false");
1025 this->sink.
set(
key,
"null");
1072 operator std::string()
const;
1079 return this->
operator std::string();
1087 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:837
const_value array(size_t index) const
Returns another constant value from this array.
Definition: json.h:871
const std::string & ref() const
Returns a reference to the value.
Definition: json.h:847
const std::string key
The key for the referenced value.
Definition: json.h:844
const_proxy(const jobject &source, const std::string key)
Constructor.
Definition: json.h:859
Represents an entry as a constant value.
Definition: json.h:783
const_value(std::string value)
Constructs a proxy with the provided value.
Definition: json.h:803
const std::string & ref() const
Reference to the entry data.
Definition: json.h:793
const_value array(const size_t index) const
Returns another constant value from this array.
Definition: json.h:826
const_value get(const std::string &key) const
Returns another constant value from this object.
Definition: json.h:814
Representation of a value in the object.
Definition: json.h:596
bool operator==(const std::string other) const
Comparison operator.
Definition: json.h:650
bool is_array() const
Returns true if the value is an array.
Definition: json.h:756
T get_number(const char *format) const
Converts an serialzed value to a numeric value.
Definition: json.h:611
bool is_number() const
Returns true if the value is a number.
Definition: json.h:743
std::string as_string() const
Returns a string representation of the value.
Definition: json.h:636
bool is_null() const
Returns true if the value is a null value.
Definition: json.h:775
virtual const std::string & ref() const =0
A method for reference the entry's value.
bool is_object() const
Returns true if the value is an object.
Definition: json.h:749
json::jobject as_object() const
Casts the value as a JSON object.
Definition: json.h:680
bool is_string() const
Returns true if the value is a string.
Definition: json.h:737
bool is_true() const
Returns true if the value is a boolean and set to true.
Definition: json.h:768
std::vector< T > as_array() const
Casts an array.
Definition: json.h:731
bool operator!=(const std::string other) const
Comparison operator.
Definition: json.h:653
std::vector< T > get_number_array(const char *format) const
Converts a serialized array of numbers to a vector of numbers.
Definition: json.h:623
bool is_bool() const
Returns true if the value is a bool.
Definition: json.h:762
A proxy that allows modification of the value.
Definition: json.h:886
void operator=(const std::vector< unsigned int > input)
Assigns an array of unsigned integers.
Definition: json.h:981
proxy(jobject &source, const std::string key)
Constructor.
Definition: json.h:933
void set_array(const std::vector< std::string > &values, const bool wrap=false)
Stores an array of values.
Definition: json.cpp:874
void operator=(const std::vector< int > input)
Assigns an array of integers.
Definition: json.h:978
void set_null()
Definition: json.h:1023
void operator=(const std::vector< json::jobject > input)
Assigns an array of JSON objects.
Definition: json.h:1002
void operator=(const unsigned long input)
Assigns a long unsigned integer.
Definition: json.h:960
void operator=(const std::vector< long > input)
Assigns an array of long integers.
Definition: json.h:984
void operator=(const std::vector< unsigned long > input)
Assigns an array of unsigned long integers.
Definition: json.h:987
void operator=(const long input)
Assigns a long integer.
Definition: json.h:957
void operator=(const std::vector< std::string > input)
Assigns an array of strings.
Definition: json.h:999
void set_number(const T value, const char *format)
Sets a number value in the parent object.
Definition: json.h:899
void operator=(json::jobject input)
Assigns a JSON object or array.
Definition: json.h:972
void operator=(const char input)
Assigns an character.
Definition: json.h:963
void operator=(const std::vector< char > input)
Assigns an array of characters.
Definition: json.h:990
void operator=(const double input)
Assigns an double floating-point integer
Definition: json.h:966
void set_boolean(const bool value)
Sets a boolean value.
Definition: json.h:1016
void set_number_array(const std::vector< T > &values, const char *format)
Stores an array of numbers.
Definition: json.h:918
void operator=(const std::vector< float > input)
Assigns an array of floating-point numbers.
Definition: json.h:993
void operator=(const unsigned int input)
Assigns an unsigned integer.
Definition: json.h:954
void operator=(const std::vector< double > input)
Assigns an array of double floating-point numbers.
Definition: json.h:996
void operator=(const std::string value)
Assigns a string value.
Definition: json.h:939
void clear()
Definition: json.h:1029
void operator=(const float input)
Assigns an floating-point integer
Definition: json.h:969
void operator=(const int input)
Assigns an integer.
Definition: json.h:951
The class used for manipulating JSON objects and arrays.
Definition: json.h:410
static jobject parse(const char *input)
Parses a serialized JSON string.
Definition: json.cpp:887
void set(const std::string &key, const std::string &value)
Sets the value assocaited with the key.
Definition: json.cpp:944
static bool tryparse(const char *input, jobject &output)
Definition: json.h:520
virtual ~jobject()
Destructor.
Definition: json.h:437
bool operator!=(const json::jobject other) const
Comparison operator.
Definition: json.h:458
const jobject::const_value array(const size_t index) const
Returns the value of an element in an array.
Definition: json.h:1066
bool is_array() const
Flag for differentiating objects and arrays.
Definition: json.h:443
virtual const jobject::const_proxy operator[](const std::string key) const
Returns an element of the JSON object.
Definition: json.h:1053
size_t size() const
Returns the number of entries in the JSON object or array.
Definition: json.h:446
void clear()
Clears the JSON object or array.
Definition: json.h:449
std::string get(const size_t index) const
Returns the serialized value at a given index.
Definition: json.h:560
static jobject parse(const std::string input)
Parses a serialized JSON string.
Definition: json.h:512
jobject & operator+=(const jobject &other)
Appends one JSON object to another.
Definition: json.h:482
jobject(const jobject &other)
Copy constructor.
Definition: json.h:431
jobject(bool array=false)
Default constructor.
Definition: json.h:426
jobject & operator+=(const kvp &other)
Appends a key-value pair to a JSON object.
Definition: json.h:472
void remove(const size_t index)
Removes the entry at the specified index.
Definition: json.h:589
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:997
bool has_key(const std::string &key) const
Determines if an object contains a key.
Definition: json.h:539
std::string get(const std::string &key) const
Returns the serialized value associated with a key.
Definition: json.h:571
std::string as_string() const
Serialzes the object or array.
Definition: json.h:1077
void remove(const std::string &key)
Removes the entry associated with the key.
Definition: json.cpp:961
jobject & operator=(const jobject rhs)
Assignment operator.
Definition: json.h:461
jobject operator+(jobject &other)
Merges two JSON objects.
Definition: json.h:493
bool operator==(const json::jobject other) const
Comparison operator.
Definition: json.h:455
virtual jobject::proxy operator[](const std::string key)
Returns an element of the JSON object.
Definition: json.h:1041
Class for reading object key value pairs.
Definition: json.h:241
virtual void clear()
Resets the reader.
Definition: json.h:250
virtual bool is_valid() const
Checks if the stored value is valid.
Definition: json.h:267
kvp_reader()
Constructor.
Definition: json.h:244
virtual std::string readout() const
Reads out the key value pair.
Definition: json.cpp:666
virtual push_result push(const char next)
Definition: json.cpp:627
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
Value reader.
Definition: json.h:84
push_result push_null(const char next)
Pushes a character to a null value.
Definition: json.cpp:598
reader()
Reader constructor.
Definition: json.h:94
reader * sub_reader
The subreader used during reading.
Definition: json.h:150
array_reader_enum
Definition: json.h:218
@ ARRAY_AWAITING_NEXT_LINE
An array value has been read and a comma was encountered. Expecting new line.
Definition: json.h:222
@ ARRAY_EMPTY
No values have been read.
Definition: json.h:219
@ ARRAY_CLOSED
The array has been fully read. Reading should stop.
Definition: json.h:223
@ ARRAY_OPEN_BRACKET
The array has been opened.
Definition: json.h:220
@ ARRAY_READING_VALUE
An array value is being read.
Definition: json.h:221
push_result push_string(const char next)
Pushes a character to a string value.
Definition: json.cpp:222
T get_state() const
Returns the stored state.
Definition: json.h:175
void set_state(const T state)
Stores the reader state.
Definition: json.h:185
push_result push_number(const char next)
Pushes a character to a number value.
Definition: json.cpp:438
push_result
Definition: json.h:87
@ WHITESPACE
The character was whitespace. Reading should continue but the whtiespace was not stored.
Definition: json.h:90
@ REJECTED
The character was not valid. Reading should stop.
Definition: json.h:89
@ ACCEPTED
The character was valid. Reading should continue.
Definition: json.h:88
push_result push_object(const char next)
Pushes a character to an object value.
Definition: json.cpp:364
virtual push_result push(const char next)
Definition: json.cpp:112
virtual ~reader()
Destructor.
Definition: json.h:143
object_reader_enum
Definition: json.h:227
@ OBJECT_CLOSED
The object has been fully read. Reading should stop.
Definition: json.h:232
@ OBJECT_READING_ENTRY
An object key value pair is being read.
Definition: json.h:230
@ OBJECT_OPEN_BRACE
The object has been opened.
Definition: json.h:229
@ OBJECT_EMPTY
No values have been read.
Definition: json.h:228
@ OBJECT_AWAITING_NEXT_LINE
An object key value pair has been read and a comma was encountered. Expecting new line.
Definition: json.h:231
string_reader_enum
Enumeration of the state machine for strings.
Definition: json.h:192
@ STRING_OPEN
The opening quote has been read and the last character was not an escape character.
Definition: json.h:195
@ STRING_CODE_POINT_1
An encoded unicode character is encountered. Expecting three following hex digits (one has already be...
Definition: json.h:198
@ STRING_OPENING_QUOTE
The opening quote has been read. Equivalant to STRING_OPEN, but used for debugging the state.
Definition: json.h:194
@ STRING_CLOSED
The closing quote has been read. Reading should cease.
Definition: json.h:201
@ STRING_CODE_POINT_3
An encoded unicode character is encountered. Expecting one following hex digit (three has already bee...
Definition: json.h:200
@ STRING_ESCAPED
The last character was an reverse solidus (), indicating the next character should be a control chara...
Definition: json.h:196
@ STRING_EMPTY
No values have been read.
Definition: json.h:193
@ STRING_CODE_POINT_2
An encoded unicode character is encountered. Expecting two following hex digits (two have already bee...
Definition: json.h:199
@ STRING_CODE_POINT_START
An encoded unicode character is encountered. Expecting four following hex digits.
Definition: json.h:197
push_result push_boolean(const char next)
Pushes a character to a boolean value.
Definition: json.cpp:523
virtual void clear()
Resets the reader.
Definition: json.cpp:102
virtual std::string readout() const
Returns the stored value.
Definition: json.h:140
number_reader_enum
Definition: json.h:205
@ NUMBER_EXPONENT_SIGN
An exponent sign has been read.
Definition: json.h:213
@ NUMBER_EMPTY
No values have been read.
Definition: json.h:206
@ NUMBER_INTEGER_DIGITS
Integer digits were the last values read.
Definition: json.h:209
@ NUMBER_EXPONENT
An exponent indicator has been read.
Definition: json.h:212
@ NUMBER_OPEN_NEGATIVE
A negative value has been read as the first character.
Definition: json.h:207
@ NUMBER_FRACTION_DIGITS
A decimal point and subsequent digits were the last values read.
Definition: json.h:211
@ NUMBER_DECIMAL
A decimal point was the last value read.
Definition: json.h:210
@ NUMBER_ZERO
A zero has been read as an integer value.
Definition: json.h:208
@ NUMBER_EXPONENT_DIGITS
An exponent indicator and subsequent digits were the last values read.
Definition: json.h:214
virtual jtype::jtype type() const
Checks the value.
Definition: json.h:124
push_result push_array(const char next)
Pushes a character to an array value.
Definition: json.cpp:296
virtual bool is_valid() const
Checks if the stored value is valid.
Definition: json.cpp:166
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:57
std::string read_digits(const char *input)
Reads a set of digits from a string.
Definition: json.cpp:671
parse_results parse(const char *input)
Parses the first value encountered in a JSON string.
Definition: json.cpp:795
std::string decode_string(const char *input)
Decodes a string in JSON format.
Definition: json.cpp:704
std::vector< std::string > parse_array(const char *input)
Parses a JSON array.
Definition: json.cpp:825
T get_number(const char *input, const char *format)
Template for reading a numeric value.
Definition: json.h:356
std::string encode_string(const char *input)
Encodes a string in JSON format.
Definition: json.cpp:756
std::string get_number_string(const T &number, const char *format)
Converts a number to a string.
Definition: json.h:371
Base namespace for simpleson.
Definition: json.h:18
std::pair< std::string, std::string > kvp
(k)ey (v)alue (p)air
Definition: json.h:395
Structure for capturing the results of parsing.
Definition: json.h:329
std::string value
The parsed value encountered.
Definition: json.h:334
const char * remainder
A pointer to the first character after the parsed value.
Definition: json.h:337
jtype::jtype type
The type of value encountered while parsing.
Definition: json.h:331