84 static JsonValue from_json(
const std::string &json);
87 JsonValue() : type(type_null), value_number(), value_boolean() { }
89 JsonValue(
const std::string &value) : type(type_string), value_string(value), value_number(), value_boolean() { }
90 JsonValue(
int value) : type(type_number), value_number((double)value), value_boolean() { }
91 JsonValue(
double value) : type(type_number), value_number(value), value_boolean() { }
92 explicit JsonValue(
bool value) : type(type_boolean), value_number(), value_boolean(value) { }
99 operator bool()
const {
return to_boolean(); }
100 operator std::string()
const {
return to_string(); }
101 operator double()
const {
return to_double(); }
102 operator int()
const {
return to_int(); }
118 case type_object:
return members.size();
119 case type_array:
return items.size();
120 case type_string:
return value_string.size();
126 std::map<std::string, JsonValue> &
get_members() {
return members; }
127 const std::map<std::string, JsonValue> &
get_members()
const {
return members; }
131 const std::vector<JsonValue> &
get_items()
const {
return items; }
134 bool is_null()
const {
return type == type_null; }
140 bool is_array()
const {
return type == type_array; }
152 std::string
to_string()
const {
if (type != type_string)
throw JsonException(
"JSON Value is not a string");
return value_string; }
155 int to_int()
const {
if (type != type_number)
throw JsonException(
"JSON Value is not a number");
return (
int)value_number; }
158 float to_float()
const {
if (type != type_number)
throw JsonException(
"JSON Value is not a number");
return (
float)value_number; }
161 double to_double()
const {
if (type != type_number)
throw JsonException(
"JSON Value is not a number");
return value_number; }
164 bool to_boolean()
const {
if (type != type_boolean)
throw JsonException(
"JSON Value is not a boolean");
return value_boolean; }
178 template<
typename Type>
179 std::map<std::string, Type>
to_map()
const 181 if (type != type_object)
184 std::map<std::string, Type> object;
185 std::map<std::string, JsonValue>::const_iterator it;
186 for (it = members.begin(); it != members.end(); ++it)
187 object[it->first] = it->second;
192 template<
typename Type>
195 if (type != type_array)
198 std::vector<Type> list;
199 list.reserve(items.size());
200 for (
size_t i = 0; i < items.size(); i++)
201 list.push_back(items[i]);
206 std::string to_json()
const;
207 void to_json(std::string &result)
const;
213 void write(std::string &json)
const;
214 void write_array(std::string &json)
const;
215 void write_object(std::string &json)
const;
216 static void write_string(
const std::string &str, std::string &json);
217 void write_number(std::string &json)
const;
219 static JsonValue read(
const std::string &json,
size_t &pos);
220 static JsonValue read_object(
const std::string &json,
size_t &pos);
221 static JsonValue read_array(
const std::string &json,
size_t &pos);
222 static std::string read_string(
const std::string &json,
size_t &pos);
223 static JsonValue read_number(
const std::string &json,
size_t &pos);
224 static JsonValue read_boolean(
const std::string &json,
size_t &pos);
225 static void read_whitespace(
const std::string &json,
size_t &pos);
228 std::map<std::string, JsonValue> members;
229 std::vector<JsonValue> items;
230 std::string value_string;
static JsonValue string(const std::string &value)
Create a string value.
Definition: json_value.h:74
static JsonValue number(double value)
Definition: json_value.h:81
bool is_number() const
Return true if value is a number.
Definition: json_value.h:146
std::map< std::string, Type > to_map() const
Convert value object to a std::map with the template specified value type.
Definition: json_value.h:179
JsonException(const std::string &message)
Definition: json_value.h:43
JsonValue & operator[](const std::string &key)
Definition: json_value.h:106
int to_int() const
Convert value object to an int.
Definition: json_value.h:155
bool is_object() const
Return true if value is an object.
Definition: json_value.h:137
const std::map< std::string, JsonValue > & get_members() const
Definition: json_value.h:127
std::string message
Description of exception.
Definition: exception.h:59
Definition: json_value.h:54
JsonValue()
Constructs a value.
Definition: json_value.h:87
JsonValue(double value)
Definition: json_value.h:91
Definition: json_value.h:56
static JsonValue boolean(bool value)
Create a boolean value.
Definition: json_value.h:77
JsonValue(Type type)
Definition: json_value.h:88
bool is_null() const
Return true if value is null.
Definition: json_value.h:134
Definition: json_value.h:53
Definition: json_value.h:55
std::string to_string() const
Convert value object to a string.
Definition: json_value.h:152
Exception class thrown for JSON exceptions.
Definition: json_value.h:40
Top-level exception class.
Definition: exception.h:43
size_t get_size() const
Get size of value.
Definition: json_value.h:114
Class representing a JSON value.
Definition: json_value.h:47
static JsonValue object()
Create a object value.
Definition: json_value.h:65
const JsonValue & operator[](int index) const
Definition: json_value.h:107
bool to_boolean() const
Convert value object to a boolean.
Definition: json_value.h:164
float to_float() const
Convert value object to a float.
Definition: json_value.h:158
bool is_string() const
Return true if value is a string.
Definition: json_value.h:143
JsonValue & operator[](const char *key)
Indexers for object members or array items.
Definition: json_value.h:105
static JsonValue null()
Create a null value.
Definition: json_value.h:71
Definition: json_value.h:57
Type
value type
Definition: json_value.h:51
double to_double() const
Convert value object to a double.
Definition: json_value.h:161
std::vector< Type > to_vector() const
Convert value array to a std::vector with the template specified value type.
Definition: json_value.h:193
JsonValue & operator[](int index)
Definition: json_value.h:108
static JsonValue number(int value)
Create a number value.
Definition: json_value.h:80
bool is_array() const
Return true if value is an array.
Definition: json_value.h:140
JsonValue(bool value)
Definition: json_value.h:92
JsonValue(int value)
Definition: json_value.h:90
JsonValue(const std::string &value)
Definition: json_value.h:89
Type get_type() const
Get value type.
Definition: json_value.h:111
bool is_boolean() const
Return true if value is a boolean.
Definition: json_value.h:149
const std::vector< JsonValue > & get_items() const
Definition: json_value.h:131
std::vector< JsonValue > & get_items()
Get array items.
Definition: json_value.h:130
static JsonValue array()
Create a array value.
Definition: json_value.h:68
std::map< std::string, JsonValue > & get_members()
Get object members.
Definition: json_value.h:126