Simpleson
json.h
Go to the documentation of this file.
1#ifndef JSON_H
2#define JSON_H
3
8#include <cstdlib>
9#include <string>
10#include <vector>
11#include <cstdio>
12#include <utility>
13#include <stdexcept>
14#include <cctype>
15
17namespace json
18{
20 class invalid_key : public std::exception
21 {
22 public:
24 const std::string key;
25
30 inline invalid_key(const std::string &key) : key(key) { }
31
33 inline virtual ~invalid_key() throw() { }
34
36 virtual const char* what() const throw()
37 {
38 return key.c_str();
39 }
40 };
41
43 class parsing_error : public std::invalid_argument
44 {
45 public:
50 inline parsing_error(const char *message) : std::invalid_argument(message) { }
51
53 inline virtual ~parsing_error() throw() { }
54 };
55
56 /* \brief Namespace for handling of JSON data types */
57 namespace jtype
58 {
60 enum jtype {
68 };
69
70 jtype peek(const char input);
71
79 jtype detect(const char *input);
80 }
81
83 class reader : protected std::string
84 {
85 public:
87 {
91 };
92
94 inline reader() : std::string(), sub_reader(NULL) { this->clear(); }
95
97 virtual void clear();
98
100 using std::string::length;
101
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); }
105 #else
107 using std::string::front;
108
110 using std::string::back;
111 #endif
112
118 virtual push_result push(const char next);
119
124 inline virtual jtype::jtype type() const
125 {
126 return this->length() > 0 ? jtype::peek(this->front()) : json::jtype::not_valid;
127 }
128
133 virtual bool is_valid() const;
134
140 inline virtual std::string readout() const { return *this; }
141
143 inline virtual ~reader() { this->clear(); }
144
145 protected:
151
153 push_result push_string(const char next);
154
156 push_result push_array(const char next);
157
159 push_result push_object(const char next);
160
162 push_result push_number(const char next);
163
165 push_result push_boolean(const char next);
166
168 push_result push_null(const char next);
169
174 template<typename T>
175 T get_state() const
176 {
177 return static_cast<T>(this->read_state);
178 }
179
184 template<typename T>
185 void set_state(const T state)
186 {
187 this->read_state = (char)state;
188 }
189
192 {
202 };
203
205 {
215 };
216
218 {
224 };
225
227 {
233 };
234 private:
236 char read_state;
237 };
238
240 class kvp_reader : public reader
241 {
242 public:
244 inline kvp_reader() : reader()
245 {
246 this->clear();
247 }
248
250 inline virtual void clear()
251 {
253 this->_key.clear();
254 this->_colon_read = false;
255 }
256
261 virtual push_result push(const char next);
262
267 inline virtual bool is_valid() const
268 {
269 return reader::is_valid() && this->_key.is_valid();
270 }
271
276 virtual std::string readout() const;
277
278 private:
280 reader _key;
281
283 bool _colon_read;
284 };
285
287 namespace parsing
288 {
297 const char* tlws(const char *start);
298
305 std::string read_digits(const char *input);
306
315 std::string decode_string(const char * input);
316
325 std::string encode_string(const char *input);
326
329 {
332
334 std::string value;
335
337 const char *remainder;
338 };
339
346 parse_results parse(const char *input);
347
355 template <typename T>
356 T get_number(const char *input, const char* format)
357 {
358 T result;
359 std::sscanf(input, format, &result);
360 return result;
361 }
362
370 template <typename T>
371 std::string get_number_string(const T &number, const char *format)
372 {
373 std::vector<char> cstr(6);
374 int remainder = std::snprintf(&cstr[0], cstr.size(), format, number);
375 if(remainder < 0) {
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);
380 }
381 std::string result(&cstr[0]);
382 return result;
383 }
384
391 std::vector<std::string> parse_array(const char *input);
392 }
393
395 typedef std::pair<std::string, std::string> kvp;
396
410 {
411 private:
413 std::vector<kvp> data;
414
419 bool array_flag;
420
421 public:
426 inline jobject(bool array = false)
427 : array_flag(array)
428 { }
429
431 inline jobject(const jobject &other)
432 : data(other.data),
433 array_flag(other.array_flag)
434 { }
435
437 inline virtual ~jobject() { }
438
443 bool is_array() const { return this->array_flag; }
444
446 inline size_t size() const { return this->data.size(); }
447
449 inline void clear() { this->data.resize(0); }
450
455 bool operator== (const json::jobject other) const { return ((std::string)(*this)) == (std::string)other; }
456
458 bool operator!= (const json::jobject other) const { return ((std::string)(*this)) != (std::string)other; }
459
461 inline jobject& operator=(const jobject rhs)
462 {
463 this->array_flag = rhs.array_flag;
464 this->data = rhs.data;
465 return *this;
466 }
467
472 jobject& operator+=(const kvp& other)
473 {
474 if (!this->array_flag && this->has_key(other.first)) throw json::parsing_error("Key conflict");
475 if(this->array_flag && other.first != "") throw json::parsing_error("Array cannot have key");
476 if(!this->array_flag && other.first == "") throw json::parsing_error("Missing key");
477 this->data.push_back(other);
478 return *this;
479 }
480
483 {
484 if(this->array_flag != other.array_flag) throw json::parsing_error("Array/object mismatch");
485 json::jobject copy(other);
486 for (size_t i = 0; i < copy.size(); i++) {
487 this->operator+=(copy.data.at(i));
488 }
489 return *this;
490 }
491
494 {
495 jobject result = *this;
496 result += other;
497 return result;
498 }
499
506 static jobject parse(const char *input);
507
512 static inline jobject parse(const std::string input) { return parse(input.c_str()); }
513
520 inline bool static tryparse(const char *input, jobject &output)
521 {
522 try
523 {
524 output = parse(input);
525 }
526 catch(...)
527 {
528 return false;
529 }
530 return true;
531 }
532
539 inline bool has_key(const std::string &key) const
540 {
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;
543 return false;
544 }
545
553 void set(const std::string &key, const std::string &value);
554
560 inline std::string get(const size_t index) const
561 {
562 return this->data.at(index).second;
563 }
564
571 inline std::string get(const std::string &key) const
572 {
573 if(this->array_flag) throw json::invalid_key(key);
574 for (size_t i = 0; i < this->size(); i++) if (this->data.at(i).first == key) return this->get(i);
575 throw json::invalid_key(key);
576 }
577
583 void remove(const std::string &key);
584
589 void remove(const size_t index)
590 {
591 this->data.erase(this->data.begin() + index);
592 }
593
595 class entry
596 {
597 protected:
602 virtual const std::string& ref() const = 0;
603
610 template<typename T>
611 inline T get_number(const char* format) const
612 {
613 return json::parsing::get_number<T>(this->ref().c_str(), format);
614 }
615
622 template<typename T>
623 inline std::vector<T> get_number_array(const char* format) const
624 {
625 std::vector<std::string> numbers = json::parsing::parse_array(this->ref().c_str());
626 std::vector<T> result;
627 for (size_t i = 0; i < numbers.size(); i++)
628 {
629 result.push_back(json::parsing::get_number<T>(numbers[i].c_str(), format));
630 }
631 return result;
632 }
633
634 public:
636 inline std::string as_string() const
637 {
638 return json::jtype::peek(*this->ref().c_str()) == json::jtype::jstring ?
639 json::parsing::decode_string(this->ref().c_str()) :
640 this->ref();
641 }
642
644 inline operator std::string() const
645 {
646 return this->as_string();
647 }
648
650 bool operator== (const std::string other) const { return ((std::string)(*this)) == other; }
651
653 bool operator!= (const std::string other) const { return !(((std::string)(*this)) == other); }
654
656 operator int() const;
657
659 operator unsigned int() const;
660
662 operator long() const;
663
665 operator unsigned long() const;
666
668 operator char() const;
669
671 operator float() const;
672
674 operator double() const;
675
681 {
682 return json::jobject::parse(this->ref().c_str());
683 }
684
686 inline operator json::jobject() const
687 {
688 return this->as_object();
689 }
690
692 operator std::vector<int>() const;
693
695 operator std::vector<unsigned int>() const;
696
698 operator std::vector<long>() const;
699
701 operator std::vector<unsigned long>() const;
702
704 operator std::vector<char>() const;
705
707 operator std::vector<float>() const;
708
710 operator std::vector<double>() const;
711
713 operator std::vector<json::jobject>() const
714 {
715 const std::vector<std::string> objs = json::parsing::parse_array(this->ref().c_str());
716 std::vector<json::jobject> results;
717 for (size_t i = 0; i < objs.size(); i++) {
718 results.push_back(json::jobject::parse(objs[i].c_str()));
719 }
720 return results;
721 }
722
724 operator std::vector<std::string>() const { return json::parsing::parse_array(this->ref().c_str()); }
725
730 template<typename T>
731 inline std::vector<T> as_array() const
732 {
733 return (std::vector<T>)(*this);
734 }
735
737 inline bool is_string() const
738 {
739 return json::parsing::parse(this->ref().c_str()).type == json::jtype::jstring;
740 }
741
743 inline bool is_number() const
744 {
745 return json::parsing::parse(this->ref().c_str()).type == json::jtype::jnumber;
746 }
747
749 inline bool is_object() const
750 {
751 const jtype::jtype type = json::parsing::parse(this->ref().c_str()).type;
752 return type == json::jtype::jobject || type == json::jtype::jarray;
753 }
754
756 inline bool is_array() const
757 {
758 return json::parsing::parse(this->ref().c_str()).type == json::jtype::jarray;
759 }
760
762 inline bool is_bool() const
763 {
764 return json::parsing::parse(this->ref().c_str()).type == json::jtype::jbool;
765 }
766
768 inline bool is_true() const
769 {
770 json::parsing::parse_results result = json::parsing::parse(this->ref().c_str());
771 return (result.type == json::jtype::jbool && result.value == "true");
772 }
773
775 inline bool is_null() const
776 {
777 return json::parsing::parse(this->ref().c_str()).type == json::jtype::jnull;
778 }
779 };
780
782 class const_value : public entry
783 {
784 private:
786 std::string data;
787
788 protected:
793 inline const std::string& ref() const
794 {
795 return this->data;
796 }
797
798 public:
803 inline const_value(std::string value)
804 : data(value)
805 { }
806
814 inline const_value get(const std::string &key) const
815 {
816 return const_value(json::jobject::parse(this->data).get(key));
817 }
818
826 inline const_value array(const size_t index) const
827 {
828 return const_value(json::jobject::parse(this->data).get(index));
829 }
830 };
831
836 class const_proxy : public entry
837 {
838 private:
840 const jobject &source;
841
842 protected:
844 const std::string key;
845
847 inline const std::string& ref() const
848 {
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;
850 throw json::invalid_key(key);
851 }
852
853 public:
859 const_proxy(const jobject &source, const std::string key) : source(source), key(key)
860 {
861 if(source.array_flag) throw std::logic_error("Source cannot be an array");
862 }
863
871 const_value array(size_t index) const
872 {
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");
876 const std::vector<std::string> values = json::parsing::parse_array(value);
877 return const_value(values[index]);
878 }
879 };
880
886 {
887 private:
889 jobject &sink;
890
891 protected:
898 template<typename T>
899 inline void set_number(const T value, const char* format)
900 {
901 this->sink.set(key, json::parsing::get_number_string(value, format));
902 }
903
909 void set_array(const std::vector<std::string> &values, const bool wrap = false);
910
917 template<typename T>
918 inline void set_number_array(const std::vector<T> &values, const char* format)
919 {
920 std::vector<std::string> numbers;
921 for (size_t i = 0; i < values.size(); i++)
922 {
923 numbers.push_back(json::parsing::get_number_string(values[i], format));
924 }
925 this->set_array(numbers);
926 }
927 public:
933 proxy(jobject &source, const std::string key)
934 : json::jobject::const_proxy(source, key),
935 sink(source)
936 { }
937
939 inline void operator= (const std::string value)
940 {
941 this->sink.set(this->key, json::parsing::encode_string(value.c_str()));
942 }
943
945 inline void operator= (const char* value)
946 {
947 this->operator=(std::string(value));
948 }
949
951 void operator=(const int input) { this->set_number(input, "%i"); }
952
954 void operator=(const unsigned int input) { this->set_number(input, "%u"); }
955
957 void operator=(const long input) { this->set_number(input, "%li"); }
958
960 void operator=(const unsigned long input) { this->set_number(input, "%lu"); }
961
963 void operator=(const char input) { this->set_number(input, "%c"); }
964
966 void operator=(const double input) { this->set_number(input, "%e"); }
967
969 void operator=(const float input) { this->set_number(input, "%e"); }
970
973 {
974 this->sink.set(key, (std::string)input);
975 }
976
978 void operator=(const std::vector<int> input) { this->set_number_array(input, "%i"); }
979
981 void operator=(const std::vector<unsigned int> input) { this->set_number_array(input, "%u"); }
982
984 void operator=(const std::vector<long> input) { this->set_number_array(input, "%li"); }
985
987 void operator=(const std::vector<unsigned long> input) { this->set_number_array(input, "%lu"); }
988
990 void operator=(const std::vector<char> input) { this->set_number_array(input, "%c"); }
991
993 void operator=(const std::vector<float> input) { this->set_number_array(input, "%e"); }
994
996 void operator=(const std::vector<double> input) { this->set_number_array(input, "%e"); }
997
999 void operator=(const std::vector<std::string> input) { this->set_array(input, true); }
1000
1002 void operator=(const std::vector<json::jobject> input)
1003 {
1004 std::vector<std::string> objs;
1005 for (size_t i = 0; i < input.size(); i++)
1006 {
1007 objs.push_back((std::string)input[i]);
1008 }
1009 this->set_array(objs, false);
1010 }
1011
1016 inline void set_boolean(const bool value)
1017 {
1018 if (value) this->sink.set(key, "true");
1019 else this->sink.set(key, "false");
1020 }
1021
1023 inline void set_null()
1024 {
1025 this->sink.set(key, "null");
1026 }
1027
1029 inline void clear()
1030 {
1031 this->sink.remove(key);
1032 }
1033 };
1034
1041 inline virtual jobject::proxy operator[](const std::string key)
1042 {
1043 if(this->array_flag) throw json::invalid_key(key);
1044 return jobject::proxy(*this, key);
1045 }
1046
1053 inline virtual const jobject::const_proxy operator[](const std::string key) const
1054 {
1055 if(this->array_flag) throw json::invalid_key(key);
1056 return jobject::const_proxy(*this, key);
1057 }
1058
1066 inline const jobject::const_value array(const size_t index) const
1067 {
1068 return jobject::const_value(this->data.at(index).second);
1069 }
1070
1072 operator std::string() const;
1073
1077 inline std::string as_string() const
1078 {
1079 return this->operator std::string();
1080 }
1081
1087 std::string pretty(unsigned int indent_level = 0) const;
1088 };
1089}
1090
1091#endif // !JSON_H
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