Add simple json::object accessors
This commit is contained in:
parent
e618fb4236
commit
cd9762a94a
25
json.cpp.rl
25
json.cpp.rl
@ -384,6 +384,31 @@ json::object::operator[](const std::string &key) const {
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
json::object::clear (void)
|
||||
{ m_values.clear (); }
|
||||
|
||||
|
||||
void
|
||||
json::object::erase (const std::string &key) {
|
||||
auto pos = m_values.find (key);
|
||||
if (pos == m_values.end ())
|
||||
throw json::error ("erasing invalid key");
|
||||
|
||||
m_values.erase (key);
|
||||
}
|
||||
|
||||
|
||||
json::object::const_iterator
|
||||
json::object::begin (void) const
|
||||
{ return m_values.begin (); }
|
||||
|
||||
|
||||
json::object::const_iterator
|
||||
json::object::end (void) const
|
||||
{ return m_values.end (); }
|
||||
|
||||
|
||||
std::ostream&
|
||||
json::object::write (std::ostream &os) const {
|
||||
os << "{\n";
|
||||
|
14
json.hpp
14
json.hpp
@ -87,7 +87,11 @@ namespace json {
|
||||
/// Represents a JSON object, and contains its children.
|
||||
class object : public node {
|
||||
protected:
|
||||
std::map<std::string, std::unique_ptr<node>> m_values;
|
||||
typedef std::map<std::string, std::unique_ptr<node>> value_store;
|
||||
typedef value_store::iterator iterator;
|
||||
typedef value_store::const_iterator const_iterator;
|
||||
|
||||
value_store m_values;
|
||||
|
||||
public:
|
||||
object () { ; }
|
||||
@ -99,9 +103,15 @@ namespace json {
|
||||
virtual bool operator==(const node &rhs) const
|
||||
{ return rhs == *this; }
|
||||
|
||||
virtual void insert (const std::string &_key, std::unique_ptr<node>&& value);
|
||||
virtual void insert (const std::string &key, std::unique_ptr<node>&& value);
|
||||
virtual const node& operator[](const std::string &key) const;
|
||||
|
||||
virtual void clear (void);
|
||||
virtual void erase (const std::string &key);
|
||||
|
||||
virtual const_iterator begin (void) const;
|
||||
virtual const_iterator end (void) const;
|
||||
|
||||
virtual std::ostream& write (std::ostream &os) const;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user