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

libzypp 17.38.5

JsonValue.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9
10#include "JsonValue.h"
11
12namespace zypp::json {
13
14 Array::Array() {}
15
16 Array::Array( std::initializer_list<Value> contents_r )
17 : Array( contents_r.begin(), contents_r.end() )
18 {}
19
20 void Array::add(Value val_r)
21 { _values.push_back( std::move(val_r) ); }
22
23 void Array::add(const std::initializer_list<Value> &contents_r)
24 {
25 for( const auto &v : contents_r )
26 add( v );
27 }
28
29 std::ostream &Array::dumpOn(std::ostream &str) const
30 {
31 if ( _values.empty() )
32 return str << "[]";
33 str << '[' << _values.begin()->asJSON();
34 for( auto val = ++_values.begin(); val != _values.end(); val++ )
35 str << ", " << val->asJSON();
36 return str << ']';
37 }
38
39 const Value &Array::operator[](size_type n) const
40 {
41 return _values[n];
42 }
43
44 Array::size_type Array::size() const { return _values.size(); }
45
46 Value &Array::operator[](size_type n)
47 {
48 return _values[n];
49 }
50
51 bool Array::operator==(const Array &other) const
52 {
53 return (other._values == _values);
54 }
55
56 Object::Object() {}
57
58 Object::Object(const std::initializer_list<std::pair<String, Value> > &contents_r)
59 : Object( contents_r.begin(), contents_r.end() )
60 {}
61
62 void Object::add(String key_r, Value val_r )
63 { _values.insert( std::make_pair( std::move(key_r), std::move(val_r)) ); }
64
65 void Object::add(std::initializer_list<std::pair<String, Value> > contents_r)
66 {
67 for( const auto &val : contents_r )
68 add( val.first, val.second );
69 }
70
71 std::string Object::asJSON() const
72 { return str::Str() << *this; }
73
74 std::ostream &Object::dumpOn(std::ostream &str) const
75 {
76 using std::endl;
77 if ( _values.empty() )
78 return str << "{}";
79 dumpOn( str << '{' << endl, _values.begin() );
80 for_ ( val, ++_values.begin(), _values.end() )
81 dumpOn( str << ',' << endl, val );
82 return str << endl << '}';
83 }
84
85 bool Object::contains(const String &key) const {
86 return ( _values.find(key) != _values.end() );
87 }
88
89 std::vector<Value> Object::values(const String &key) const
90 {
91 std::vector<Value> vs;
92 auto range = _values.equal_range ( key );
93 std::for_each( range.first, range.second, [&]( const auto &v) { vs.push_back (v.second);} );
94 return vs;
95 }
96
97 const Value &Object::value(const String &key) const
98 {
99 auto i = _values.find (key);
100 return i->second;
101 }
102
103 bool Object::operator==(const Object &other) const {
104 return (_values == other._values);
105 }
106
107 std::ostream &Object::dumpOn(std::ostream &str, std::map<String, Value>::const_iterator val_r) const
108 { return str << val_r->first << ": " << val_r->second; }
109
110 std::string Value::asJSON() const
111 {
112 return std::visit( []( auto &&val ) { return val.asJSON(); }, _value );
113 }
114} // namespace zypp::json
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
JsonValue.h
zypp::json::Array::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonValue.cc:29
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::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::begin
iterator begin()
Definition JsonValue.h:75
zypp::json::Array::Array
Array()
Definition JsonValue.cc:14
zypp::json::Array::add
void add(Value val_r)
Push JSON Value to Array.
Definition JsonValue.cc:20
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::Object::Object
Object()
Definition JsonValue.cc:56
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::contains
bool contains(const String &key) const
Definition JsonValue.cc:85
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::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::Value
Definition JsonValue.h:173
zypp::json::Value::_value
JsonValueData _value
Definition JsonValue.h:308
zypp::json::Value::asJSON
std::string asJSON() const
JSON representation.
Definition JsonValue.cc:110
str
String related utilities and Regular expression matching.
zypp::json
Definition TargetImpl.cc:151
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:213