~thomas-voss/trust-store/add-click-based-package-name-query

« back to all changes in this revision

Viewing changes to src/core/trust/click/json_object.cpp

  • Committer: thomas-voss
  • Date: 2014-08-21 08:07:29 UTC
  • Revision ID: thomas.voss@canonical.com-20140821080729-sihlhaifeibhicqt
Add an interface to installed click packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#include <core/trust/click/json_object.h>
 
20
 
 
21
#include <json-c/json.h>
 
22
 
 
23
#include <stdexcept>
 
24
 
 
25
namespace json
 
26
{
 
27
Object Object::parse_from_string(const std::string& s)
 
28
{
 
29
    return Object{json_tokener_parse(s.c_str())};
 
30
}
 
31
 
 
32
Object Object::create_array()
 
33
{
 
34
    return Object{json_object_new_array()};
 
35
}
 
36
 
 
37
Object Object::create_object()
 
38
{
 
39
    return Object{json_object_new_object()};
 
40
}
 
41
 
 
42
Object::Object(json_object* object) : object(object)
 
43
{
 
44
}
 
45
 
 
46
Object::Object(const Object& rhs) : object(json_object_get(rhs.object))
 
47
{
 
48
}
 
49
 
 
50
Object::~Object()
 
51
{
 
52
    json_object_put(object);
 
53
}
 
54
 
 
55
Object& Object::operator=(const Object& rhs)
 
56
{
 
57
    json_object_put(object);
 
58
    object = json_object_get(rhs.object);
 
59
 
 
60
    return *this;
 
61
}
 
62
 
 
63
std::string Object::to_plain_string()
 
64
{
 
65
    return std::string
 
66
    {
 
67
        json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN)
 
68
    };
 
69
}
 
70
 
 
71
Object Object::get(const std::string& name) const
 
72
{
 
73
    json_object* result{nullptr};
 
74
 
 
75
    json_object_object_get_ex(object, name.c_str(), &result);
 
76
 
 
77
    if (not result) throw std::out_of_range
 
78
    {
 
79
        name.c_str()
 
80
    };
 
81
 
 
82
    return Object{result};
 
83
}
 
84
 
 
85
namespace
 
86
{
 
87
template<json_type type> void throw_if_type_mismatch(json_object* object)
 
88
{
 
89
    if (not json_object_is_type(object, type)) throw std::logic_error
 
90
    {
 
91
        "Type mismatch."
 
92
    };
 
93
}
 
94
}
 
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
 
98
{
 
99
    throw_if_type_mismatch<json_type_boolean>(object);
 
100
    return json_object_get_boolean(object);
 
101
}
 
102
 
 
103
std::int32_t Object::to_int32() const
 
104
{
 
105
    throw_if_type_mismatch<json_type_int>(object);
 
106
    return json_object_get_int(object);
 
107
}
 
108
 
 
109
std::int64_t Object::to_int64() const
 
110
{
 
111
    throw_if_type_mismatch<json_type_int>(object);
 
112
    return json_object_get_int64(object);
 
113
}
 
114
 
 
115
double Object::to_double() const
 
116
{
 
117
    throw_if_type_mismatch<json_type_double>(object);
 
118
    return json_object_get_double(object);
 
119
}
 
120
 
 
121
std::string Object::to_string() const
 
122
{
 
123
    throw_if_type_mismatch<json_type_string>(object);
 
124
    return std::string{json_object_get_string(object)};
 
125
}
 
126
 
 
127
void Object::put_array(const std::string& name, Object array)
 
128
{
 
129
    json_object_object_add(object, name.c_str(), json_object_get(array.object));
 
130
}
 
131
 
 
132
void Object::put_object(const std::string& name, Object other)
 
133
{
 
134
    json_object_object_add(object, name.c_str(), json_object_get(other.object));
 
135
}
 
136
 
 
137
void Object::put_boolean(const std::string& name, bool value)
 
138
{
 
139
    json_object_object_add(object, name.c_str(), json_object_new_boolean(value));
 
140
}
 
141
 
 
142
void Object::put_int32(const std::string& name, std::int32_t value)
 
143
{
 
144
    json_object_object_add(object, name.c_str(), json_object_new_int(value));
 
145
}
 
146
 
 
147
void Object::put_int64(const std::string& name, std::int64_t value)
 
148
{
 
149
    json_object_object_add(object, name.c_str(), json_object_new_int64(value));
 
150
}
 
151
 
 
152
void Object::put_double(const std::string& name, double value)
 
153
{
 
154
    json_object_object_add(object, name.c_str(), json_object_new_double(value));
 
155
}
 
156
 
 
157
void Object::put_string(const std::string& name, const std::string& value)
 
158
{
 
159
    json_object_object_add(object, name.c_str(), json_object_new_string_len(value.c_str(), value.size()));
 
160
}
 
161
 
 
162
std::size_t Object::array_size() const
 
163
{
 
164
    throw_if_type_mismatch<json_type_array>(object);
 
165
    return json_object_array_length(object);
 
166
}
 
167
 
 
168
void Object::append(Object other)
 
169
{
 
170
    throw_if_type_mismatch<json_type_array>(object);
 
171
    json_object_array_add(object, json_object_get(other.object));
 
172
}
 
173
 
 
174
void Object::put_object_for_index(std::size_t index, Object other)
 
175
{
 
176
    throw_if_type_mismatch<json_type_array>(object);
 
177
    json_object_array_put_idx(object, index, json_object_get(other.object));
 
178
}
 
179
 
 
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)
 
184
{
 
185
    throw_if_type_mismatch<json_type_array>(object);
 
186
    return Object
 
187
    {
 
188
        json_object_get(json_object_array_get_idx(object, index))
 
189
    };
 
190
}
 
191
}