libzypp  17.38.17
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;
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;
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 {
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>;
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
const Object & asObject() const
Definition: JsonValue.h:267
Value(Int val_r)
Definition: JsonValue.h:234
iterator begin()
Definition: JsonValue.h:77
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition: Capability.h:309
Value(String val_r)
Definition: JsonValue.h:233
bool operator==(const Value &other) const
Definition: JsonValue.h:306
Value(UInt val_r)
Definition: JsonValue.h:235
iterator end()
Definition: JsonValue.h:78
ZYPP_API std::ostream & operator<<(std::ostream &str, const Bool &obj)
relates: Bool Stream output
Definition: JsonBool.h:59
const Null & asNull() const
Definition: JsonValue.h:243
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition: JsonValue.cc:29
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition: Patch.cc:122
Value(std::uint32_t val_r)
Definition: JsonValue.h:213
const Number & asNumber() const
Definition: JsonValue.h:263
std::vector< Value >::iterator iterator
Definition: JsonValue.h:38
Value(std::int64_t val_r)
Definition: JsonValue.h:209
Value(double val_r)
Definition: JsonValue.h:217
const String & asString() const
Definition: JsonValue.h:251
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition: JsonValue.cc:74
Value(Array val_r)
Definition: JsonValue.h:238
String related utilities and Regular expression matching.
Value(Number val_r)
Definition: JsonValue.h:236
Definition: ansi.h:854
std::vector< Value >::size_type size_type
Definition: JsonValue.h:40
const_iterator begin() const
Definition: JsonValue.h:139
std::pair< iterator, iterator > equal_range(const String &key)
Definition: JsonValue.h:148
Value(std::int8_t val_r)
Definition: JsonValue.h:206
Value(std::uint16_t val_r)
Definition: JsonValue.h:212
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
Value(std::int16_t val_r)
Definition: JsonValue.h:207
Edition * _value
Definition: SysContent.cc:311
std::multimap< String, Value > _values
Definition: JsonValue.h:164
Object(std::multimap< K, V > values)
Definition: JsonValue.h:119
const UInt & asUInt() const
Definition: JsonValue.h:259
Value(std::uint8_t val_r)
Definition: JsonValue.h:211
Type type() const
Definition: JsonValue.h:283
Object(Iterator begin, Iterator end)
Construct from map-iterator.
Definition: JsonValue.h:112
Value(bool val_r)
Definition: JsonValue.h:203
std::pair< const_iterator, const_iterator > equal_range(const String &key) const
Definition: JsonValue.h:152
bool contains(const Container &c, const Elem &elem)
Definition: Algorithm.h:70
Array(const std::vector< V > &cont_r)
Definition: JsonValue.h:49
Value(Bool val_r)
Definition: JsonValue.h:232
Value(const char val_r)
Definition: JsonValue.h:220
iterator begin()
Definition: JsonValue.h:136
std::string asJSON() const
JSON representation.
Definition: JsonValue.h:67
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:212
const Array & asArray() const
Definition: JsonValue.h:271
const Int & asInt() const
Definition: JsonValue.h:255
const_iterator end() const
Definition: JsonValue.h:81
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition: Arch.h:173
std::vector< Value > _values
Definition: JsonValue.h:91
Value(std::int32_t val_r)
Definition: JsonValue.h:208
Provides API related macros.
Value(std::nullptr_t)
Definition: JsonValue.h:200
Array(const std::list< V > &cont_r)
Definition: JsonValue.h:52
Value(Object val_r)
Definition: JsonValue.h:237
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
relates: Capability Detailed stream output
Definition: Capability.cc:589
const_iterator begin() const
Definition: JsonValue.h:80
Object(std::map< K, V > values)
Definition: JsonValue.h:122
std::string asString() const
String representation.
Definition: JsonValue.h:71
bool isNull() const
Definition: JsonValue.h:279
void add(Value val_r)
Push JSON Value to Array.
Definition: JsonValue.cc:20
Array(const std::set< V > &cont_r)
Definition: JsonValue.h:55
std::vector< Value >::const_iterator const_iterator
Definition: JsonValue.h:39
Value(std::string val_r)
Definition: JsonValue.h:222
std::variant< Null, Bool, String, Int, UInt, Number, Object, Array > JsonValueData
Definition: JsonValue.h:309
std::multimap< String, Value >::size_type size_type
Definition: JsonValue.h:106
Value(std::uint64_t val_r)
Definition: JsonValue.h:214
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition: JsonValue.h:276
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
JsonValueData _value
Definition: JsonValue.h:310
std::multimap< String, Value >::const_iterator const_iterator
Definition: JsonValue.h:105
Value(const char *val_r)
Definition: JsonValue.h:221
const Bool & asBool() const
Definition: JsonValue.h:247
typename decay< T >::type decay_t
Definition: TypeTraits.h:42
iterator end()
Definition: JsonValue.h:137
Value(Null val_r)
Definition: JsonValue.h:231
SolvableIdType size_type
Definition: PoolDefines.h:66
std::multimap< String, Value >::iterator iterator
Definition: JsonValue.h:104
Value(float val_r)
Definition: JsonValue.h:216
size_type size() const
Definition: JsonValue.h:144
const_iterator end() const
Definition: JsonValue.h:140