2
* Copyright © 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Thomas Voß <thomas.voss@canonical.com>
19
#include <core/trust/click/json_object.h>
21
#include <json-c/json.h>
27
Object Object::parse_from_string(const std::string& s)
29
return Object{json_tokener_parse(s.c_str())};
32
Object Object::create_array()
34
return Object{json_object_new_array()};
37
Object Object::create_object()
39
return Object{json_object_new_object()};
42
Object::Object(json_object* object) : object(object)
46
Object::Object(const Object& rhs) : object(json_object_get(rhs.object))
52
json_object_put(object);
55
Object& Object::operator=(const Object& rhs)
57
json_object_put(object);
58
object = json_object_get(rhs.object);
63
std::string Object::to_plain_string()
67
json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN)
71
Object Object::get(const std::string& name) const
73
json_object* result{nullptr};
75
json_object_object_get_ex(object, name.c_str(), &result);
77
if (not result) throw std::out_of_range
82
return Object{result};
87
template<json_type type> void throw_if_type_mismatch(json_object* object)
89
if (not json_object_is_type(object, type)) throw std::logic_error
95
// Attempts to resolve the object to a boolean value.
96
// Throws std::logic_error in case of type mismatches.
97
bool Object::to_bool() const
99
throw_if_type_mismatch<json_type_boolean>(object);
100
return json_object_get_boolean(object);
103
std::int32_t Object::to_int32() const
105
throw_if_type_mismatch<json_type_int>(object);
106
return json_object_get_int(object);
109
std::int64_t Object::to_int64() const
111
throw_if_type_mismatch<json_type_int>(object);
112
return json_object_get_int64(object);
115
double Object::to_double() const
117
throw_if_type_mismatch<json_type_double>(object);
118
return json_object_get_double(object);
121
std::string Object::to_string() const
123
throw_if_type_mismatch<json_type_string>(object);
124
return std::string{json_object_get_string(object)};
127
void Object::put_array(const std::string& name, Object array)
129
json_object_object_add(object, name.c_str(), json_object_get(array.object));
132
void Object::put_object(const std::string& name, Object other)
134
json_object_object_add(object, name.c_str(), json_object_get(other.object));
137
void Object::put_boolean(const std::string& name, bool value)
139
json_object_object_add(object, name.c_str(), json_object_new_boolean(value));
142
void Object::put_int32(const std::string& name, std::int32_t value)
144
json_object_object_add(object, name.c_str(), json_object_new_int(value));
147
void Object::put_int64(const std::string& name, std::int64_t value)
149
json_object_object_add(object, name.c_str(), json_object_new_int64(value));
152
void Object::put_double(const std::string& name, double value)
154
json_object_object_add(object, name.c_str(), json_object_new_double(value));
157
void Object::put_string(const std::string& name, const std::string& value)
159
json_object_object_add(object, name.c_str(), json_object_new_string_len(value.c_str(), value.size()));
162
std::size_t Object::array_size() const
164
throw_if_type_mismatch<json_type_array>(object);
165
return json_object_array_length(object);
168
void Object::append(Object other)
170
throw_if_type_mismatch<json_type_array>(object);
171
json_object_array_add(object, json_object_get(other.object));
174
void Object::put_object_for_index(std::size_t index, Object other)
176
throw_if_type_mismatch<json_type_array>(object);
177
json_object_array_put_idx(object, index, json_object_get(other.object));
180
// Queries the object at index 'index'.
181
// Throws std::logic_error if the object does not represent an array.
182
// Throws std::out_of_range if the index exceeds the bounds of the array.
183
Object Object::get_object_for_index(std::size_t index)
185
throw_if_type_mismatch<json_type_array>(object);
188
json_object_get(json_object_array_get_idx(object, index))