From: Seongjun Yim Date: Sat, 16 Mar 2013 08:55:49 +0000 (+0900) Subject: integrate code from master to tizen_2.1 X-Git-Tag: 2.1b_release~10 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c4caaf5c1a5d7cf8930e452bf6f048ee29d74bae;p=framework%2Fosp%2Fjson.git integrate code from master to tizen_2.1 Change-Id: I6bacdad7b3bf05d4eddb9a560c369e851afe41bd Signed-off-by: Seongjun Yim --- diff --git a/inc/FWebJsonIJsonValue.h b/inc/FWebJsonIJsonValue.h old mode 100644 new mode 100755 index 068834b..81ae0d9 --- a/inc/FWebJsonIJsonValue.h +++ b/inc/FWebJsonIJsonValue.h @@ -53,7 +53,7 @@ enum JsonType * * @since 2.0 * - * The %IJsonValue interface abstracts a Json value class of a Json value type (string, bool, number, array, object, and null type). + * The %IJsonValue interface abstracts a Json value class of a %Json value type (string, bool, number, array, object, and null type). * @n * For more information on the class features, see JSON Guide. */ diff --git a/inc/FWebJsonJsonArray.h b/inc/FWebJsonJsonArray.h index 6c55039..9c32547 100755 --- a/inc/FWebJsonJsonArray.h +++ b/inc/FWebJsonJsonArray.h @@ -140,9 +140,8 @@ public: * @since 2.0 * * @return An error code - * @exception E_SUCCESS The method is successful. - * @exception E_SYSTEM A system error has occurred. - * @exception E_INVALID_OPERATION This instance has already been constructed. + * @exception E_SUCCESS The method is successful. + * @exception E_SYSTEM This exception exists only for historical reason. */ result Construct(void); @@ -153,7 +152,6 @@ public: * @since 2.0 * * @return The JSON type - * @see JsonType */ JsonType GetType(void) const; @@ -215,11 +213,11 @@ public: * @param[in] count The number of elements to be read * @param[out] index The index of the element * @exception E_SUCCESS The method is successful. - * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n - * - The specified index is outside the bounds of the data structure. @n - * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n + * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: + * - The specified index is outside the bounds of the data structure. + * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0. - * @exception E_OBJ_NOT_FOUND The @c pJsonValue is not found. + * @exception E_OBJ_NOT_FOUND The @c pJsonValue is not found. * @see LastIndexOf() */ virtual result IndexOf(IJsonValue* const& pJsonValue, int startIndex, int count, int& index) const; @@ -240,26 +238,26 @@ public: virtual result LastIndexOf(IJsonValue* const& pJsonValue, int& index) const; /** - * Checks whether the list contains the specified @c JsonValue. + * Checks whether the list contains the specified @c pJsonValue. * * @since 2.0 * - * @return @c true if the specified @c JsonValue is present in the list, @n + * @return @c true if the specified @c pJsonValue is present in the list, @n * else @c false * @param[in] pJsonValue A pointer to the JsonValue class to be located */ virtual bool Contains(IJsonValue* const& pJsonValue) const; /** - * Checks whether value of the specified instance equals to value of the current instance of @c %JsonArray. + * Checks whether value of the specified instance equals to value of the current instance of %JsonArray. * * @since 2.0 * * @return @c true if value of the current instance equals to value of the specified instance, @n - * else @c false - * @param[in] obj The element to be compared with the current instance of @c %JsonArray - * @remarks The method returns @c false if the specified element is not @c %JsonArray. - * @see Object::Equals() + * else @c false @n + * @param[in] obj The element to be compared with the current instance of %JsonArray @n + * If the specified @c obj is not %JsonArray, this method returns @c false. + * @see Tizen::Base::Object::Equals() */ virtual bool Equals(const Object& obj) const; @@ -281,8 +279,8 @@ public: * @since 2.0 * * @return An error code - * @param[in] pJsonValue A pointer to the %JsonValue class to be removed - * @param[in] deallocate Set to @c true to deallocate the @c JsonValue, @n + * @param[in] pJsonValue A pointer to the JsonValue class to be removed + * @param[in] deallocate Set to @c true to deallocate the JsonValue, @n * else @c false * @exception E_SUCCESS The method is successful. * @exception E_OBJ_NOT_FOUND The @c pJsonValue is not found. @@ -299,7 +297,7 @@ public: * * @return An error code * @param[in] collection The collection to be removed from this list - * @param[in] deallocate Set to @c true to deallocate the @c JsonValues, @n + * @param[in] deallocate Set to @c true to deallocate the JsonValues, @n * else @c false * @exception E_SUCCESS The method is successful. * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation. @n @@ -308,22 +306,23 @@ public: virtual result RemoveItems(const Tizen::Base::Collection::ICollectionT& collection, bool deallocate = false); /** - * Removes an element from the specified location. + * Removes an element from the specified location. @n + * The elements that follow the deleted element move up the list to occupy the empty location. * * @since 2.0 * * @return An error code * @param[in] index The index of the element to be removed - * @param[in] deallocate Set to @c true to deallocate the @c JsonValue, @n + * @param[in] deallocate Set to @c true to deallocate the JsonValue, @n * else @c false * @exception E_SUCCESS The method is successful. * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or the specified @c index is greater than or equal to the number of elements or less than @c 0. - * @remarks The elements that follow the deleted element move up the list to occupy the empty location. */ virtual result RemoveAt(int index, bool deallocate = false); /** - * Removes all the elements within a specified range. + * Removes all the elements within a specified range. @n + * The elements that follow the deleted elements move up the list to occupy the empty locations. * * @since 2.0 * @@ -333,11 +332,10 @@ public: * @param[in] deallocate Set to @c true to deallocate the JsonValue, @n * else @c false * @exception E_SUCCESS The method is successful. - * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n - * - The specified index is outside the bounds. @n - * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n + * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: + * - The specified index is outside the bounds of the list. + * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0. - * @remarks The elements that follow the deleted elements move up the list to occupy the empty locations. */ virtual result RemoveItems(int startIndex, int count, bool deallocate = false); @@ -361,7 +359,7 @@ public: * @param[in] index The index at which the element must be set * @exception E_SUCCESS The method is successful. * @exception E_INVALID_ARG A specified input parameter is invalid. - * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or the specified @c index is either equal to or greater than the number of elements or less than @c 0. + * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or the specified @c index is either equal to or greater than the number of elements or less than @c 0. * @see GetAt() */ virtual result SetAt(IJsonValue* const& pJsonValue, int index); @@ -374,11 +372,11 @@ public: * @return An error code * @param[in] pJsonValue A pointer to the JsonValue class to be set * @param[in] index The index at which the element must be set - * @param[in] deallocate Set to @c true to deallocate the @c JsonValue, @n + * @param[in] deallocate Set to @c true to deallocate the JsonValue, @n * else @c false * @exception E_SUCCESS The method is successful. * @exception E_INVALID_ARG A specified input parameter is invalid. - * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or the specified @c index is either equal to or greater than the number of elements or less than @c 0. + * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or the specified @c index is either equal to or greater than the number of elements or less than @c 0. * @see GetAt() */ virtual result SetAt(IJsonValue* const& pJsonValue, int index, bool deallocate); @@ -388,9 +386,10 @@ public: * * @since 2.0 * - * @return A new cloned %JsonArray - * @exception E_SUCCESS The memory is successful. - * @exception E_SYSTEM A system error has occurred. + * @return A new cloned %JsonArray or null if it fails to allocate the instance + * @exception E_SUCCESS The method is successful. + * @exception E_OUT_OF_MEMORY The memory is insufficient. + * @remarks The specific error code can be accessed using the GetLastResult() method. */ JsonArray* CloneN(void) const; diff --git a/inc/FWebJsonJsonBool.h b/inc/FWebJsonJsonBool.h old mode 100644 new mode 100755 index 481e7b6..6d31b40 --- a/inc/FWebJsonJsonBool.h +++ b/inc/FWebJsonJsonBool.h @@ -103,7 +103,7 @@ public: virtual ~JsonBool(void); /** - * Gets the type of this class (always JSON_BOOL). + * Gets the type of this class (always @c JSON_BOOL). * * @since 2.0 * diff --git a/inc/FWebJsonJsonNull.h b/inc/FWebJsonJsonNull.h old mode 100644 new mode 100755 index d191aab..e2220a4 --- a/inc/FWebJsonJsonNull.h +++ b/inc/FWebJsonJsonNull.h @@ -86,10 +86,10 @@ public: * @since 2.0 * * @return @c true if the value of the current instance equals the value of the specified instance, @n - else @c false - * @param[in] obj The object to compare with the current instance of %JsonNull - * @remarks The method returns @c false if the specified object is not %JsonNull. - * @see Object::Equals() + * else @c false + * @param[in] obj The object to compare with the current instance of %JsonNull @n + * The method returns @c false if the specified @c obj is not %JsonNull. + * @see Tizen::Base::Object::Equals() */ virtual bool Equals(const Object& obj) const; @@ -107,7 +107,7 @@ public: virtual int GetHashCode(void) const; /** - * Gets the type of the object (Always JSON_NULL) + * Gets the type of the object (Always @c JSON_NULL) * * @since 2.0 * diff --git a/inc/FWebJsonJsonNumber.h b/inc/FWebJsonJsonNumber.h old mode 100644 new mode 100755 index 9bf9f99..bfd1b48 --- a/inc/FWebJsonJsonNumber.h +++ b/inc/FWebJsonJsonNumber.h @@ -97,7 +97,6 @@ public: * @since 2.0 * * @return The type of JSON number - * @see JsonType */ JsonType GetType(void) const; diff --git a/inc/FWebJsonJsonObject.h b/inc/FWebJsonJsonObject.h index 5b2db1e..a425664 100755 --- a/inc/FWebJsonJsonObject.h +++ b/inc/FWebJsonJsonObject.h @@ -139,8 +139,7 @@ public: * * @return An error code * @exception E_SUCCESS The method is successful. - * @exception E_SYSTEM A system error has occurred. - * @exception E_INVALID_OPERATION The Construct() method cannot be called twice. + * @exception E_SYSTEM This exception exists only for historical reason. */ result Construct(void); @@ -150,7 +149,6 @@ public: * @since 2.0 * * @return The type of the JSON object - * @see JsonType */ JsonType GetType(void) const; @@ -205,11 +203,11 @@ public: * @return @c true if the value of the current instance equals the value of the specified instance, @n else @c false * @param[in] obj The object to compare @n - * This object is compared with the current instance of %JsonObject. - * @remarks This method returns @c false if the specified object is not of type JSON object. - * @see Object::Equals() + * This object is compared with the current instance of %JsonObject. @n + * This method returns @c false if the specified @c obj is not of type JSON object. + * @see Tizen::Base::Object::Equals() */ - virtual bool Equals(const Object& obj) const; + virtual bool Equals(const Object& obj) const; /** * Gets the hash value of the current instance. @@ -313,9 +311,10 @@ public: * * @since 2.0 * - * @return a new cloned %JsonObject - * @exception E_SUCCESS The memory is successful. - * @exception E_SYSTEM A system error has occurred. + * @return A new cloned %JsonArray or null if it fails to allocate the instance + * @exception E_SUCCESS The method is successful. + * @exception E_OUT_OF_MEMORY The memory is insufficient. + * @remarks The specific error code can be accessed using the GetLastResult() method. */ JsonObject* CloneN(void) const; diff --git a/inc/FWebJsonJsonParser.h b/inc/FWebJsonJsonParser.h index 66c241a..cd1a0b7 100755 --- a/inc/FWebJsonJsonParser.h +++ b/inc/FWebJsonJsonParser.h @@ -148,10 +148,11 @@ public: * @param[in] filePath The path of the JSON-encoded file * @exception E_SUCCESS The method is successful. * @exception E_INVALID_ARG The specified file path is incorrect, or the file is not found. - * @exception E_INVALID_DATA The input JSON data is invalid. + * @exception E_INVALID_DATA The input JSON data is invalid, or the buffer is empty * @exception E_MAX_EXCEEDED The input JSON data exceeds the maximum length. - * @exception E_UNKNOWN An unknown error has occurred. - * + * @exception E_ILLEGAL_ACCESS Access is denied due to insufficient permission. + * @exception E_FILE_NOT_FOUND The specified filePath cannot be found. + * @exception E_SYSTEM The method cannot proceed due to a severe system error. * @remarks The specific error code can be accessed using the GetLastResult() method. */ static IJsonValue* ParseN(const Tizen::Base::String& filePath); @@ -166,9 +167,11 @@ public: * @param[in] pBuffer A JSON-encoded buffer * @param[in] bufferLength A JSON-encoded buffer length * @exception E_SUCCESS The method is successful. + * @exception E_INVALID_ARG Either of the following conditions has occurred: @n + * - The specified @c pBuffer is @c null. @n + * - The specified bufferLength is zero or negative. * @exception E_INVALID_DATA The input JSON data is invalid, or the buffer is empty. * @exception E_MAX_EXCEEDED The input JSON data exceeds the maximum length. - * @exception E_UNKNOWN An unknown error has occurred. * * @remarks The specific error code can be accessed using the GetLastResult() method. */ @@ -185,7 +188,6 @@ public: * @exception E_SUCCESS The method is successful. * @exception E_INVALID_DATA The input JSON data is invalid, or the buffer is empty. * @exception E_MAX_EXCEEDED The input JSON data exceeds the maximum length. - * @exception E_UNKNOWN An unknown error has occurred. * * @remarks The specific error code can be accessed using the GetLastResult() method. */ diff --git a/inc/FWebJsonJsonString.h b/inc/FWebJsonJsonString.h old mode 100644 new mode 100755 index 5e3faef..8f2b44e --- a/inc/FWebJsonJsonString.h +++ b/inc/FWebJsonJsonString.h @@ -104,7 +104,6 @@ public: * @since 2.0 * * @return The type of the JSON string - * @see JsonType */ JsonType GetType(void) const; diff --git a/inc/FWebJsonJsonWriter.h b/inc/FWebJsonJsonWriter.h index 5c8a5a5..38835be 100755 --- a/inc/FWebJsonJsonWriter.h +++ b/inc/FWebJsonJsonWriter.h @@ -110,7 +110,9 @@ public: * @exception E_SUCCESS The method is successful. * @exception E_INVALID_DATA The specified @c pValue is @c null. * @exception E_INVALID_ARG The specified @c filePath is incorrect. - * @exception E_FAILURE The method has failed. + * @exception E_ILLEGAL_ACCESS Access is denied due to insufficient permission. + * @exception E_STORAGE_FULL The disk space is full. + * @exception E_SYSTEM The method cannot proceed due to a severe system error. */ static result Compose(const IJsonValue* pValue, Tizen::Base::String& filePath); @@ -120,12 +122,14 @@ public: * @since 2.0 * * @param[in] pValue JSON value to be encoded. - * @param[in] bufferLength Json encoded data buffer length. - * @param[out] pBuffer The buffer with Json encoded data. + * @param[in] bufferLength JSON encoded data buffer length. + * @param[out] pBuffer The buffer with JSON encoded data. * @return An error code * @exception E_SUCCESS The method is successful. + * @exception E_INVALID_ARG Either of the following conditions has occurred: @n + * - The specified @c pBuffer is @c null. @n + * - The specified bufferLength is negative. * @exception E_INVALID_DATA The specified @c pValue is @c null. - * @exception E_FAILURE The method has failed. */ static result Compose(const IJsonValue* pValue, void* pBuffer, int bufferLength); @@ -135,12 +139,11 @@ public: * @since 2.0 * * @param[in] pValue JSON value to be encoded. - * @param[out] buffer The buffer with JSON-encoded data + * @param[out] buffer The buffer with JSON-encoded data @n + * The specified @c buffer has to be constructed with a non-zero capacity before calling this method. * @return An error code * @exception E_SUCCESS The method is successful. * @exception E_INVALID_DATA The specified @c pValue is @c null. - * @exception E_FAILURE The method has failed. - * @remarks ByteBuffer parameter has to be constructed with a non zero capacity before calling this method. */ static result Compose(const IJsonValue* pValue, Tizen::Base::ByteBuffer& buffer); diff --git a/src/FWebJsonJsonArray.cpp b/src/FWebJsonJsonArray.cpp index e229517..d7f29e7 100644 --- a/src/FWebJsonJsonArray.cpp +++ b/src/FWebJsonJsonArray.cpp @@ -213,9 +213,12 @@ JsonArray::Equals(const Object& obj) const return true; } - //if number of key value pairs are same, we can compare element by element else they are not equal int count = GetCount(); - SysTryReturn(NID_WEB_JSON, count == pOther->GetCount(), false, E_SUCCESS, "[%s] count is not same in JsonArrays", GetErrorMessage(E_SUCCESS)); + if (count != pOther->GetCount()) + { + SysLog(NID_WEB_JSON, "count mismatch, [%d] and [%d]", count, pOther->GetCount()); + return false; + } for (int i = 0; i < count; i++) { @@ -429,61 +432,59 @@ JsonArray::CloneN(void) const { ClearLastResult(); - unique_ptr pJsonArray(new (std::nothrow) JsonArray()); - SysTryReturn(NID_WEB_JSON, pJsonArray.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); - - result r = pJsonArray->Construct(); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + JsonArray* pJsonArray = new (std::nothrow) JsonArray(); + SysTryReturn(NID_WEB_JSON, pJsonArray, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); IJsonValue* pValue = null; IJsonValue* pNewValue = null; + result r = pJsonArray->Construct(); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r)); + for (int i = 0; i < GetCount(); i++) { r = GetAt(i, pValue); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r)); switch (pValue->GetType()) { case JSON_TYPE_STRING: pNewValue = new (std::nothrow) JsonString(static_cast(pValue)->GetPointer()); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_NUMBER: pNewValue = new (std::nothrow) JsonNumber(static_cast(pValue)->ToDouble()); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_OBJECT: pNewValue = static_cast(pValue)->CloneN(); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_ARRAY: pNewValue = static_cast(pValue)->CloneN(); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_BOOL: pNewValue = new (std::nothrow) JsonBool(static_cast(pValue)->ToBool()); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_NULL: pNewValue = new (std::nothrow) JsonNull(); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; default: SysAssertf(false, "unknown Json type."); break; } + SysTryCatch(NID_WEB_JSON, pNewValue, r = E_OUT_OF_MEMORY, r, "[%s] Memory allocation failed.", GetErrorMessage(r)); r = pJsonArray->Add(pNewValue); - if (IsFailed(r)) - { - _JsonUtils::DeallocateItem(pNewValue); - SysLogException(NID_WEB_JSON, r, "[%s] Propagated.", GetErrorMessage(r)); - return null; - } + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r)); } - return pJsonArray.release(); + return pJsonArray; + +CATCH: + _JsonUtils::DeallocateItem(pNewValue); + _JsonUtils::DeallocateItem(pJsonArray); + + SetLastResult(r); + + return null; } diff --git a/src/FWebJsonJsonObject.cpp b/src/FWebJsonJsonObject.cpp index e5c1a4a..36722d6 100644 --- a/src/FWebJsonJsonObject.cpp +++ b/src/FWebJsonJsonObject.cpp @@ -75,7 +75,7 @@ public: result Compare(const StringKey& obj1, const StringKey& obj2, int& cmp) const { - SysTryReturnResult(NID_WEB_JSON, obj1 && obj2, E_INVALID_ARG, "Input parameters are null"); + SysTryReturnResult(NID_WEB_JSON, obj1 && obj2, E_INVALID_ARG, "Invalid argument(s) used. (null)"); if (*obj1 == *obj2) { @@ -119,10 +119,10 @@ JsonObject::Construct(void) //Create comparer and provider to be given to HashMap unique_ptr<_JsonObjectComparer> pJsonObjectComparer(new (std::nothrow) _JsonObjectComparer()); - SysTryReturnResult(NID_WEB_JSON, pJsonObjectComparer.get(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + SysTryReturnResult(NID_WEB_JSON, pJsonObjectComparer.get(), E_OUT_OF_MEMORY, "Memory allocation failed."); unique_ptr<_JsonObjectHashCodeProvider> pJsonObjectHashCodeProvider(new (std::nothrow) _JsonObjectHashCodeProvider()); - SysTryReturnResult(NID_WEB_JSON, pJsonObjectHashCodeProvider.get(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + SysTryReturnResult(NID_WEB_JSON, pJsonObjectHashCodeProvider.get(), E_OUT_OF_MEMORY, "Memory allocation failed."); //base class construct called to make hashmap ready result r = HashMapT::Construct(0, 0.0, *pJsonObjectHashCodeProvider, *pJsonObjectComparer); @@ -219,8 +219,11 @@ JsonObject::Equals(const Object& obj) const } int count = GetCount(); - //if number of key value pairs are same, we can compare element by element else they are not equal - SysTryReturn(NID_WEB_JSON, count == pOther->GetCount(), false, E_SUCCESS, "[%s] pair count is not same in jsonobjects", GetErrorMessage(E_SUCCESS)); + if (count != pOther->GetCount()) + { + SysLog(NID_WEB_JSON, "count mismatch, [%d] and [%d]", count, pOther->GetCount()); + return false; + } if (count == 0) { @@ -330,7 +333,7 @@ JsonObject::Remove(const String* const& pKey, bool deallocate) SysTryReturn(NID_WEB_JSON, r == E_SUCCESS && pValue, r, r, "[%s] Propagating.", GetErrorMessage(r)); unique_ptr pKeyList(GetKeysN()); - SysTryReturnResult(NID_WEB_JSON, pKeyList.get(), E_OBJ_NOT_FOUND, "[%s] Propagating.", GetErrorMessage(E_OBJ_NOT_FOUND)); + SysTryReturn(NID_WEB_JSON, pKeyList.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); //Remove from Base class r = HashMapT::Remove(pKey); @@ -407,7 +410,7 @@ JsonObject::SetValue(const String* const& pKey, IJsonValue* const& pJsonValue, b SysTryReturnResult(NID_WEB_JSON, pKey && pJsonValue, E_INVALID_ARG, "Input Argument is null"); int count = GetCount(); - SysTryReturnResult(NID_WEB_JSON, count > 0, E_OBJ_NOT_FOUND, "number of elements are zero"); + SysTryReturnResult(NID_WEB_JSON, count > 0, E_OBJ_NOT_FOUND, "Count is zero"); result r = E_OBJ_NOT_FOUND; IJsonValue* pValue = null; @@ -443,77 +446,73 @@ JsonObject::CloneN(void) const { ClearLastResult(); - result r = E_SUCCESS; - - unique_ptr pJsonObject(new (std::nothrow) JsonObject()); + JsonObject* pJsonObject = new (std::nothrow) JsonObject(); SysTryReturn(NID_WEB_JSON, pJsonObject, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); - r = pJsonObject->Construct(); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - const String* pKey = null; IJsonValue* pValue = null; unique_ptr pNewKey; IJsonValue* pNewValue = null; unique_ptr pMapEnum(GetMapEnumeratorN()); + result r = pJsonObject->Construct(); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r)); + if (pMapEnum.get()) { while (pMapEnum->MoveNext() == E_SUCCESS) { r = pMapEnum->GetKey(pKey); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); r = pMapEnum->GetValue(pValue); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); pNewKey = unique_ptr(new (std::nothrow) String(*pKey)); - SysTryReturn(NID_WEB_JSON, pNewKey.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + SysTryCatch(NID_WEB_JSON, pNewKey.get(), r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] Memory allocation failed."); switch (pValue->GetType()) { case JSON_TYPE_STRING: pNewValue = new (std::nothrow) JsonString(static_cast(pValue)->String::GetPointer()); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_NUMBER: pNewValue = new (std::nothrow) JsonNumber(static_cast(pValue)->ToDouble()); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_OBJECT: pNewValue = static_cast(pValue)->CloneN(); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_ARRAY: pNewValue = static_cast(pValue)->CloneN(); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_BOOL: pNewValue = new (std::nothrow) JsonBool(static_cast(pValue)->ToBool()); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; case JSON_TYPE_NULL: pNewValue = new (std::nothrow) JsonNull(); - SysTryReturn(NID_WEB_JSON, pNewValue, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); break; default: SysAssertf(false, "unknown Json type."); break; } + SysTryCatch(NID_WEB_JSON, pNewValue, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] Memory allocation failed."); + r = pJsonObject->Add(pNewKey.get(), pNewValue); - if (IsFailed(r)) - { - _JsonUtils::DeallocateItem(pNewValue); - SysLogException(NID_WEB_JSON, r, "[%s] Propagated.", GetErrorMessage(r)); - return null; - } + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r)); pNewKey.release(); } } + return pJsonObject; + +CATCH: + _JsonUtils::DeallocateItem(pNewValue); + _JsonUtils::DeallocateItem(pJsonObject); + + SetLastResult(r); - return pJsonObject.release(); + return null; } diff --git a/src/FWebJson_JsonParserImpl.cpp b/src/FWebJson_JsonParserImpl.cpp index 4771acb..0b002f6 100644 --- a/src/FWebJson_JsonParserImpl.cpp +++ b/src/FWebJson_JsonParserImpl.cpp @@ -83,8 +83,7 @@ _JsonParserImpl::ParseN(const String& filePath) file.Seek(FILESEEKPOSITION_BEGIN, 0); size = file.Read(pBuffer.get(), size); - r = GetLastResult(); - SysTryReturn(NID_WEB_JSON, size > 0, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryReturn(NID_WEB_JSON, size > 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); unique_ptr<_JsonParserImpl> pParserImpl(_JsonParserImpl::CreateInstanceN()); SysTryReturn(NID_WEB_JSON, pParserImpl.get(), null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); @@ -164,12 +163,12 @@ _JsonParserImpl::ParseRootN(void) switch (__pJsonData[__parserPosition]) { case '{': - pRoot = ParseObjectN(); SysLog(NID_WEB_JSON, "root is object"); + pRoot = ParseObjectN(); break; case '[': + SysLog(NID_WEB_JSON, "root is Array"); pRoot = ParseArrayN(); - SysLog(NID_WEB_JSON, "root is array"); break; default: SysLogException(NID_WEB_JSON, E_INVALID_DATA, "[E_INVALID_DATA] root is malformed [%c] at [%d]", __pJsonData[__parserPosition], __parserPosition); @@ -184,11 +183,14 @@ _JsonParserImpl::ParseRootN(void) SysTryCatch(NID_WEB_JSON, __pJsonData[__parserPosition] == '\0', , E_INVALID_DATA, "[E_INVALID_DATA] Expected EOF, Invalid character [%c] at [%d]", __pJsonData[__parserPosition], __parserPosition); } + return pRoot; CATCH: _JsonUtils::DeallocateItem(pRoot); + SetLastResult(E_INVALID_DATA); + return null; } @@ -245,36 +247,44 @@ _JsonParserImpl::ParseObjectN(void) { SysTryReturn(NID_WEB_JSON, GoToNextTokenPosition(true), null, E_INVALID_DATA, "[E_INVALID_DATA] object incomplete at [%d]", __parserPosition); - unique_ptr pJsonObject(new (std::nothrow) JsonObject()); - SysTryReturn(NID_WEB_JSON, pJsonObject.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + JsonObject* pJsonObject = new (std::nothrow) JsonObject(); + SysTryReturn(NID_WEB_JSON, pJsonObject, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed."); result r = pJsonObject->Construct(); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r)); if (__pJsonData[__parserPosition] == '}') { GoToNextTokenPosition(true); - return pJsonObject.release(); + return pJsonObject; } while (true) { //add key value pair r = AddPairToObject(*pJsonObject); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r)); if (__pJsonData[__parserPosition] != ',') { break; } - SysTryReturn(NID_WEB_JSON, GoToNextTokenPosition(true), null, E_INVALID_DATA, "[E_INVALID_DATA] object incomplete at [%d]", __parserPosition); + SysTryCatch(NID_WEB_JSON, GoToNextTokenPosition(true), r = E_INVALID_DATA, r, "[E_INVALID_DATA] object incomplete at [%d]", __parserPosition); } - SysTryReturn(NID_WEB_JSON, __pJsonData[__parserPosition] == '}', null, E_INVALID_DATA, "[E_INVALID_DATA] data malfored [%c] at [%d]", __pJsonData[__parserPosition], __parserPosition); + SysTryCatch(NID_WEB_JSON, __pJsonData[__parserPosition] == '}', r = E_INVALID_DATA, r, "[E_INVALID_DATA] data malfored [%c] at [%d]", __pJsonData[__parserPosition], __parserPosition); GoToNextTokenPosition(true); - return pJsonObject.release(); + + return pJsonObject; + +CATCH: + _JsonUtils::DeallocateItem(pJsonObject); + + SetLastResult(r); + + return null; } @@ -283,36 +293,44 @@ _JsonParserImpl::ParseArrayN(void) { SysTryReturn(NID_WEB_JSON, GoToNextTokenPosition(true), null, E_INVALID_DATA, "[E_INVALID_DATA] array incomplete at [%d]", __parserPosition); - unique_ptr pJsonArray(new (std::nothrow) JsonArray()); - SysTryReturn(NID_WEB_JSON, pJsonArray.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + JsonArray* pJsonArray = new (std::nothrow) JsonArray(); + SysTryReturn(NID_WEB_JSON, pJsonArray, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed."); result r = pJsonArray->Construct(); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r)); if (__pJsonData[__parserPosition] == ']') { GoToNextTokenPosition(true); - return pJsonArray.release(); + return pJsonArray; } while (true) { //add value to array r = AddValueToArray(*pJsonArray); - SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryCatch(NID_WEB_JSON, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r)); if (__pJsonData[__parserPosition] != ',') { break; } - SysTryReturn(NID_WEB_JSON, GoToNextTokenPosition(true), null, E_INVALID_DATA, "[E_INVALID_DATA] array incomplete at [%d]", __parserPosition); + SysTryCatch(NID_WEB_JSON, GoToNextTokenPosition(true), r = E_INVALID_DATA, r, "[E_INVALID_DATA] array incomplete at [%d]", __parserPosition); } - SysTryReturn(NID_WEB_JSON, __pJsonData[__parserPosition] == ']', null, E_INVALID_DATA, "[E_INVALID_DATA] data malfored [%c] at [%d]", __pJsonData[__parserPosition], __parserPosition); + SysTryCatch(NID_WEB_JSON, __pJsonData[__parserPosition] == ']', r = E_INVALID_DATA, r, "[E_INVALID_DATA] data malfored [%c] at [%d]", __pJsonData[__parserPosition], __parserPosition); GoToNextTokenPosition(true); - return pJsonArray.release(); + + return pJsonArray; + +CATCH: + _JsonUtils::DeallocateItem(pJsonArray); + + SetLastResult(r); + + return null; } diff --git a/src/FWebJson_JsonWriterImpl.cpp b/src/FWebJson_JsonWriterImpl.cpp index d247c7e..52ae101 100644 --- a/src/FWebJson_JsonWriterImpl.cpp +++ b/src/FWebJson_JsonWriterImpl.cpp @@ -97,8 +97,7 @@ _JsonWriterImpl::Compose(const IJsonValue* pValue, void* pBuffer, int bufferLeng SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r)); unique_ptr pText(_StringConverter::CopyToCharArrayN(output)); - r = GetLastResult(); - SysTryReturn(NID_WEB_JSON, pText.get(), r, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryReturn(NID_WEB_JSON, pText.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); int charCount = strlen(pText.get()); charCount = (charCount <= bufferLength) ? charCount : bufferLength; @@ -119,8 +118,7 @@ _JsonWriterImpl::Compose(const IJsonValue* pValue, ByteBuffer& buffer) SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r)); unique_ptr pByteBuf(StringUtil::StringToUtf8N(output)); - r = GetLastResult(); - SysTryReturn(NID_WEB_JSON, pByteBuf.get(), r, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryReturn(NID_WEB_JSON, pByteBuf.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); if (buffer.GetPointer()) { @@ -168,8 +166,7 @@ _JsonWriterImpl::ConvertIJsonToString(IJsonValue* pValue, String& outString) JsonObject* pObject = static_cast(pValue); unique_ptr pMapEnum(pObject->GetMapEnumeratorN()); - r = GetLastResult(); - SysTryReturn(NID_WEB_JSON, pMapEnum.get(), r, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryReturn(NID_WEB_JSON, pMapEnum.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); int itemCount = pObject->GetCount();