libzypp 17.38.15

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
20#include <zypp-core/base/String.h>
21#include <zypp-core/Globals.h>
22
23#include "JsonBool.h"
24#include "JsonNull.h"
25#include "JsonNumber.h"
26#include "JsonString.h"
27
28 namespace zypp::json {
29
30 class Value;
31 class Array;
32 class Object;
33
34 class ZYPP_API Array {
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
101 class ZYPP_API Object {
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
175 class ZYPP_API Value {
176
177 public:
178
179
180 enum Type {
181 NullType,
182 BoolType,
183 StringType,
184 IntType,
185 UIntType,
186 NumberType,
187 ObjectType,
188 ArrayType
189 };
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>;
310 JsonValueData _value = Null();
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>
324 Value ZYPP_API toJSON( T &&v ) {
325 return Value( std::forward<T>(v) );
326 }
327}
328
329#endif
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
Globals.h
Provides API related macros.
JsonBool.h
JsonNull.h
JsonNumber.h
JsonString.h
String.h
zypp::json::Array
Definition JsonValue.h:34
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:52
zypp::json::Array::_values
std::vector< Value > _values
Definition JsonValue.h:91
zypp::json::Array::asJSON
std::string asJSON() const
JSON representation.
Definition JsonValue.h:67
zypp::json::Array::end
iterator end()
Definition JsonValue.h:78
zypp::json::Array::asString
std::string asString() const
String representation.
Definition JsonValue.h:71
zypp::json::Array::begin
const_iterator begin() const
Definition JsonValue.h:80
zypp::json::Array::end
const_iterator end() const
Definition JsonValue.h:81
zypp::json::Array::Array
Array(const std::set< V > &cont_r)
Definition JsonValue.h:55
zypp::json::Array::begin
iterator begin()
Definition JsonValue.h:77
zypp::json::Array::Array
Array()
Definition JsonValue.cc:14
zypp::json::Array::Array
Array(const std::vector< V > &cont_r)
Definition JsonValue.h:49
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:39
zypp::json::Array::size_type
std::vector< Value >::size_type size_type
Definition JsonValue.h:40
zypp::json::Array::iterator
std::vector< Value >::iterator iterator
Definition JsonValue.h:38
zypp::json::Bool
Definition JsonBool.h:23
zypp::json::Int
Definition JsonNumber.h:60
zypp::json::Null
Definition JsonNull.h:22
zypp::json::Number
Definition JsonNumber.h:20
zypp::json::Object
Definition JsonValue.h:101
zypp::json::Object::Object
Object(Iterator begin, Iterator end)
Construct from map-iterator.
Definition JsonValue.h:112
zypp::json::Object::end
const_iterator end() const
Definition JsonValue.h:140
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:106
zypp::json::Object::iterator
std::multimap< String, Value >::iterator iterator
Definition JsonValue.h:104
zypp::json::Object::begin
const_iterator begin() const
Definition JsonValue.h:139
zypp::json::Object::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonValue.cc:74
zypp::json::Object::equal_range
std::pair< const_iterator, const_iterator > equal_range(const String &key) const
Definition JsonValue.h:152
zypp::json::Object::Object
Object(std::map< K, V > values)
Definition JsonValue.h:122
zypp::json::Object::Object
Object(std::multimap< K, V > values)
Definition JsonValue.h:119
zypp::json::Object::begin
iterator begin()
Definition JsonValue.h:136
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:164
zypp::json::Object::equal_range
std::pair< iterator, iterator > equal_range(const String &key)
Definition JsonValue.h:148
zypp::json::Object::const_iterator
std::multimap< String, Value >::const_iterator const_iterator
Definition JsonValue.h:105
zypp::json::Object::size
size_type size() const
Definition JsonValue.h:144
zypp::json::Object::end
iterator end()
Definition JsonValue.h:137
zypp::json::Object::add
void add(String key_r, Value val_r)
Add key/value pair.
Definition JsonValue.cc:62
zypp::json::String
Definition JsonString.h:116
zypp::json::UInt
Definition JsonNumber.h:98
zypp::json::Value
Definition JsonValue.h:175
zypp::json::Value::asUInt
const UInt & asUInt() const
Definition JsonValue.h:259
zypp::json::Value::Value
Value(std::uint64_t val_r)
Definition JsonValue.h:214
zypp::json::Value::asString
const String & asString() const
Definition JsonValue.h:251
zypp::json::Value::Value
Value(std::uint32_t val_r)
Definition JsonValue.h:213
zypp::json::Value::asBool
const Bool & asBool() const
Definition JsonValue.h:247
zypp::json::Value::operator==
bool operator==(const Value &other) const
Definition JsonValue.h:306
zypp::json::Value::Value
Value(std::int16_t val_r)
Definition JsonValue.h:207
zypp::json::Value::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonValue.h:276
zypp::json::Value::~Value
~Value()=default
zypp::json::Value::isNull
bool isNull() const
Definition JsonValue.h:279
zypp::json::Value::Value
Value(Null val_r)
Definition JsonValue.h:231
zypp::json::Value::Value
Value(std::int8_t val_r)
Definition JsonValue.h:206
zypp::json::Value::Value
Value(float val_r)
Definition JsonValue.h:216
zypp::json::Value::Value
Value(std::int64_t val_r)
Definition JsonValue.h:209
zypp::json::Value::Value
Value(String val_r)
Definition JsonValue.h:233
zypp::json::Value::Value
Value(Int val_r)
Definition JsonValue.h:234
zypp::json::Value::operator=
Value & operator=(Value &&)=default
zypp::json::Value::type
Type type() const
Definition JsonValue.h:283
zypp::json::Value::Value
Value(UInt val_r)
Definition JsonValue.h:235
zypp::json::Value::Value
Value(std::int32_t val_r)
Definition JsonValue.h:208
zypp::json::Value::Value
Value(double val_r)
Definition JsonValue.h:217
zypp::json::Value::Value
Value(const Value &)=default
zypp::json::Value::Value
Value(Number val_r)
Definition JsonValue.h:236
zypp::json::Value::asArray
const Array & asArray() const
Definition JsonValue.h:271
zypp::json::Value::Value
Value(const char *val_r)
Definition JsonValue.h:221
zypp::json::Value::asNull
const Null & asNull() const
Definition JsonValue.h:243
zypp::json::Value::Value
Value(std::uint16_t val_r)
Definition JsonValue.h:212
zypp::json::Value::Value
Value(Object val_r)
Definition JsonValue.h:237
zypp::json::Value::Value
Value(bool val_r)
Definition JsonValue.h:203
zypp::json::Value::Value
Value(Array val_r)
Definition JsonValue.h:238
zypp::json::Value::_value
JsonValueData _value
Definition JsonValue.h:310
zypp::json::Value::Value
Value()=default
zypp::json::Value::Value
Value(std::nullptr_t)
Definition JsonValue.h:200
zypp::json::Value::JsonValueData
std::variant< Null, Bool, String, Int, UInt, Number, Object, Array > JsonValueData
Definition JsonValue.h:309
zypp::json::Value::Type
Type
Definition JsonValue.h:180
zypp::json::Value::UIntType
@ UIntType
Definition JsonValue.h:185
zypp::json::Value::ObjectType
@ ObjectType
Definition JsonValue.h:187
zypp::json::Value::IntType
@ IntType
Definition JsonValue.h:184
zypp::json::Value::StringType
@ StringType
Definition JsonValue.h:183
zypp::json::Value::NullType
@ NullType
Definition JsonValue.h:181
zypp::json::Value::BoolType
@ BoolType
Definition JsonValue.h:182
zypp::json::Value::ArrayType
@ ArrayType
Definition JsonValue.h:188
zypp::json::Value::NumberType
@ NumberType
Definition JsonValue.h:186
zypp::json::Value::Value
Value(Bool val_r)
Definition JsonValue.h:232
zypp::json::Value::Value
Value(const char val_r)
Definition JsonValue.h:220
zypp::json::Value::Value
Value(Value &&)=default
zypp::json::Value::Value
Value(std::string val_r)
Definition JsonValue.h:222
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:211
zypp::json::Value::asNumber
const Number & asNumber() const
Definition JsonValue.h:263
zypp::json::Value::asObject
const Object & asObject() const
Definition JsonValue.h:267
zypp::json::Value::asInt
const Int & asInt() const
Definition JsonValue.h:255
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<<
ZYPP_API std::ostream & operator<<(std::ostream &str, const Bool &obj)
relates: Bool Stream output
Definition JsonBool.h:59
zypp::operator==
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition Capability.h:309
zypp::contains
bool contains(const Container &c, const Elem &elem)
Definition Algorithm.h:70
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
relates: Capability Detailed stream output
Definition Capability.cc:589
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:213