control character handling and JsonNumber modification in composing
authorBINDUCHAITANYA TUMMALA <bc.tummala@samsung.com>
Wed, 3 Jul 2013 16:46:58 +0000 (22:16 +0530)
committerBINDUCHAITANYA TUMMALA <bc.tummala@samsung.com>
Wed, 3 Jul 2013 17:59:35 +0000 (23:29 +0530)
Change-Id: I727c249adf65069666ca768467360831d92797cc
Signed-off-by: BINDUCHAITANYA TUMMALA <bc.tummala@samsung.com>
src/FWebJson_JsonParserImpl.cpp
src/FWebJson_JsonWriterImpl.cpp
src/FWebJson_JsonWriterImpl.h

index d4aa059..b0d64df 100755 (executable)
@@ -35,6 +35,7 @@
 #include <FWebJsonJsonObject.h>
 #include <FWebJsonJsonParser.h>
 #include <FWebJsonJsonString.h>
+#include <FBase_LocalizedNumParser.h>
 #include <FBase_StringConverter.h>
 #include "FWebJson_JsonParserImpl.h"
 #include "FWebJson_JsonUtils.h"
@@ -459,9 +460,7 @@ _JsonParserImpl::ParseNumberN(void)
 
        SysTryReturn(NID_WEB_JSON, created, null, E_INVALID_DATA, "[E_INVALID_DATA] Input data malformed [%c] at [%d]", __pJsonData[__parserPosition], __parserPosition);
 
-       std::unique_ptr<char[]> pNumber(Tizen::Base::_StringConverter::CopyToCharArrayN(numString));
-       SysTryReturn(NID_WEB_JSON, pNumber.get(), null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
-       double number = atof(pNumber.get());
+       double number = _LocalizedNumParser::ToDouble(numString, "C");
 
        JsonNumber* pJsonNumber = new (std::nothrow) JsonNumber(number);
        SysTryReturn(NID_WEB_JSON, pJsonNumber, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
index 82d5a80..0e4a6a2 100755 (executable)
@@ -63,6 +63,9 @@ static const char ARRAY_BEGIN = '[';
 static const char ARRAY_END = ']';
 
 
+static const int DBL_MAX_LENGTH = 17 + 308 + 3;        // significant decimal digits + exponent + extra characters
+
+
 result
 _JsonWriterImpl::Compose(const IJsonValue* pValue, String& filePath)
 {
@@ -281,14 +284,11 @@ _JsonWriterImpl::ConvertIJsonToString(IJsonValue* pValue, String& outString, boo
 
        case JSON_TYPE_NUMBER:
                {
-                       String out;
-                       double val1 = static_cast<JsonNumber*>(pValue)->ToDouble();
-                       double val2 = Math::Floor(val1);
-
-                       r = (Double::Compare(val1, val2) == 0) ? out.Format(15, L"%g", val1) : out.Format(15, L"%f", val1);
-                       SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+                       String numString;
+                       r = JsonNumberToString(static_cast<JsonNumber*>(pValue)->ToDouble(), numString);
+                       SysTryReturn(NID_WEB_JSON, r == E_SUCCESS, r, r, "[%s] Propagating. numberString is [%ls]", GetErrorMessage(r), numString.GetPointer());
 
-                       outString.Append(out);
+                       outString.Append(numString);
                }
                break;
 
@@ -346,7 +346,13 @@ _JsonWriterImpl::ToEscString(const String& unEscStr, String& escString, bool unE
                        escString.Append(L"\\t");
                        break;
                default:
-                       if (unEscUnicode || pUnEscBuf[i] < 0x80)
+                       if (pUnEscBuf[i] >= 0 && pUnEscBuf[i] < 0x20)
+                       {
+                               String control;
+                               control.Format(7, L"\\u%04x", pUnEscBuf[i]);
+                               escString.Append(control);
+                       }
+                       else if (unEscUnicode || pUnEscBuf[i] < 0x80)
                        {
                                escString.Append(pUnEscBuf[i]);
                        }
@@ -364,4 +370,29 @@ _JsonWriterImpl::ToEscString(const String& unEscStr, String& escString, bool unE
 }
 
 
+result
+_JsonWriterImpl::JsonNumberToString(double value, String& numString)
+{
+       ClearLastResult();
+
+       if (Double::IsNaN(value))
+       {
+               numString = String(L"NaN");
+               return E_INVALID_DATA;
+       }
+
+       if (Double::IsInfinity(value))
+       {
+               numString = String(L"Infinity");
+               return E_INVALID_DATA;
+       }
+
+       wchar_t sValue[DBL_MAX_LENGTH + 1] = {0, };
+       swprintf(sValue, sizeof(sValue) / sizeof(sValue[0]), L"%.16lg", value);
+
+       numString = String(sValue);
+       return E_SUCCESS;
+}
+
+
 }}} // Tizen::Web::Json
index 268bba7..11d0630 100644 (file)
@@ -96,6 +96,8 @@ private:
 
        static result ToEscString(const Tizen::Base::String& unEscStr, Tizen::Base::String& escString, bool unEscUnicode = false);
 
+       static result JsonNumberToString(double value, Tizen::Base::String& numString);
+
 }; // _JsonWriterImpl
 
 }}} // Tizen::Web::Json