00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _SOCKETS_Json_H
00032 #define _SOCKETS_Json_H
00033
00034 #include "Exception.h"
00035 #include <list>
00036 #include <map>
00037
00038 #ifdef _WIN32
00039 #ifdef GetObject
00040 #undef GetObject
00041 #endif
00042 #endif
00043
00044 #ifdef SOCKETS_NAMESPACE
00045 namespace SOCKETS_NAMESPACE {
00046 #endif
00047
00048 class Json
00049 {
00050 public:
00051 typedef std::list<Json> json_list_t;
00052 typedef std::map<std::string, Json> json_map_t;
00053
00054 typedef enum {
00055 TYPE_UNKNOWN = 1,
00056 TYPE_INTEGER,
00057 TYPE_REAL,
00058 TYPE_STRING,
00059 TYPE_BOOLEAN,
00060 TYPE_ARRAY,
00061 TYPE_OBJECT } json_type_t;
00062
00063 public:
00065 Json();
00066
00068 Json(char value);
00069 Json(short value);
00070 Json(long value);
00071 Json(double value);
00072 Json(const char *value);
00073 Json(const std::string& value);
00074 Json(bool value);
00075
00077 Json(json_type_t t);
00078
00079 virtual ~Json();
00080
00081 char Parse(const char *buffer, size_t& index);
00082
00083 json_type_t Type() const;
00084
00085 bool HasValue(const std::string& name) const;
00086
00087 operator char() const;
00088 operator short() const;
00089 operator long() const;
00090 operator double() const;
00091 operator std::string() const;
00092 operator bool() const;
00093
00094 const Json& operator[](const char *name) const;
00095 Json& operator[](const char *name);
00096
00097 const Json& operator[](const std::string& name) const;
00098 Json& operator[](const std::string& name);
00099
00100 void Add(Json data);
00101
00102 const std::string& GetString() const;
00103
00104 const json_list_t& GetArray() const;
00105 json_list_t& GetArray();
00106
00107 const json_map_t& GetObject() const;
00108 json_map_t& GetObject();
00109
00110 std::string ToString(bool quote = true) const;
00111
00112 static Json Parse(const std::string& data);
00113
00114 void encode(std::string& src) const;
00115 void decode(std::string& src) const;
00116 std::string encode(const std::string&) const;
00117
00118 private:
00119 virtual int Token(const char *buffer, size_t& index, std::string& ord);
00120
00121 json_type_t m_type;
00122 long m_i_value;
00123 double m_d_value;
00124 std::string m_str_value;
00125 bool m_b_value;
00126 json_list_t m_array;
00127 json_map_t m_object;
00128 };
00129
00130 #ifdef SOCKETS_NAMESPACE
00131 }
00132 #endif
00133
00134 #endif // _SOCKETS_Json_H