2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FWebJsonJsonObject.h
20 * @brief This is the header file for the %JsonObject class.
22 * This header file contains the declarations of the %JsonObject class.
23 * This class represents the JSON value of type object.
25 #ifndef _FWEB_JSON_JSON_OBJECT_H_
26 #define _FWEB_JSON_JSON_OBJECT_H_
28 #include <FBaseColHashMapT.h>
29 #include <FBaseString.h>
30 #include <FWebJsonIJsonValue.h>
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Woverloaded-virtual"
37 namespace Tizen { namespace Web { namespace Json
39 class _JsonObjectImpl;
40 class _JsonObjectComparer;
41 class _JsonObjectHashCodeProvider;
42 }}} // Tizen::Web::Json
44 namespace Tizen { namespace Web { namespace Json
49 * @brief This class represents the JSON value of type object.
53 * @final This class is not intended for extension.
55 * The %JsonObject class represents the JSON value of type object.
57 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/web/json_namespace.htm">JSON Guide</a>.
59 * The following example demonstrates how to create and initialize a %JsonObject instance and how to use its methods.
62 * #include <FWebJson.h>
64 * using namespace Tizen::Base;
65 * using namespace Tizen::Base::Collection;
66 * using namespace Tizen::Web::Json;
69 * MyClass::JsonObjectSample(void)
71 * //Creates an instance of JsonObject
72 * JsonObject *pJsonObj = new JsonObject();
74 * //Construct() must be called for JsonObject
75 * pJsonObj->Construct();
77 * //Creates keys, the pointer to string is the key for the element of the JsonObject
78 * String *pStrFNKey = new String(L"firstName");
79 * String *pStrAgeKey = new String(L"age");
81 * //Creates values, the pointer to any JsonValue is the value for the element of the JsonObject
82 * JsonString *pStrFNVal = new JsonString(L"John");
83 * JsonNumber *pNumAge = new JsonNumber(25);
85 * //Adds key-value pairs to object
86 * pJsonObj->Add(pStrFNKey, pStrFNVal);
87 * pJsonObj->Add(pStrAgeKey, pNumAge);
89 * // Gets the value with the specified key
90 * IJsonValue* pValue = null;
91 * pJsonObj->GetValue(pStrFNKey, pValue);
93 * // Uses enumerator to access elements in the map
94 * const String* pKey = null;
95 * IMapEnumeratorT<const String*, IJsonValue*>* pMapEnum = null;
96 * pMapEnum = pJsonObj->GetMapEnumeratorN();
99 * while (pMapEnum->MoveNext() == E_SUCCESS)
101 * pMapEnum->GetKey(pKey);
102 * pMapEnum->GetValue(pValue);
107 * // Removes a key-value pair with memory deallocation
108 * pJsonObj->Remove(pStrAgeKey, true);
110 * // Removes all remaining key-value pairs with memory deallocation
111 * pJsonObj->RemoveAll(true);
116 class _OSP_EXPORT_ JsonObject
118 , public Tizen::Base::Collection::HashMapT<const Tizen::Base::String*, IJsonValue*>
122 * The object is not fully constructed after this constructor is called. @n
123 * For full construction, the Construct() method must be called right after calling this constructor.
130 * This destructor overrides Tizen::Base::Object::~Object().
134 virtual ~JsonObject(void);
137 * Initializes this instance of %JsonObject.
141 * @return An error code
142 * @exception E_SUCCESS The method is successful.
143 * @exception E_SYSTEM This exception exists only for historical reasons.
145 result Construct(void);
148 * Gets the type of the JSON object.
152 * @return The type of the JSON object
154 JsonType GetType(void) const;
157 * Adds the specified key-value pair to a JSON object.
161 * @return An error code
162 * @param[in] pKey A pointer to the key-value to add
163 * @param[in] pJsonValue A pointer to the value to add
164 * @exception E_SUCCESS The method is successful.
165 * @exception E_INVALID_ARG A specified input parameter is invalid, or the key comparison has failed.
166 * @exception E_OBJ_ALREADY_EXIST The specified @c pKey already exists.
169 virtual result Add(const Tizen::Base::String* const& pKey, Tizen::Web::Json::IJsonValue* const& pJsonValue);
173 * Gets the value associated with a specified key.
177 * @return The value associated with the key, @n
178 * else @c null if an exception occurs
179 * @param[in] pKey A pointer to the key to locate
180 * @param[out] pJsonValue A pointer to the value associated with the key
181 * @exception E_SUCCESS The method is successful.
182 * @exception E_INVALID_ARG A specified input parameter is invalid, or the key comparison has failed.
183 * @exception E_OBJ_NOT_FOUND The specified @c pKey is not found.
186 virtual result GetValue(const Tizen::Base::String* const& pKey, Tizen::Web::Json::IJsonValue*& pJsonValue) const;
189 * Checks whether a JSON object contains the specified value.
193 * @return @c true if the JSON object contains the specified value, @n
195 * @param[in] pJsonValue A pointer to the value to locate
197 virtual bool ContainsValue(IJsonValue* const& pJsonValue) const;
200 * Checks whether the value of the specified instance equals the value of the current instance of %JsonObject.
204 * @return @c true if the value of the current instance equals the value of the specified instance, @n
206 * @param[in] obj The object to compare @n
207 * This object is compared with the current instance of %JsonObject.
208 * @remarks This method returns @c false if the specified object is not of type JSON object.
209 * @see Tizen::Base::Object::Equals()
211 virtual bool Equals(const Object& obj) const;
214 * Gets the hash value of the current instance.
218 * @return An integer value indicating the hash value of the current instance
219 * @remarks The two equal instances must return the same hash value. For better performance,
220 * the used hash function must generate a random distribution for all inputs. @n
221 * The default implementation of this method returns the address of the current instance.
223 virtual int GetHashCode(void) const;
226 * Removes the value associated with a specified key.
230 * @return An error code
231 * @param[in] pKey A pointer to the key to remove
232 * @exception E_SUCCESS The method is successful.
233 * @exception E_INVALID_ARG The specified input parameter is invalid, or the key comparison has failed.
234 * @exception E_OBJ_NOT_FOUND The specified @c pKey is not found.
236 virtual result Remove(const Tizen::Base::String* const& pKey);
239 * Removes the value associated with a specified key.
243 * @return An error code
244 * @param[in] pKey A pointer to the key to remove
245 * @param[in] deallocate Set to @c true to deallocate the JSON value, @n
247 * @exception E_SUCCESS The method is successful.
248 * @exception E_INVALID_ARG A specified input parameter is invalid, or the key comparison has failed.
249 * @exception E_OBJ_NOT_FOUND The specified @c pKey is not found.
251 virtual result Remove(const Tizen::Base::String* const& pKey, bool deallocate);
254 * Removes all the key-value pairs in %JsonObject.
258 * @param[in] deallocate Set to @c true to deallocate the JSON value, @n
261 virtual void RemoveAll(bool deallocate = false);
264 * Sets a new value to a specified key.
268 * @return An error code
269 * @param[in] pKey A pointer to the key for which the value is to replace
270 * @param[in] pJsonValue A pointer to the new value to set
271 * @exception E_SUCCESS The method is successful.
272 * @exception E_INVALID_ARG A specified input parameter is invalid, or the key comparison has failed.
273 * @exception E_OBJ_NOT_FOUND The specified @c pKey is not found.
274 * @remarks Use the Add() method to add a new key-value pair.
276 virtual result SetValue(const Tizen::Base::String* const& pKey, Tizen::Web::Json::IJsonValue* const& pJsonValue);
279 * Sets a new value to a specified key.
283 * @return An error code
284 * @param[in] pKey A pointer to the key for which the value is to replace
285 * @param[in] pJsonValue A pointer to the new value to set
286 * @param[in] deallocate Set to @c true to deallocate the JSON value, @n
288 * @exception E_SUCCESS The method is successful.
289 * @exception E_INVALID_ARG A specified input parameter is invalid, or the key comparison has failed.
290 * @exception E_OBJ_NOT_FOUND The specified @c pKey is not found.
291 * @remarks Use the Add() method to add a new key-value pair.
293 virtual result SetValue(const Tizen::Base::String* const& pKey, Tizen::Web::Json::IJsonValue* const& pJsonValue, bool deallocate);
296 * Checks whether a JSON object contains the specified key.
300 * @return An error code
301 * @param[in] pKey The key to locate
302 * @param[out] out @c true if the JSON object contains the specified key, @n
304 * @exception E_SUCCESS The method is successful.
305 * @exception E_INVALID_ARG A specified input parameter is invalid, or the key comparison has failed.
307 virtual result ContainsKey(const Tizen::Base::String* const& pKey, bool& out) const;
310 * Returns a new cloned %JsonObject instance.
314 * @return A new cloned %JsonObject or @c null if it fails to allocate the instance
315 * @exception E_SUCCESS The method is successful.
316 * @exception E_OUT_OF_MEMORY The memory is insufficient.
317 * @remarks The specific error code can be accessed using the GetLastResult() method.
319 JsonObject* CloneN(void) const;
323 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
325 // @param[in] item The instance of the %JsonObject class to copy from
326 // @remarks This constructor is hidden.
328 JsonObject(const JsonObject& item);
331 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
333 // @param[in] item The instance of the %JsonObject class to assign from
334 // @remarks This operator is hidden.
336 JsonObject& operator =(const JsonObject& item);
339 _JsonObjectImpl* __pJsonObjectImpl;
340 _JsonObjectComparer* __pJsonObjectComparer;
341 _JsonObjectHashCodeProvider* __pJsonObjectHashCodeProvider;
342 friend class _JsonObjectImpl;
345 }}} // Tizen::Web::Json
346 #endif // _FWEB_JSON_JSON_OBJECT_H_
349 #pragma clang diagnostic pop