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