json_value.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include <map>
32 #include <vector>
33 
34 namespace clan
35 {
38 
40  class JsonException : public Exception
41  {
42  public:
43  JsonException(const std::string &message) : Exception(message) { }
44  };
45 
46  enum class JsonType
47  {
48  undefined,
49  null,
50  object,
51  array,
52  number,
53  boolean,
54  string
55  };
56 
57  class JsonValue
58  {
59  public:
60  static JsonValue undefined() { JsonValue v; v._type = JsonType::undefined; return v; }
61  static JsonValue null() { JsonValue v; v._type = JsonType::null; return v; }
62  static JsonValue object() { JsonValue v; v._type = JsonType::object; return v; }
63  static JsonValue array() { JsonValue v; v._type = JsonType::array; return v; }
64  static JsonValue number(double value) { JsonValue v; v._type = JsonType::number; v._number = value; return v; }
65  static JsonValue number(float value) { return JsonValue::number(static_cast<double>(value)); }
66  static JsonValue number(int value) { return JsonValue::number(static_cast<double>(value)); }
67  static JsonValue number(unsigned int value) { return JsonValue::number(static_cast<double>(value)); }
68  static JsonValue number(short value) { return JsonValue::number(static_cast<double>(value)); }
69  static JsonValue number(unsigned short value) { return JsonValue::number(static_cast<double>(value)); }
70  static JsonValue number(char value) { return JsonValue::number(static_cast<double>(value)); }
71  static JsonValue number(unsigned char value) { return JsonValue::number(static_cast<double>(value)); }
72  static JsonValue boolean(bool value) { JsonValue v; v._type = JsonType::boolean; v._boolean = value; return v; }
73  static JsonValue string(const std::string &value) { JsonValue v; v._type = JsonType::string; v._string = value; return v; }
74 
75  static JsonValue parse(const std::string &json);
76  std::string to_json() const;
77 
78  const JsonValue &prop(const std::string &name) const { auto it = _properties.find(name); if (it != _properties.end()) return it->second; static JsonValue undef; return undef; }
79  const JsonValue &prop(const char *name) const { auto it = _properties.find(name); if (it != _properties.end()) return it->second; static JsonValue undef; return undef; }
80  JsonValue &prop(const std::string &name) { return _properties[name]; }
81  JsonValue &prop(const char *name) { return _properties[name]; }
82 
83  void remove(const std::string &name) { auto it = _properties.find(name); if (it != _properties.end()) _properties.erase(it); }
84  void remove(const char *name) { auto it = _properties.find(name); if (it != _properties.end()) _properties.erase(it); }
85 
86  size_t size() const { return _items.size(); }
87  JsonValue &at(size_t index) { return _items.at(index); }
88  const JsonValue &at(size_t index) const { return _items.at(index); }
89  void erase(size_t offset, size_t length) { _items.erase(_items.begin() + offset, _items.begin() + offset + length); }
90 
91  void clear() { _items.clear(); _properties.clear(); }
92 
93  JsonType type() const { return _type; }
94  bool is_undefined() const { return type() == JsonType::undefined; }
95  bool is_null() const { return type() == JsonType::null; }
96  bool is_object() const { return type() == JsonType::object; }
97  bool is_array() const { return type() == JsonType::array; }
98  bool is_number() const { return type() == JsonType::number; }
99  bool is_boolean() const { return type() == JsonType::boolean; }
100 
101  std::map<std::string, JsonValue> &properties() { return _properties; }
102  const std::map<std::string, JsonValue> &properties() const { return _properties; }
103 
104  std::vector<JsonValue> &items() { return _items; }
105  const std::vector<JsonValue> &items() const { return _items; }
106 
107  double to_number() const { return _number; }
108  bool to_boolean() const { return _boolean; }
109  const std::string &to_string() const { return _string; }
110 
111  double to_double() const { return to_number(); }
112  float to_float() const { return static_cast<float>(to_number()); }
113  int to_int() const { return static_cast<int>(to_number()); }
114  unsigned int to_uint() const { return static_cast<unsigned int>(to_number()); }
115  short to_short() const { return static_cast<short>(to_number()); }
116  unsigned short to_ushort() const { return static_cast<unsigned short>(to_number()); }
117  char to_char() const { return static_cast<char>(to_number()); }
118  unsigned char to_uchar() const { return static_cast<unsigned char>(to_number()); }
119 
120  void set_undefined() { *this = JsonValue::undefined(); }
121  void set_null() { *this = JsonValue::null(); }
122  void set_object() { *this = JsonValue::object(); }
123  void set_array() { *this = JsonValue::array(); }
124  void set_string(const std::string &v) { *this = JsonValue::string(v); }
125  void set_string(const char *v) { *this = JsonValue::string(v); }
126  void set_number(double v) { *this = JsonValue::number(v); }
127  void set_number(float v) { *this = JsonValue::number(v); }
128  void set_number(int v) { *this = JsonValue::number(v); }
129  void set_number(unsigned int v) { *this = JsonValue::number(v); }
130  void set_number(short v) { *this = JsonValue::number(v); }
131  void set_number(unsigned short v) { *this = JsonValue::number(v); }
132  void set_number(char v) { *this = JsonValue::number(v); }
133  void set_number(unsigned char v) { *this = JsonValue::number(v); }
134  void set_boolean(bool v) { *this = JsonValue::boolean(v); }
135 
136  JsonValue &operator[](const std::string &name) { return prop(name); }
137  JsonValue &operator[](const char *name) { return prop(name); }
138  const JsonValue &operator[](const std::string &name) const { return prop(name); }
139  const JsonValue &operator[](const char *name) const { return prop(name); }
140 
141  JsonValue &operator[](size_t index) { return at(index); }
142  const JsonValue &operator[](size_t index) const { return at(index); }
143 
144  private:
146  std::vector<JsonValue> _items;
147  std::map<std::string, JsonValue> _properties;
148  std::string _string;
149  double _number = 0.0;
150  bool _boolean = false;
151  };
152 }
Definition: clanapp.h:35
void set_null()
Definition: json_value.h:121
static JsonValue number(float value)
Definition: json_value.h:65
static JsonValue boolean(bool value)
Definition: json_value.h:72
JsonValue & operator[](const char *name)
Definition: json_value.h:137
const std::string & to_string() const
Definition: json_value.h:109
JsonValue & prop(const std::string &name)
Definition: json_value.h:80
static JsonValue number(unsigned short value)
Definition: json_value.h:69
const JsonValue & prop(const char *name) const
Definition: json_value.h:79
bool is_undefined() const
Definition: json_value.h:94
void set_string(const std::string &v)
Definition: json_value.h:124
void set_boolean(bool v)
Definition: json_value.h:134
void set_number(unsigned int v)
Definition: json_value.h:129
JsonException(const std::string &message)
Definition: json_value.h:43
const std::vector< JsonValue > & items() const
Definition: json_value.h:105
double to_number() const
Definition: json_value.h:107
bool is_object() const
Definition: json_value.h:96
void set_number(char v)
Definition: json_value.h:132
JsonValue & operator[](const std::string &name)
Definition: json_value.h:136
JsonValue & prop(const char *name)
Definition: json_value.h:81
const JsonValue & operator[](const std::string &name) const
Definition: json_value.h:138
std::vector< JsonValue > & items()
Definition: json_value.h:104
void set_number(unsigned char v)
Definition: json_value.h:133
Exception class thrown for JSON exceptions.
Definition: json_value.h:40
void erase(size_t offset, size_t length)
Definition: json_value.h:89
void set_number(unsigned short v)
Definition: json_value.h:131
const JsonValue & operator[](const char *name) const
Definition: json_value.h:139
Top-level exception class.
Definition: exception.h:41
void clear()
Definition: json_value.h:91
static JsonValue number(double value)
Definition: json_value.h:64
void set_number(short v)
Definition: json_value.h:130
std::string to_json() const
void set_string(const char *v)
Definition: json_value.h:125
void set_number(int v)
Definition: json_value.h:128
size_t size() const
Definition: json_value.h:86
const JsonValue & operator[](size_t index) const
Definition: json_value.h:142
static JsonValue number(unsigned int value)
Definition: json_value.h:67
int to_int() const
Definition: json_value.h:113
void set_undefined()
Definition: json_value.h:120
Definition: json_value.h:57
static JsonValue number(short value)
Definition: json_value.h:68
unsigned char to_uchar() const
Definition: json_value.h:118
void set_array()
Definition: json_value.h:123
float to_float() const
Definition: json_value.h:112
unsigned int to_uint() const
Definition: json_value.h:114
static JsonValue object()
Definition: json_value.h:62
JsonValue & at(size_t index)
Definition: json_value.h:87
static JsonValue string(const std::string &value)
Definition: json_value.h:73
bool is_array() const
Definition: json_value.h:97
value is a keyword
const JsonValue & at(size_t index) const
Definition: json_value.h:88
void set_object()
Definition: json_value.h:122
JsonType
Definition: json_value.h:46
bool to_boolean() const
Definition: json_value.h:108
bool is_null() const
Definition: json_value.h:95
std::map< std::string, JsonValue > & properties()
Definition: json_value.h:101
static JsonValue parse(const std::string &json)
static JsonValue number(int value)
Definition: json_value.h:66
JsonType type() const
Definition: json_value.h:93
double to_double() const
Definition: json_value.h:111
const std::map< std::string, JsonValue > & properties() const
Definition: json_value.h:102
unsigned short to_ushort() const
Definition: json_value.h:116
short to_short() const
Definition: json_value.h:115
bool is_number() const
Definition: json_value.h:98
bool is_boolean() const
Definition: json_value.h:99
JsonValue & operator[](size_t index)
Definition: json_value.h:141
void set_number(float v)
Definition: json_value.h:127
static JsonValue null()
Definition: json_value.h:61
static JsonValue undefined()
Definition: json_value.h:60
static JsonValue array()
Definition: json_value.h:63
void set_number(double v)
Definition: json_value.h:126
static JsonValue number(unsigned char value)
Definition: json_value.h:71
char to_char() const
Definition: json_value.h:117
static JsonValue number(char value)
Definition: json_value.h:70
std::string message
Description of exception.
Definition: exception.h:51
const JsonValue & prop(const std::string &name) const
Definition: json_value.h:78