From c3ad96f88e1315cf12ef7485bf59337f642c496f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 23 Sep 2011 13:45:09 +1000 Subject: [PATCH] Add more convenient array and object accessors --- json.cpp.rl | 9 +++++++++ json.hpp | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/json.cpp.rl b/json.cpp.rl index 6d4530ca..7b40336d 100644 --- a/json.cpp.rl +++ b/json.cpp.rl @@ -304,6 +304,15 @@ json::node::operator!=(const node &rhs) const { return !(*this == rhs); } +const json::node& +json::node::operator[] (const std::string &key) const + { return to_object ()[key]; } + + +const json::node& +json::node::operator[] (unsigned int idx) const + { return to_array()[idx]; } + /* * Object */ diff --git a/json.hpp b/json.hpp index 061089e5..c4e30bbe 100644 --- a/json.hpp +++ b/json.hpp @@ -69,8 +69,10 @@ namespace json { virtual bool operator==(const boolean &) const { return false; } virtual bool operator==(const null &) const { return false; } - virtual std::ostream& print (std::ostream &os) const = 0; + virtual const node& operator[] (const std::string&) const; + virtual const node& operator[] (unsigned int) const; + virtual std::ostream& print (std::ostream &os) const = 0; }; @@ -146,6 +148,7 @@ namespace json { { return rhs == *this; } operator const std::string&(void) const { return m_value; } + const std::string& native (void) const { return m_value; } virtual std::ostream& print (std::ostream &os) const; }; @@ -167,6 +170,7 @@ namespace json { { return rhs == *this; } operator double(void) const { return m_value; } + double native (void) const { return m_value; } virtual std::ostream& print (std::ostream &os) const; }; @@ -186,6 +190,8 @@ namespace json { virtual bool operator==(const node &rhs) const { return rhs == *this; } + bool native (void) const { return m_value; } + virtual std::ostream& print (std::ostream &os) const; };