1 //////////////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included 13 // in all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 // 23 // http://www.opensource.org/licenses/mit-license.php 24 // 25 //////////////////////////////////////////////////////////////////////////////// 26 27 #ifndef INCLUDED_JSON 28 #define INCLUDED_JSON 29 30 #include <map> 31 #include <vector> 32 #include <string> 33 #include <Pig.h> 34 35 namespace json 36 { 37 enum jtype 38 { 39 j_value, // 0 40 j_object, // 1 41 j_array, // 2 42 j_string, // 3 43 j_number, // 4 44 j_literal // 5 45 }; 46 47 class value 48 { 49 public: value()50 value () {} ~value()51 virtual ~value () {} 52 static value* parse (Pig&); 53 virtual jtype type (); 54 virtual std::string dump () const; 55 }; 56 57 class string : public value 58 { 59 public: string()60 string () {} 61 string (const std::string&); ~string()62 ~string () {} 63 static string* parse (Pig&); 64 jtype type (); 65 std::string dump () const; 66 67 public: 68 std::string _data; 69 }; 70 71 class number : public value 72 { 73 public: number()74 number () : _dvalue (0.0) {} ~number()75 ~number () {} 76 static number* parse (Pig&); 77 jtype type (); 78 std::string dump () const; 79 operator double () const; 80 81 public: 82 double _dvalue; 83 }; 84 85 class literal : public value 86 { 87 public: literal()88 literal () : _lvalue (none) {} ~literal()89 ~literal () {} 90 static literal* parse (Pig&); 91 jtype type (); 92 std::string dump () const; 93 94 public: 95 enum literal_value {none, nullvalue, falsevalue, truevalue}; 96 literal_value _lvalue; 97 }; 98 99 class array : public value 100 { 101 public: array()102 array () {} 103 ~array (); 104 static array* parse (Pig&); 105 jtype type (); 106 std::string dump () const; 107 108 public: 109 std::vector <value*> _data; 110 }; 111 112 class object : public value 113 { 114 public: object()115 object () {} 116 ~object (); 117 static object* parse (Pig&); 118 static bool parse_pair (Pig&, std::string&, value*&); 119 jtype type (); 120 std::string dump () const; 121 122 public: 123 std::map <std::string, value*> _data; 124 }; 125 126 // Parser entry point. 127 value* parse (const std::string&); 128 129 // Encode/decode for JSON entities. 130 std::string encode (const std::string&); 131 std::string decode (const std::string&); 132 133 class SAX 134 { 135 public: 136 class Sink 137 { 138 public: eventDocStart()139 virtual void eventDocStart () {} eventDocEnd()140 virtual void eventDocEnd () {} eventObjectStart()141 virtual void eventObjectStart () {} eventObjectEnd(int)142 virtual void eventObjectEnd (int) {} eventArrayStart()143 virtual void eventArrayStart () {} eventArrayEnd(int)144 virtual void eventArrayEnd (int) {} eventName(const std::string &)145 virtual void eventName (const std::string&) {} eventValueNull()146 virtual void eventValueNull () {} eventValueBool(bool)147 virtual void eventValueBool (bool) {} eventValueInt(int64_t)148 virtual void eventValueInt (int64_t) {} eventValueUint(uint64_t)149 virtual void eventValueUint (uint64_t) {} eventValueDouble(double)150 virtual void eventValueDouble (double) {} eventValueString(const std::string &)151 virtual void eventValueString (const std::string&) {} 152 }; 153 154 bool parse (const std::string&, json::SAX::Sink&); 155 156 private: 157 void ignoreWhitespace (const std::string&, std::string::size_type&); 158 bool isObject (const std::string&, std::string::size_type&, SAX::Sink&); 159 bool isArray (const std::string&, std::string::size_type&, SAX::Sink&); 160 bool isPair (const std::string&, std::string::size_type&, SAX::Sink&); 161 bool isValue (const std::string&, std::string::size_type&, SAX::Sink&); 162 bool isKey (const std::string&, std::string::size_type&, SAX::Sink&); 163 bool isString (const std::string&, std::string::size_type&, SAX::Sink&); 164 bool isStringValue (const std::string&, std::string::size_type&, std::string&); 165 bool isNumber (const std::string&, std::string::size_type&, SAX::Sink&); 166 bool isInt (const std::string&, std::string::size_type&, std::string&); 167 bool isFrac (const std::string&, std::string::size_type&, std::string&); 168 bool isDigits (const std::string&, std::string::size_type&); 169 bool isDecDigit (int); 170 bool isHexDigit (int); 171 int hexToInt (int); 172 int hexToInt (int, int, int, int); 173 bool isExp (const std::string&, std::string::size_type&, std::string&); 174 bool isE (const std::string&, std::string::size_type&); 175 bool isBool (const std::string&, std::string::size_type&, SAX::Sink&); 176 bool isNull (const std::string&, std::string::size_type&, SAX::Sink&); 177 bool isLiteral (const std::string&, char, std::string::size_type&); 178 void error (const std::string&, std::string::size_type); 179 }; 180 } 181 182 #endif 183 //////////////////////////////////////////////////////////////////////////////// 184