spirit2json
A JSON parser/generator written with boost spirit
spirit2json/spirit2json.h
Go to the documentation of this file.
00001 
00043 #ifndef SPIRIT2JSON_H
00044 #define SPIRIT2JSON_H
00045 
00046 #include <string>
00047 #include <stdexcept>
00048 #include <ostream>
00049 #include <vector>
00050 #include <map>
00051 #include <cstddef>
00052 
00053 #include <boost/version.hpp>
00054 #include <boost/variant.hpp>
00055 #include <boost/config.hpp>
00056 
00057 #if !defined(BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT) \
00058         && BOOST_VERSION < 104700 // Work around incorrect recursive_variant_ definition in older boosts
00059 struct boost::recursive_variant_ {};
00060 #endif
00061 
00065 namespace spirit2json {
00066 
00070 #if defined(BOOST_NO_NULLPTR) // C++03 compat
00071 class JSONNull {
00072 public:
00073         template<class T>
00074         operator T*() const { return 0; }
00075 
00076         template <class C, class T>
00077         operator T C::*() const { return 0; }
00078 
00079         bool operator==(const JSONNull&) const { return true; }
00080 private:
00081         void operator &() const;
00082 };
00083 #else
00084 typedef std::nullptr_t JSONNull;
00085 #endif
00086 
00091 enum JSONValueTypes
00092 {
00093         JSON_STRING, 
00094         JSON_NUMBER, 
00095         JSON_BOOL,   
00096         JSON_NULL,   
00097         JSON_ARRAY,  
00098         JSON_OBJECT  
00099 };
00100 
00102 typedef std::wstring JSONString;
00104 typedef double JSONNumber;
00106 typedef bool JSONBool;
00107 
00127 typedef boost::make_recursive_variant<
00128         JSONString,
00129         JSONNumber,
00130         JSONBool,
00131         JSONNull,
00132         std::vector<boost::recursive_variant_ >,
00133         std::map<JSONString, boost::recursive_variant_ > >::type JSONValue;
00134 
00152 typedef std::vector<JSONValue> JSONArray;
00153 
00170 typedef std::map<JSONString, JSONValue> JSONObject;
00171 
00173 typedef JSONObject::value_type JSONObjectPair;
00174 
00178 class Exception : public std::exception {
00179         virtual const char* what() const throw() {
00180                 return "spirit2json: Exception";
00181         }
00182 };
00183 
00187 class ParsingFailed : public Exception {
00188         virtual const char* what() const throw() {
00189                 return "spirit2json: Failed to parse given json";
00190         }
00191 };
00192 
00199 JSONValue parse(JSONString str);
00200 
00206 JSONString generate(JSONValue& val);
00207 
00208 }
00209 
00216 std::wostream& operator<<(std::wostream& output, spirit2json::JSONValue& val);
00217 
00218 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator