libzypp 17.37.10

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