json_value.h Source File - ClanLib SDK
Description: json_value.h Source File

json_value.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
34namespace 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:
145 JsonType _type = JsonType::undefined;
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}
clan::Exception::Exception
Exception(const std::string &message)
Constructs an exception object.
clan::Exception::message
std::string message
Description of exception.
Definition exception.h:51
clan::JsonValue
Definition json_value.h:58
clan::JsonValue::to_double
double to_double() const
Definition json_value.h:111
clan::JsonValue::set_undefined
void set_undefined()
Definition json_value.h:120
clan::JsonValue::set_number
void set_number(double v)
Definition json_value.h:126
clan::JsonValue::prop
JsonValue & prop(const std::string &name)
Definition json_value.h:80
clan::JsonValue::to_float
float to_float() const
Definition json_value.h:112
clan::JsonValue::parse
static JsonValue parse(const std::string &json)
clan::JsonValue::null
static JsonValue null()
Definition json_value.h:61
clan::JsonValue::set_number
void set_number(char v)
Definition json_value.h:132
clan::JsonValue::is_undefined
bool is_undefined() const
Definition json_value.h:94
clan::JsonValue::set_number
void set_number(unsigned short v)
Definition json_value.h:131
clan::JsonValue::to_short
short to_short() const
Definition json_value.h:115
clan::JsonValue::to_boolean
bool to_boolean() const
Definition json_value.h:108
clan::JsonValue::prop
const JsonValue & prop(const char *name) const
Definition json_value.h:79
clan::JsonValue::to_char
char to_char() const
Definition json_value.h:117
clan::JsonValue::items
const std::vector< JsonValue > & items() const
Definition json_value.h:105
clan::JsonValue::number
static JsonValue number(float value)
Definition json_value.h:65
clan::JsonValue::set_string
void set_string(const char *v)
Definition json_value.h:125
clan::JsonValue::array
static JsonValue array()
Definition json_value.h:63
clan::JsonValue::is_array
bool is_array() const
Definition json_value.h:97
clan::JsonValue::operator[]
JsonValue & operator[](const std::string &name)
Definition json_value.h:136
clan::JsonValue::size
size_t size() const
Definition json_value.h:86
clan::JsonValue::operator[]
JsonValue & operator[](size_t index)
Definition json_value.h:141
clan::JsonValue::is_null
bool is_null() const
Definition json_value.h:95
clan::JsonValue::to_string
const std::string & to_string() const
Definition json_value.h:109
clan::JsonValue::operator[]
JsonValue & operator[](const char *name)
Definition json_value.h:137
clan::JsonValue::is_boolean
bool is_boolean() const
Definition json_value.h:99
clan::JsonValue::to_uint
unsigned int to_uint() const
Definition json_value.h:114
clan::JsonValue::type
JsonType type() const
Definition json_value.h:93
clan::JsonValue::number
static JsonValue number(int value)
Definition json_value.h:66
clan::JsonValue::set_string
void set_string(const std::string &v)
Definition json_value.h:124
clan::JsonValue::number
static JsonValue number(short value)
Definition json_value.h:68
clan::JsonValue::remove
void remove(const char *name)
Definition json_value.h:84
clan::JsonValue::undefined
static JsonValue undefined()
Definition json_value.h:60
clan::JsonValue::operator[]
const JsonValue & operator[](const char *name) const
Definition json_value.h:139
clan::JsonValue::to_int
int to_int() const
Definition json_value.h:113
clan::JsonValue::object
static JsonValue object()
Definition json_value.h:62
clan::JsonType
JsonType
Definition json_value.h:47
clan::JsonValue::clear
void clear()
Definition json_value.h:91
clan::JsonValue::is_object
bool is_object() const
Definition json_value.h:96
clan::JsonValue::set_number
void set_number(unsigned char v)
Definition json_value.h:133
clan::JsonValue::number
static JsonValue number(unsigned short value)
Definition json_value.h:69
clan::JsonValue::set_null
void set_null()
Definition json_value.h:121
clan::JsonValue::set_array
void set_array()
Definition json_value.h:123
clan::JsonValue::prop
const JsonValue & prop(const std::string &name) const
Definition json_value.h:78
clan::JsonValue::to_number
double to_number() const
Definition json_value.h:107
clan::JsonValue::at
const JsonValue & at(size_t index) const
Definition json_value.h:88
clan::JsonValue::at
JsonValue & at(size_t index)
Definition json_value.h:87
clan::JsonValue::number
static JsonValue number(unsigned int value)
Definition json_value.h:67
clan::JsonValue::to_ushort
unsigned short to_ushort() const
Definition json_value.h:116
clan::JsonValue::set_number
void set_number(float v)
Definition json_value.h:127
clan::JsonValue::operator[]
const JsonValue & operator[](const std::string &name) const
Definition json_value.h:138
clan::JsonValue::prop
JsonValue & prop(const char *name)
Definition json_value.h:81
clan::JsonValue::boolean
static JsonValue boolean(bool value)
Definition json_value.h:72
clan::JsonValue::number
static JsonValue number(double value)
Definition json_value.h:64
clan::JsonValue::number
static JsonValue number(char value)
Definition json_value.h:70
clan::JsonValue::number
static JsonValue number(unsigned char value)
Definition json_value.h:71
clan::JsonValue::set_number
void set_number(short v)
Definition json_value.h:130
clan::JsonValue::properties
const std::map< std::string, JsonValue > & properties() const
Definition json_value.h:102
clan::JsonException::JsonException
JsonException(const std::string &message)
Definition json_value.h:43
clan::JsonValue::string
static JsonValue string(const std::string &value)
Definition json_value.h:73
clan::JsonValue::is_number
bool is_number() const
Definition json_value.h:98
clan::JsonValue::set_number
void set_number(unsigned int v)
Definition json_value.h:129
clan::JsonValue::items
std::vector< JsonValue > & items()
Definition json_value.h:104
clan::JsonValue::set_boolean
void set_boolean(bool v)
Definition json_value.h:134
clan::JsonValue::to_uchar
unsigned char to_uchar() const
Definition json_value.h:118
clan::JsonValue::properties
std::map< std::string, JsonValue > & properties()
Definition json_value.h:101
clan::JsonValue::to_json
std::string to_json() const
clan::JsonValue::remove
void remove(const std::string &name)
Definition json_value.h:83
clan::JsonValue::set_object
void set_object()
Definition json_value.h:122
clan::JsonValue::erase
void erase(size_t offset, size_t length)
Definition json_value.h:89
clan::JsonValue::set_number
void set_number(int v)
Definition json_value.h:128
clan::JsonValue::operator[]
const JsonValue & operator[](size_t index) const
Definition json_value.h:142
clan::JsonType::null
@ null
Definition json_value.h:49
clan::JsonType::undefined
@ undefined
Definition json_value.h:48
clan::JsonType::boolean
@ boolean
Definition json_value.h:53
clan::JsonType::object
@ object
Definition json_value.h:50
clan::JsonType::number
@ number
Definition json_value.h:52
clan::JsonType::string
@ string
Definition json_value.h:54
clan::JsonType::array
@ array
Definition json_value.h:51
clan
Definition clanapp.h:36
clan::StyleValueType::length
@ length
value is a keyword
Definition style_value_type.h:38
clan::Key::v
@ v
Definition keys.h:102