libzypp: zypp-core/parser/json/JsonValue.h Source File

libzypp 17.38.11

JsonValue.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#ifndef ZYPP_CORE_PARSER_JSON_JSON_VALUE_DEFINED
10#define ZYPP_CORE_PARSER_JSON_JSON_VALUE_DEFINED
11
12#include <cmath>
13#include <variant>
14#include <vector>
15#include <map>
16#include <set>
17#include <ostream>
18
19#include <zypp-core/base/String.h>
20
21#include "JsonBool.h"
22#include "JsonNull.h"
23#include "JsonNumber.h"
24#include "JsonString.h"
25
26namespace zypp::json {
27
28 class Value;
29 class Array;
30 class Object;
31
32 class Array {
33
34 public:
35
36 using iterator = std::vector<Value>::iterator;
37 using const_iterator = std::vector<Value>::const_iterator;
38 using size_type = std::vector<Value>::size_type;
39
40 Array();
41
43 template <class Iterator>
44 Array( Iterator begin, Iterator end );
45
46 template< class V>
47 Array( const std::vector<V> & cont_r ) : Array(cont_r.begin (), cont_r.end()) {}
48
49 template< class V>
50 Array( const std::list<V> & cont_r ) : Array(cont_r.begin (), cont_r.end()) {}
51
52 template< class V>
53 Array( const std::set<V> & cont_r ) : Array(cont_r.begin (), cont_r.end()) {}
54
56 Array( std::initializer_list<Value> contents_r );
57
59 void add( Value val_r );
60
62 void add( const std::initializer_list<Value> & contents_r );
63
65 std::string asJSON() const
66 { return str::Str() << *this; }
67
69 std::string asString() const
70 { return asJSON(); }
71
73 std::ostream & dumpOn( std::ostream & str ) const;
74
75 iterator begin() { return _values.begin (); }
76 iterator end() { return _values.end (); }
77
78 const_iterator begin() const { return _values.begin (); }
79 const_iterator end() const { return _values.end (); }
80
81 const Value &operator[]( size_type n ) const;
82 Value &operator[](size_type n);
83
84 size_type size() const;
85
86 bool operator==( const Array &other ) const;
87
88 private:
89 std::vector<Value> _values;
90 };
91
93 inline std::ostream & operator<<( std::ostream & str, const Array & obj )
94 {
95 return obj.dumpOn( str );
96 }
97
98
99 class Object {
100 public:
101
102 using iterator = std::multimap<String, Value>::iterator;
103 using const_iterator = std::multimap<String, Value>::const_iterator;
104 using size_type = std::multimap<String, Value>::size_type;
105
106 Object();
107
109 template <class Iterator>
110 Object( Iterator begin, Iterator end )
111 { for_( it, begin, end ) add( it->first, it->second ); }
112
114 Object( const std::initializer_list<std::pair<String, Value>> & contents_r );
115
116 template <typename K, typename V>
117 Object( std::multimap<K,V> values ) : _values( std::make_move_iterator(values.begin()), std::make_move_iterator(values.end()) ) { }
118
119 template <typename K, typename V>
120 Object( std::map<K,V> values ) : _values( std::make_move_iterator(values.begin()), std::make_move_iterator(values.end()) ) { }
121
123 void add( String key_r, Value val_r );
124
126 void add(std::initializer_list<std::pair<String, Value> > contents_r );
127
129 std::string asJSON() const;
130
132 std::ostream & dumpOn( std::ostream & str ) const;
133
134 iterator begin() { return _values.begin (); }
135 iterator end() { return _values.end (); }
136
137 const_iterator begin() const { return _values.begin (); }
138 const_iterator end() const { return _values.end (); }
139
140 bool contains( const String& key ) const;
141
142 size_type size() const {
143 return _values.size ();
144 }
145
146 std::pair<iterator, iterator> equal_range( const String& key ) {
147 return _values.equal_range (key);
148 }
149
150 std::pair<const_iterator, const_iterator> equal_range( const String& key ) const {
151 return _values.equal_range (key);
152 }
153
154 std::vector<Value> values(const String &key) const;
155 const Value &value( const String &key ) const;
156
157 bool operator==( const Object &other ) const;
158
159 private:
160 std::ostream & dumpOn( std::ostream & str, std::map<String, Value>::const_iterator val_r ) const;
161 private:
162 std::multimap<String, Value> _values;
163 };
164
166 inline std::ostream & operator<<( std::ostream & str, const Object & obj )
167 {
168 return obj.dumpOn( str );
169 }
170
171
172
173 class Value {
174
175 public:
176
177
178 enum Type {
179 NullType,
180 BoolType,
181 StringType,
182 IntType,
183 UIntType,
184 NumberType,
185 ObjectType,
186 ArrayType
187 };
188
189
190 ~Value() = default;
191 Value(const Value &) = default;
192 Value(Value &&) = default;
193 Value &operator=(const Value &) = default;
194 Value &operator=(Value &&) = default;
195
196 // null
197 Value() = default;
198 Value( std::nullptr_t ) { }
199
200 // bool
201 Value( bool val_r ) : _value(Bool(val_r)) {}
202
203 // numbers
204 Value( std::int8_t val_r ) : _value(Int(val_r) ) {}
205 Value( std::int16_t val_r ) : _value(Int(val_r) ) {}
206 Value( std::int32_t val_r ) : _value(Int(val_r) ) {}
207 Value( std::int64_t val_r ) : _value(Int(val_r) ) {}
208
209 Value( std::uint8_t val_r ) : _value(UInt(val_r) ) {}
210 Value( std::uint16_t val_r ) : _value(UInt(val_r) ) {}
211 Value( std::uint32_t val_r ) : _value(UInt(val_r) ) {}
212 Value( std::uint64_t val_r ) : _value(UInt(val_r) ) {}
213
214 Value( float val_r ) : _value(Number(val_r) ) {}
215 Value( double val_r ) : _value(Number(val_r) ) {}
216
217 // strings
218 Value( const char val_r ) : _value(String(std::string( 1, val_r )) ) {}
219 Value( const char * val_r ) : _value(String(str::asString (val_r)) ) {}
220 Value( std::string val_r ) : _value(String(std::move(val_r)) ) {}
221
222 // Object
223 //Value (const std::initializer_list<std::pair<String, Value>> & contents_r) : _value(Object(contents_r)) {}
224
225 // Array
226 // Value (const std::initializer_list<Value> &contents_r) : _value(Array(contents_r)) {}
227
228 // JSON types
229 Value( Null val_r ) : _value( std::move(val_r) ) {}
230 Value( Bool val_r ) : _value( std::move(val_r) ) {}
231 Value( String val_r ) : _value( std::move(val_r) ) {}
232 Value( Int val_r ) : _value( std::move(val_r) ) {}
233 Value( UInt val_r ) : _value( std::move(val_r) ) {}
234 Value( Number val_r ) : _value( std::move(val_r) ) {}
235 Value( Object val_r ) : _value( std::move(val_r) ) {}
236 Value( Array val_r ) : _value( std::move(val_r) ) {}
237
239 std::string asJSON() const;
240
241 const Null &asNull() const {
242 return std::get<Null>( _value );
243 }
244
245 const Bool &asBool() const {
246 return std::get<Bool>( _value );
247 }
248
249 const String &asString() const {
250 return std::get<String>( _value );
251 }
252
253 const Int &asInt() const {
254 return std::get<Int>( _value );
255 }
256
257 const UInt &asUInt() const {
258 return std::get<UInt>( _value );
259 }
260
261 const Number &asNumber() const {
262 return std::get<Number>( _value );
263 }
264
265 const Object &asObject() const {
266 return std::get<Object>( _value );
267 }
268
269 const Array &asArray() const {
270 return std::get<Array>( _value );
271 }
272
274 std::ostream & dumpOn( std::ostream & str ) const
275 { return str << asJSON(); }
276
277 bool isNull() const {
278 return std::holds_alternative<Null>(_value);
279 }
280
281 Type type() const {
282 return std::visit([](const auto &t) {
283 using _Type = std::decay_t<decltype(t)>;
284 if constexpr ( std::is_same_v<_Type, Bool> ) {
285 return BoolType;
286 } else if constexpr ( std::is_same_v<_Type, String> ) {
287 return StringType;
288 } else if constexpr ( std::is_same_v<_Type, Int> ) {
289 return IntType;
290 } else if constexpr ( std::is_same_v<_Type, UInt> ) {
291 return UIntType;
292 } else if constexpr ( std::is_same_v<_Type, Number> ) {
293 return NumberType;
294 } else if constexpr ( std::is_same_v<_Type, Object> ) {
295 return ObjectType;
296 } else if constexpr ( std::is_same_v<_Type, Array> ) {
297 return ArrayType;
298 }
299 return NullType;
300 }, _value );
301 }
302
303 // comparison
304 bool operator==( const Value &other ) const { return _value == other._value; }
305
306 private:
307 using JsonValueData = std::variant<Null, Bool, String, Int, UInt, Number, Object, Array>;
308 JsonValueData _value = Null();
309 };
310
311 template<class Iterator>
312 Array::Array(Iterator begin, Iterator end)
313 {
314 std::for_each( begin, end, [this]( const auto &v) { this->add(v); } );
315 }
316
318 inline std::ostream & operator<<( std::ostream & str, const Value & obj )
319 { return obj.dumpOn( str ); }
320
321 template <typename T>
322 Value toJSON( T &&v ) {
323 return Value( std::forward<T>(v) );
324 }
325}
326
327#endif
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
JsonBool.h
JsonNull.h
JsonNumber.h
JsonString.h
String.h
zypp::json::Array
Definition JsonValue.h:32
zypp::json::Array::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonValue.cc:29
zypp::json::Array::Array
Array(const std::list< V > &cont_r)
Definition JsonValue.h:50
zypp::json::Array::size
size_type size() const
Definition JsonValue.cc:44
zypp::json::Array::_values
std::vector< Value > _values
Definition JsonValue.h:89
zypp::json::Array::asJSON
std::string asJSON() const
JSON representation.
Definition JsonValue.h:65
zypp::json::Array::end
iterator end()
Definition JsonValue.h:76
zypp::json::Array::operator[]
const Value & operator[](size_type n) const
Definition JsonValue.cc:39
zypp::json::Array::asString
std::string asString() const
String representation.
Definition JsonValue.h:69
zypp::json::Array::begin
const_iterator begin() const
Definition JsonValue.h:78
zypp::json::Array::end
const_iterator end() const
Definition JsonValue.h:79
zypp::json::Array::Array
Array(const std::set< V > &cont_r)
Definition JsonValue.h:53
zypp::json::Array::begin
iterator begin()
Definition JsonValue.h:75
zypp::json::Array::Array
Array()
Definition JsonValue.cc:14
zypp::json::Array::Array
Array(const std::vector< V > &cont_r)
Definition JsonValue.h:47
zypp::json::Array::add
void add(Value val_r)
Push JSON Value to Array.
Definition JsonValue.cc:20
zypp::json::Array::const_iterator
std::vector< Value >::const_iterator const_iterator
Definition JsonValue.h:37
zypp::json::Array::operator==
bool operator==(const Array &other) const
Definition JsonValue.cc:51
zypp::json::Array::size_type
std::vector< Value >::size_type size_type
Definition JsonValue.h:38
zypp::json::Array::iterator
std::vector< Value >::iterator iterator
Definition JsonValue.h:36
zypp::json::Bool
Definition JsonBool.h:20
zypp::json::Int
Definition JsonNumber.h:66
zypp::json::Null
Definition JsonNull.h:19
zypp::json::Number
Definition JsonNumber.h:19
zypp::json::Object
Definition JsonValue.h:99
zypp::json::Object::Object
Object(Iterator begin, Iterator end)
Construct from map-iterator.
Definition JsonValue.h:110
zypp::json::Object::end
const_iterator end() const
Definition JsonValue.h:138
zypp::json::Object::Object
Object()
Definition JsonValue.cc:56
zypp::json::Object::size_type
std::multimap< String, Value >::size_type size_type
Definition JsonValue.h:104
zypp::json::Object::iterator
std::multimap< String, Value >::iterator iterator
Definition JsonValue.h:102
zypp::json::Object::begin
const_iterator begin() const
Definition JsonValue.h:137
zypp::json::Object::asJSON
std::string asJSON() const
JSON representation.
Definition JsonValue.cc:71
zypp::json::Object::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonValue.cc:74
zypp::json::Object::value
const Value & value(const String &key) const
Definition JsonValue.cc:97
zypp::json::Object::equal_range
std::pair< const_iterator, const_iterator > equal_range(const String &key) const
Definition JsonValue.h:150
zypp::json::Object::contains
bool contains(const String &key) const
Definition JsonValue.cc:85
zypp::json::Object::Object
Object(std::map< K, V > values)
Definition JsonValue.h:120
zypp::json::Object::Object
Object(std::multimap< K, V > values)
Definition JsonValue.h:117
zypp::json::Object::begin
iterator begin()
Definition JsonValue.h:134
zypp::json::Object::values
std::vector< Value > values(const String &key) const
Definition JsonValue.cc:89
zypp::json::Object::_values
std::multimap< String, Value > _values
Definition JsonValue.h:162
zypp::json::Object::equal_range
std::pair< iterator, iterator > equal_range(const String &key)
Definition JsonValue.h:146
zypp::json::Object::const_iterator
std::multimap< String, Value >::const_iterator const_iterator
Definition JsonValue.h:103
zypp::json::Object::size
size_type size() const
Definition JsonValue.h:142
zypp::json::Object::end
iterator end()
Definition JsonValue.h:135
zypp::json::Object::add
void add(String key_r, Value val_r)
Add key/value pair.
Definition JsonValue.cc:62
zypp::json::Object::operator==
bool operator==(const Object &other) const
Definition JsonValue.cc:103
zypp::json::String
Definition JsonString.h:113
zypp::json::UInt
Definition JsonNumber.h:111
zypp::json::Value
Definition JsonValue.h:173
zypp::json::Value::asUInt
const UInt & asUInt() const
Definition JsonValue.h:257
zypp::json::Value::Value
Value(std::uint64_t val_r)
Definition JsonValue.h:212
zypp::json::Value::asString
const String & asString() const
Definition JsonValue.h:249
zypp::json::Value::Value
Value(std::uint32_t val_r)
Definition JsonValue.h:211
zypp::json::Value::asBool
const Bool & asBool() const
Definition JsonValue.h:245
zypp::json::Value::operator==
bool operator==(const Value &other) const
Definition JsonValue.h:304
zypp::json::Value::Value
Value(std::int16_t val_r)
Definition JsonValue.h:205
zypp::json::Value::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonValue.h:274
zypp::json::Value::~Value
~Value()=default
zypp::json::Value::isNull
bool isNull() const
Definition JsonValue.h:277
zypp::json::Value::Value
Value(Null val_r)
Definition JsonValue.h:229
zypp::json::Value::Value
Value(std::int8_t val_r)
Definition JsonValue.h:204
zypp::json::Value::Value
Value(float val_r)
Definition JsonValue.h:214
zypp::json::Value::Value
Value(std::int64_t val_r)
Definition JsonValue.h:207
zypp::json::Value::Value
Value(String val_r)
Definition JsonValue.h:231
zypp::json::Value::Value
Value(Int val_r)
Definition JsonValue.h:232
zypp::json::Value::operator=
Value & operator=(Value &&)=default
zypp::json::Value::type
Type type() const
Definition JsonValue.h:281
zypp::json::Value::Value
Value(UInt val_r)
Definition JsonValue.h:233
zypp::json::Value::Value
Value(std::int32_t val_r)
Definition JsonValue.h:206
zypp::json::Value::Value
Value(double val_r)
Definition JsonValue.h:215
zypp::json::Value::Value
Value(const Value &)=default
zypp::json::Value::Value
Value(Number val_r)
Definition JsonValue.h:234
zypp::json::Value::asArray
const Array & asArray() const
Definition JsonValue.h:269
zypp::json::Value::Value
Value(const char *val_r)
Definition JsonValue.h:219
zypp::json::Value::asNull
const Null & asNull() const
Definition JsonValue.h:241
zypp::json::Value::Value
Value(std::uint16_t val_r)
Definition JsonValue.h:210
zypp::json::Value::Value
Value(Object val_r)
Definition JsonValue.h:235
zypp::json::Value::Value
Value(bool val_r)
Definition JsonValue.h:201
zypp::json::Value::Value
Value(Array val_r)
Definition JsonValue.h:236
zypp::json::Value::_value
JsonValueData _value
Definition JsonValue.h:308
zypp::json::Value::Value
Value()=default
zypp::json::Value::Value
Value(std::nullptr_t)
Definition JsonValue.h:198
zypp::json::Value::JsonValueData
std::variant< Null, Bool, String, Int, UInt, Number, Object, Array > JsonValueData
Definition JsonValue.h:307
zypp::json::Value::Type
Type
Definition JsonValue.h:178
zypp::json::Value::UIntType
@ UIntType
Definition JsonValue.h:183
zypp::json::Value::ObjectType
@ ObjectType
Definition JsonValue.h:185
zypp::json::Value::IntType
@ IntType
Definition JsonValue.h:182
zypp::json::Value::StringType
@ StringType
Definition JsonValue.h:181
zypp::json::Value::NullType
@ NullType
Definition JsonValue.h:179
zypp::json::Value::BoolType
@ BoolType
Definition JsonValue.h:180
zypp::json::Value::ArrayType
@ ArrayType
Definition JsonValue.h:186
zypp::json::Value::NumberType
@ NumberType
Definition JsonValue.h:184
zypp::json::Value::Value
Value(Bool val_r)
Definition JsonValue.h:230
zypp::json::Value::Value
Value(const char val_r)
Definition JsonValue.h:218
zypp::json::Value::Value
Value(Value &&)=default
zypp::json::Value::Value
Value(std::string val_r)
Definition JsonValue.h:220
zypp::json::Value::asJSON
std::string asJSON() const
JSON representation.
Definition JsonValue.cc:110
zypp::json::Value::operator=
Value & operator=(const Value &)=default
zypp::json::Value::Value
Value(std::uint8_t val_r)
Definition JsonValue.h:209
zypp::json::Value::asNumber
const Number & asNumber() const
Definition JsonValue.h:261
zypp::json::Value::asObject
const Object & asObject() const
Definition JsonValue.h:265
zypp::json::Value::asInt
const Int & asInt() const
Definition JsonValue.h:253
std
Definition ansi.h:855
std::decay_t
typename decay< T >::type decay_t
Definition TypeTraits.h:42
str
String related utilities and Regular expression matching.
zypp::json
Definition TargetImpl.cc:153
zypp::json::toJSON
json::Value toJSON(const sat::Transaction::Step &step_r)
See COMMITBEGIN (added in v1) on page Commit plugin for the specs.
Definition TargetImpl.cc:158
zypp::json::operator<<
std::ostream & operator<<(std::ostream &str, const Bool &obj)
relates: Bool Stream output
Definition JsonBool.h:56
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:213