12#ifndef ZYPP_BASE_JSON_H
13#define ZYPP_BASE_JSON_H
22#include <zypp/base/Easy.h>
23#include <zypp/base/String.h>
32 inline static const std::string &
nullJSON() {
static const std::string _s(
"null" );
return _s; }
33 inline static const std::string &
trueJSON() {
static const std::string _s(
"true" );
return _s; }
34 inline static const std::string &
falseJSON() {
static const std::string _s(
"false" );
return _s; }
41 typedef unsigned char uchar;
43 std::string::size_type add = 2;
44 for_( r, val_r.begin(), val_r.end() )
46 if ( uchar(*r) < 32u )
75 val_r.resize( val_r.size() + add,
'@' );
76 auto w( val_r.rbegin() );
80 for ( ; r != val_r.rend(); ++r )
82 if ( uchar(*r) < 32u )
84 static const char * digit =
"0123456789abcdef";
108 *w++ = digit[uchar(*r) % 15];
109 *w++ = digit[uchar(*r) / 16];
119 switch ( (*w++ = *r) )
159 template <
class V> std::string
toJSON(
const std::vector<V> & cont_r );
160 template <
class V> std::string
toJSON(
const std::list<V> & cont_r );
161 template <
class V> std::string
toJSON(
const std::set<V> & cont_r );
164 template <
class K,
class V> std::string
toJSON(
const std::map<K,V> & cont_r );
176 std::string
toJSON(
const T & val_r ) {
return val_r.asJSON(); }
261 template <
class Iterator>
262 Array( Iterator begin, Iterator end )
263 {
for_( it, begin, end )
add( *it ); }
266 Array(
const std::initializer_list<Value> & contents_r )
267 :
Array( contents_r.begin(), contents_r.end() )
275 void add(
const std::initializer_list<Value> & contents_r )
276 {
for_( it, contents_r.begin(), contents_r.end() )
add( *it ); }
306 std::string
toJSON(
const std::vector<V> & cont_r )
310 std::string
toJSON(
const std::list<V> & cont_r )
314 std::string
toJSON(
const std::set<V> & cont_r )
326 template <
class Iterator>
328 {
for_( it, begin, end )
add( it->first, it->second ); }
331 Object(
const std::initializer_list<std::pair<String, Value>> & contents_r )
332 :
Object( contents_r.begin(), contents_r.end() )
340 void add(
const std::initializer_list<std::pair<String, Value>> & contents_r )
341 {
for_( it, contents_r.begin(), contents_r.end() )
add( it->first, it->second ); }
360 return str << endl <<
'}';
364 std::ostream &
dumpOn( std::ostream &
str, std::map<std::string,std::string>::const_iterator val_r )
const
365 {
return str << val_r->first <<
": " << val_r->second; }
367 std::map<std::string,std::string>
_data;
374 template <
class K,
class V>
375 std::string
toJSON(
const std::map<K,V> & cont_r )
String related utilities and Regular expression matching.
std::string strEncode(std::string val_r)
static const std::string & falseJSON()
static const std::string & trueJSON()
static const std::string & nullJSON()
std::string numstring(char n, int w=0)
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpOn(std::ostream &str) const
Stream output.
void add(const std::initializer_list< Value > &contents_r)
std::string asJSON() const
JSON representation.
std::ostream & operator<<(std::ostream &str, const Array &obj)
Stream output.
std::list< std::string > _data
std::string asString() const
String representation.
Array(Iterator begin, Iterator end)
Construct from container iterator.
void add(const Value &val_r)
Push JSON Value to Array.
Array(const std::initializer_list< Value > &contents_r)
Construct from container initializer list { v1, v2,... }.
Object(Iterator begin, Iterator end)
Construct from map-iterator.
void add(const String &key_r, const Value &val_r)
Add key/value pair.
std::string asJSON() const
JSON representation.
std::ostream & dumpOn(std::ostream &str, std::map< std::string, std::string >::const_iterator val_r) const
std::ostream & dumpOn(std::ostream &str) const
Stream output.
std::ostream & operator<<(std::ostream &str, const Object &obj)
Stream output.
void add(const std::initializer_list< std::pair< String, Value > > &contents_r)
std::string asString() const
String representation.
std::map< std::string, std::string > _data
Object(const std::initializer_list< std::pair< String, Value > > &contents_r)
Construct from map-initializer list { {k1,v1}, {k2,v2},... }.
JSON string Force representation as JSON string, mapping e.g.
String(const char *val_r)
String(const std::string &val_r)
JSON representation of datatypes via toJSON.
std::ostream & dumpOn(std::ostream &str) const
Stream output.
std::ostream & operator<<(std::ostream &str, const Value &obj)
Stream output.
Value()
Default ctor (null)
const std::string & asString() const
String representation.
const std::string & asJSON() const
JSON representation.
Value(const Value &rhs)
Copy ctor.
Value(const T &val_r)
Ctor creating a JSON representation of T via toJSON(T)
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
#define for_(IT, BEG, END)
Convenient for-loops using iterator.