Commits

Tommaso Urli committed c2ce962

Added JSON namespace to avoid pollution.

Comments (0)

Files changed (6)

 # JSON++
 
-A Flex/Bison JSON parser for C++.
+A Flex/Bison JSON parser for C++11. Parses strings and files in JSON format, and builds an in-memory tree representing the JSON structure. JSON objects are mapped to `std::map`s, arrays to `std::vector`s, JSON native types are mapped onto C++ native types. The library also includes printing on streams. Classes exploit move semantics to avoid copying parsed structures around.
 
 ## Usage
 
 		v = parse_file("<your_json_file>.json");
 		cout << v << endl;
 		
-		return 0;
+        // Or build the object manually
+        Object obj;
+    
+        obj["foo"] = true;
+        obj["bar"] = 3;
+    
+        Object o;
+        o["given_name"] = "John";
+        o["family_name"] = "Boags";
+    
+        obj["baz"] = o;
+        
+        Array a;
+        a.push_back(true);
+        a.push_back("asia");
+        a.push_back("europe");
+        a.push_back(55);
+    
+        obj["test"] = a;
+        
+		cout << o << endl;
+        
+        return 0;
 	}
 
 ## How to build JSON++
 
 #include "json_st.hh" // JSON syntax tree
 #include "json.tab.h" // parser
-
-//extern "C"
-//{    
-    Value parse_file(const char* filename);
-    Value parse_string(const std::string& s);
-//}
+  
+JSON::Value parse_file(const char* filename);
+JSON::Value parse_string(const std::string& s);
 
 #endif
         
     void load_string(const char *);
     void load_file(FILE*);
-    Value* parsd = nullptr;
+    JSON::Value* parsd = nullptr;
 %}
 
 %code requires { #include "json_st.hh" }
     char* string_v;
     
     // Pointers to more complex classes
-    Object* object_p;
-    Array* array_p;
-    Value* value_p;
+    JSON::Object* object_p;
+    JSON::Array* array_p;
+    JSON::Value* value_p;
 } 
 
 /** Define types for union values */
 array : SQUARE_BRACKET_L list SQUARE_BRACKET_R { $$ = $2; } ;
 
 // Values rule
-value : NUMBER_I { $$ = new Value($1); }
-    | NUMBER_F { $$ = new Value($1); }
-    | BOOLEAN { $$ = new Value($1); }
-    | NULL_T { $$ = new Value(); }
-    | string { $$ = new Value(std::move(std::string($1))); delete $1; }
-    | object { $$ = new Value(std::move(*$1)); delete $1; }
-    | array { $$ = new Value(std::move(*$1)); delete $1; }
+value : NUMBER_I { $$ = new JSON::Value($1); }
+    | NUMBER_F { $$ = new JSON::Value($1); }
+    | BOOLEAN { $$ = new JSON::Value($1); }
+    | NULL_T { $$ = new JSON::Value(); }
+    | string { $$ = new JSON::Value(std::move(std::string($1))); delete $1; }
+    | object { $$ = new JSON::Value(std::move(*$1)); delete $1; }
+    | array { $$ = new JSON::Value(std::move(*$1)); delete $1; }
     ;
 
 // String rule
     };
 
 // Assignments rule
-assignment_list: /* empty */ { $$ = new Object(); } 
+assignment_list: /* empty */ { $$ = new JSON::Object(); } 
     | string COLON value {
-        $$ = new Object();
+        $$ = new JSON::Object();
         $$->insert(std::make_pair(std::string($1), std::move(*$3)));
         delete $1;
         delete $3;
     ;
     
 // List rule
-list: /* empty */ { $$ = new Array(); }
+list: /* empty */ { $$ = new JSON::Array(); }
     | value {
-        $$ = new Array();
+        $$ = new JSON::Array();
         $$->push_back(std::move(*$1));
         delete $1;
     }
     
 %%
 
-Value parse_file(const char* filename)
+JSON::Value parse_file(const char* filename)
 {    
     FILE* fh = fopen(filename, "r");
-    Value v;
+    JSON::Value v;
     
     if (fh)
     {
     return v;
 }
 
-Value parse_string(const std::string& s)
+JSON::Value parse_string(const std::string& s)
 {
     load_string(s.c_str());
     
     }
     else
     {
-        Value v = *parsd;
+        JSON::Value v = *parsd;
         delete parsd;
         return v;    
     }
 #include "json_st.hh"
 
 using namespace std;
+using namespace JSON;
 
 Value::Value() : type_t(NIL) { }
 
     _array.push_back(v);
 }
 
-void indent(ostream& os)
+void JSON::indent(ostream& os)
 {
     for (unsigned int i  = 0; i < ind; i++)
         os << "\t";
     ind++;
     for (auto e = o.begin(); e != o.end();)
     {
-        indent(os);
+        JSON::indent(os);
         os << '"' << e->first << '"' << ": " << e->second;
         if (++e != o.end())
             os << ",";
         os << endl;
     }    
     ind--;
-    indent(os);
+    JSON::indent(os);
     os << "}";
 }
 
     ind++;
     for (auto e = a.begin(); e != a.end();)
     {
-        indent(os);
+        JSON::indent(os);
         os << (*e);
         if (++e != a.end())
             os << ",";
         os << endl;
     }    
     ind--;
-    indent(os);
+    JSON::indent(os);
     os << "]";
 }
 #include <vector>
 #include <stack>
 
-/** Possible JSON type of a value (array, object, bool, ...). */
-enum ValueType
+namespace JSON 
 {
-    INT,        // JSON's int
-    FLOAT,      // JSON's float 3.14 12e-10
-    BOOL,       // JSON's boolean (true, false)
-    STRING,     // JSON's string " ... " or (not really JSON) ' ... '
-    OBJECT,     // JSON's object { ... }
-    ARRAY,      // JSON's array [ ... ]
-    NIL         // JSON's null
-};
 
-// Forward declaration
-class Value;
+    /** Possible JSON type of a value (array, object, bool, ...). */
+    enum ValueType
+    {
+        INT,        // JSON's int
+        FLOAT,      // JSON's float 3.14 12e-10
+        BOOL,       // JSON's boolean (true, false)
+        STRING,     // JSON's string " ... " or (not really JSON) ' ... '
+        OBJECT,     // JSON's object { ... }
+        ARRAY,      // JSON's array [ ... ]
+        NIL         // JSON's null
+    };
 
-/** A JSON object, i.e., a container whose keys are strings, this
-is roughly equivalent to a Python dictionary, a PHP's associative
-array, a Perl or a C++ map (depending on the implementation). */
-class Object
-{
-public:
+    // Forward declaration
+    class Value;
 
-    /** Constructor. */
-    Object();
+    /** A JSON object, i.e., a container whose keys are strings, this
+    is roughly equivalent to a Python dictionary, a PHP's associative
+    array, a Perl or a C++ map (depending on the implementation). */
+    class Object
+    {
+    public:
+
+        /** Constructor. */
+        Object();
     
-    /** Copy constructor. 
-        @param o object to copy from
-    */
-    Object(const Object& o);
+        /** Copy constructor. 
+            @param o object to copy from
+        */
+        Object(const Object& o);
     
-    /** Move constructor. */
-    Object(const Object&& o);
+        /** Move constructor. */
+        Object(const Object&& o);
     
-    /** Assignment operator. 
-        @param o object to copy from
-    */
-    Object& operator=(const Object& o);
+        /** Assignment operator. 
+            @param o object to copy from
+        */
+        Object& operator=(const Object& o);
     
-    /** Move operator. 
-        @param o object to copy from
-    */
-    Object& operator=(const Object&& o);
+        /** Move operator. 
+            @param o object to copy from
+        */
+        Object& operator=(const Object&& o);
     
-    /** Destructor. */
-    ~Object();
+        /** Destructor. */
+        ~Object();
 
-    /** Subscript operator, access an element by key.
-        @param key key of the object to access
-    */
-    Value& operator[] (const std::string& key);
+        /** Subscript operator, access an element by key.
+            @param key key of the object to access
+        */
+        Value& operator[] (const std::string& key);
 
-    /** Retrieves the starting iterator (const).
-        @remark mainly for printing
-    */
-    std::map<std::string, Value>::const_iterator begin() const;
+        /** Retrieves the starting iterator (const).
+            @remark mainly for printing
+        */
+        std::map<std::string, Value>::const_iterator begin() const;
 
-    /** Retrieves the ending iterator (const).
-        @remark mainly for printing
-    */
-    std::map<std::string, Value>::const_iterator end() const;
+        /** Retrieves the ending iterator (const).
+            @remark mainly for printing
+        */
+        std::map<std::string, Value>::const_iterator end() const;
     
-    /** Retrieves the starting iterator */
-    std::map<std::string, Value>::iterator begin();
+        /** Retrieves the starting iterator */
+        std::map<std::string, Value>::iterator begin();
 
-    /** Retrieves the ending iterator */
-    std::map<std::string, Value>::iterator end();
+        /** Retrieves the ending iterator */
+        std::map<std::string, Value>::iterator end();
     
-    /** Inserts a field in the object.
-        @param v pair <key, value> to insert
-        @return an iterator to the inserted object
-    */
-    std::pair<std::map<std::string, Value>::iterator, bool> insert(const std::pair<std::string, Value>& v);
+        /** Inserts a field in the object.
+            @param v pair <key, value> to insert
+            @return an iterator to the inserted object
+        */
+        std::pair<std::map<std::string, Value>::iterator, bool> insert(const std::pair<std::string, Value>& v);
 
-    /** Size of the object. */
-    size_t size() const;
+        /** Size of the object. */
+        size_t size() const;
 
-protected:
+    protected:
 
-    /** Inner container. */
-    std::map<std::string, Value> _object;
-};
+        /** Inner container. */
+        std::map<std::string, Value> _object;
+    };
 
-/** A JSON array, i.e., an indexed container of elements. It contains
-JSON values, that can have any of the types in ValueType. */
-class Array
-{
-public:
+    /** A JSON array, i.e., an indexed container of elements. It contains
+    JSON values, that can have any of the types in ValueType. */
+    class Array
+    {
+    public:
 
-    /** Constructor. */
-    Array();
+        /** Constructor. */
+        Array();
     
-    /** Destructor. */
-    ~Array();
+        /** Destructor. */
+        ~Array();
     
-    /** Copy constructor. 
-        @param o the object to copy from
-    */
-    Array(const Array& a);
+        /** Copy constructor. 
+            @param o the object to copy from
+        */
+        Array(const Array& a);
     
-    /** Assignment operator. 
-        @param a array to copy from
-    */
-    Array& operator=(const Array& a);
+        /** Assignment operator. 
+            @param a array to copy from
+        */
+        Array& operator=(const Array& a);
     
-    /** Copy constructor. 
-        @param o the object to copy from
-    */
-    Array(const Array&& a);
+        /** Copy constructor. 
+            @param o the object to copy from
+        */
+        Array(const Array&& a);
 
-    /** Assignment operator. 
-        @param a array to copy from
-    */
-    Array& operator=(const Array&& a);
+        /** Assignment operator. 
+            @param a array to copy from
+        */
+        Array& operator=(const Array&& a);
 
-    /** Subscript operator, access an element by index. 
-        @param i index of the element to access
-    */
-    Value& operator[] (size_t i);
+        /** Subscript operator, access an element by index. 
+            @param i index of the element to access
+        */
+        Value& operator[] (size_t i);
 
-    /** Retrieves the starting iterator (const).
-        @remark mainly for printing
-    */
-    std::vector<Value>::const_iterator begin() const;
+        /** Retrieves the starting iterator (const).
+            @remark mainly for printing
+        */
+        std::vector<Value>::const_iterator begin() const;
 
-    /** Retrieves the ending iterator (const).
-        @remark mainly for printing
-    */
-    std::vector<Value>::const_iterator end() const;
+        /** Retrieves the ending iterator (const).
+            @remark mainly for printing
+        */
+        std::vector<Value>::const_iterator end() const;
 
-    /** Retrieves the starting iterator. */
-    std::vector<Value>::iterator begin();
+        /** Retrieves the starting iterator. */
+        std::vector<Value>::iterator begin();
 
-    /** Retrieves the ending iterator */
-    std::vector<Value>::iterator end();
+        /** Retrieves the ending iterator */
+        std::vector<Value>::iterator end();
 
-    /** Inserts an element in the array.
-        @param n (a pointer to) the value to add
-    */
-    void push_back(const Value& n);
+        /** Inserts an element in the array.
+            @param n (a pointer to) the value to add
+        */
+        void push_back(const Value& n);
     
-    /** Size of the array. */
-    size_t size() const;
+        /** Size of the array. */
+        size_t size() const;
 
-protected:
+    protected:
 
-    /** Inner container. */
-    std::vector<Value> _array;
+        /** Inner container. */
+        std::vector<Value> _array;
 
-};
+    };
 
-/** A JSON value. Can have either type in ValueTypes. */
-class Value
-{
-public:
+    /** A JSON value. Can have either type in ValueTypes. */
+    class Value
+    {
+    public:
     
-    /** Default constructor (type = NIL). */
-    Value();
+        /** Default constructor (type = NIL). */
+        Value();
     
-    /** Copy constructor. */
-    Value(const Value& v);
+        /** Copy constructor. */
+        Value(const Value& v);
     
-    /** Constructor from int. */
-    Value(const int i);
+        /** Constructor from int. */
+        Value(const int i);
     
-    /** Constructor from float. */
-    Value(const float f);
+        /** Constructor from float. */
+        Value(const float f);
     
-    /** Constructor from bool. */
-    Value(const bool b);
+        /** Constructor from bool. */
+        Value(const bool b);
     
-    /** Constructor from pointer to char (C-string).  */
-    Value(const char* s);
+        /** Constructor from pointer to char (C-string).  */
+        Value(const char* s);
 
-    /** Constructor from STD string  */
-    Value(const std::string& s);
+        /** Constructor from STD string  */
+        Value(const std::string& s);
     
-    /** Constructor from pointer to Object. */
-    Value(const Object& o);
+        /** Constructor from pointer to Object. */
+        Value(const Object& o);
     
-    /** Constructor from pointer to Array. */
-    Value(const Array& a);
+        /** Constructor from pointer to Array. */
+        Value(const Array& a);
     
-    /** Move constructor. */
-    Value(const Value&& v);
+        /** Move constructor. */
+        Value(const Value&& v);
     
-    /** Move constructor from STD string  */
-    Value(const std::string&& s);
+        /** Move constructor from STD string  */
+        Value(const std::string&& s);
     
-    /** Move constructor from pointer to Object. */
-    Value(const Object&& o);
+        /** Move constructor from pointer to Object. */
+        Value(const Object&& o);
     
-    /** Move constructor from pointer to Array. */
-    Value(const Array&& a);
+        /** Move constructor from pointer to Array. */
+        Value(const Array&& a);
     
-    /** Type query. */
-    ValueType type() const
-    {
-        return type_t;
-    }
+        /** Type query. */
+        ValueType type() const
+        {
+            return type_t;
+        }
     
-    /** Assignment operator. */
-    Value& operator=(const Value& v);
+        /** Assignment operator. */
+        Value& operator=(const Value& v);
     
-    /** Move operator. */
-    Value& operator=(const Value&& v);
+        /** Move operator. */
+        Value& operator=(const Value&& v);
     
-    /** Cast operator for float */
-    operator float() const { return float_v; }
+        /** Cast operator for float */
+        operator float() const { return float_v; }
     
-    /** Cast operator for float */
-    operator int() const { return int_v; }
+        /** Cast operator for float */
+        operator int() const { return int_v; }
     
-    /** Cast operator for float */
-    operator bool() const { return bool_v; }
+        /** Cast operator for float */
+        operator bool() const { return bool_v; }
     
-    /** Cast operator for float */
-    operator std::string () const { return string_v; }
+        /** Cast operator for float */
+        operator std::string () const { return string_v; }
     
-    /** Cast operator for Object */
-    operator Object () const { return object_v; }
+        /** Cast operator for Object */
+        operator Object () const { return object_v; }
     
-    /** Cast operator for Object */
-    operator Array () const { return array_v; }
+        /** Cast operator for Object */
+        operator Array () const { return array_v; }
 
 
-protected:
+    protected:
     
-    float           float_v;
-    int             int_v;
-    bool            bool_v;
-    std::string     string_v;
+        float           float_v;
+        int             int_v;
+        bool            bool_v;
+        std::string     string_v;
     
-    Object          object_v;
-    Array           array_v;
+        Object          object_v;
+        Array           array_v;
     
-    ValueType       type_t;
-};
+        ValueType       type_t;
+    };
     
+    /** Indentation counter */
+    static unsigned int ind;
+
+    /** Print correct indentation before printing anything */
+    static void indent(std::ostream& os = std::cout);
+}
+
 /** Output operator for Values */
-std::ostream& operator<<(std::ostream&, const Value&);
+std::ostream& operator<<(std::ostream&, const JSON::Value&);
 
 /** Output operator for Objects */
-std::ostream& operator<<(std::ostream&, const Object&);
+std::ostream& operator<<(std::ostream&, const JSON::Object&);
 
 /** Output operator for Arrays */
-std::ostream& operator<<(std::ostream&, const Array&);
+std::ostream& operator<<(std::ostream&, const JSON::Array&);
 
-/** Indentation counter */
-static unsigned int ind;
 
-/** Print correct indentation before printing anything */
-static void indent(std::ostream& os = std::cout);
 
 #endif
 #include "json.hh"
 
 using namespace std;
+using namespace JSON;
 
 int main(int argc, char** argv)
 {