1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2011 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #ifndef WT_JSON_OBJECT_H_
8 #define WT_JSON_OBJECT_H_
9 
10 #include <initializer_list>
11 #include <map>
12 #include <set>
13 #include <Wt/WException.h>
14 #include <Wt/Json/Value.h>
15 
16 namespace Wt {
17   namespace Json {
18 
19 class Array;
20 
21 /*! \brief A JSON object
22  *
23  * This class represents a JSON object, which defines a value map. It
24  * is implemented as a <tt>std::map&lt;std::string,Json::Value&gt;</tt>.
25  * It is therefore possible to use the std::map API to deal with it.
26  *
27  * However, a number of additional methods have been added to more
28  * conveniently query the existence and the type of contained
29  * objects. A #get() method can be used to return a member value,
30  * which returns Value::Null if the member is not defined.
31  *
32  * \ingroup json
33  */
34 class WT_API Object : public std::map<std::string, Value>
35 {
36 public:
37   /*! \brief Default constructor.
38    */
39   Object();
40 
41   /*! \brief Copy constructor.
42    */
43   Object(const Object& other);
44 
45   /*! \brief Swap operation.
46    *
47    * This swaps the contents of two objects.
48    */
49   void swap(Object& other);
50 
51   /*! \brief Assignment operator.
52    */
53   Object& operator= (const Object& other);
54   //bool operator== (const Object& other) const;
55   //bool operator!= (const Object& other) const;
56   /*! \brief Move constructor.
57    */
58   Object(Object&& other);
59 
60   /*! \brief Initializer list constructor.
61    */
62   Object(std::initializer_list<std::pair<const std::string,Value> > list);
63 
64   /*! \brief Move assignment operator.
65    */
66   Object& operator= (Object&& other);
67 
68   /*! \brief Returns the key set.
69    *
70    * This returns the member names: these are the members for which
71    * values have been associated in this object.
72    *
73    * \note This may include members with explicit Value::Null values.
74    */
75   std::set<std::string> names() const;
76 
77   /*! \brief Returns whether a member exists.
78    *
79    * This returns whether the \p name is in the names() set.
80    */
81   bool contains(const std::string& name) const;
82 
83   /*! \brief Returns the type of a given member.
84    *
85    * This method returns the type of the value for members that are included,
86    * or returns \link Wt::Json::Type::Null Json::Type::Null\endlink for when no
87    * value is associated with \p name.
88    */
89   Type type(const std::string& name) const;
90 
91   /*! \brief Returns whether the value for a member is null (or not defined).
92    *
93    * \sa get(), Value::isNull()
94    */
95   bool isNull(const std::string& name) const;
96 
97   /*! \brief Returns the value for a member (or null if not defined).
98    *
99    * Returns the value associated with member \p name or null if no value
100    * has been associated with this member name.
101    */
102   const Value& get(const std::string& name) const;
103 
104   /*! \brief Empty object constant.
105    */
106   static Object Empty;
107 
108 #ifdef WT_TARGET_JAVA
109   bool isNull() const;
110   Value& operator[](const std::string& name);
111 #endif
112 };
113 
114   }
115 }
116 
117 #endif // WT_JSON_OBJECT_H_
118