From acbafdd298aaf6db775ef62cec2efcad5693f00e Mon Sep 17 00:00:00 2001 From: leongor Date: Sun, 13 Aug 2017 10:12:32 +0300 Subject: [PATCH 1/4] Added reset capacity to the internal JSON object. --- com.ibm.streamsx.json/impl/include/JsonReader.h | 1 + 1 file changed, 1 insertion(+) diff --git a/com.ibm.streamsx.json/impl/include/JsonReader.h b/com.ibm.streamsx.json/impl/include/JsonReader.h index 6e2b76b..4dcdaa9 100644 --- a/com.ibm.streamsx.json/impl/include/JsonReader.h +++ b/com.ibm.streamsx.json/impl/include/JsonReader.h @@ -452,6 +452,7 @@ namespace com { namespace ibm { namespace streamsx { namespace json { template inline bool parseJSON(rstring const& jsonString, Status & status, uint32_t & offset, const Index & jsonIndex) { Document & json = getDocument(); + Document(kObjectType).Swap(json); if(json.Parse(jsonString.c_str()).HasParseError()) { json.SetObject(); From 6d7e9cac0f0416a00aa4ab8e3c001fe5c437b46d Mon Sep 17 00:00:00 2001 From: leongor Date: Sun, 13 Aug 2017 16:30:54 +0300 Subject: [PATCH 2/4] Added implicit cast from boolean/number to string. --- .../impl/include/JsonReader.h | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/com.ibm.streamsx.json/impl/include/JsonReader.h b/com.ibm.streamsx.json/impl/include/JsonReader.h index 4dcdaa9..6f1420d 100644 --- a/com.ibm.streamsx.json/impl/include/JsonReader.h +++ b/com.ibm.streamsx.json/impl/include/JsonReader.h @@ -14,6 +14,8 @@ #include "rapidjson/document.h" #include "rapidjson/pointer.h" #include "rapidjson/reader.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/writer.h" #include #include @@ -539,8 +541,35 @@ namespace com { namespace ibm { namespace streamsx { namespace json { if(!value) status = 4; else if(value->IsNull()) status = 3; - else if(!value->IsString()) status = 2; - else return value->GetString(); + else { + try { + switch (value->GetType()) { + case kStringType: { + status = 0; + return value->GetString(); + } + case kFalseType: { + status = 1; + return "false"; + } + case kTrueType: { + status = 1; + return "true"; + } + case kNumberType: { + status = 1; + StringBuffer str; + Writer writer(str); + value->Accept(writer); + return str.GetString(); + } + default:; + } + } + catch(bad_lexical_cast const&) {} + + status = 2; + } return defaultVal; } From dc01334d5e28837cc8263732974443d00f4a9522 Mon Sep 17 00:00:00 2001 From: leongor Date: Thu, 7 Sep 2017 10:16:19 +0300 Subject: [PATCH 3/4] Detect numeric value type by an agrument's SPL type. --- .../impl/include/JsonReader.h | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/com.ibm.streamsx.json/impl/include/JsonReader.h b/com.ibm.streamsx.json/impl/include/JsonReader.h index 6f1420d..e66a889 100644 --- a/com.ibm.streamsx.json/impl/include/JsonReader.h +++ b/com.ibm.streamsx.json/impl/include/JsonReader.h @@ -514,13 +514,20 @@ namespace com { namespace ibm { namespace streamsx { namespace json { else if(value->IsNull()) { status = 3; } else { try { - if(value->IsInt()) { status = 0; return static_cast(value->GetInt()); } - if(value->IsUint()) { status = 0; return static_cast(value->GetUint()); } - if(value->IsInt64()) { status = 0; return static_cast(value->GetInt64()); } - if(value->IsUint64()) { status = 0; return static_cast(value->GetUint64()); } - if(value->IsFloat()) { status = 0; return static_cast(value->GetFloat()); } - if(value->IsDouble()) { status = 0; return static_cast(value->GetDouble()); } - if(value->IsString()) { status = 1; return lexical_cast(value->GetString()); } + if( is_same::value) { status = 0; return static_cast(value->GetInt()); } + if( is_same::value) { status = 0; return static_cast(value->GetInt()); } + if( is_same::value) { status = 0; return static_cast(value->GetInt()); } + if( is_same::value) { status = 0; return static_cast(value->GetInt64()); } + if( is_same::value) { status = 0; return static_cast(value->GetUint()); } + if( is_same::value) { status = 0; return static_cast(value->GetUint()); } + if( is_same::value) { status = 0; return static_cast(value->GetUint()); } + if( is_same::value) { status = 0; return static_cast(value->GetUint64()); } + if( is_same::value) { status = 0; return static_cast(value->GetFloat()); } + if( is_same::value) { status = 0; return static_cast(value->GetDouble()); } + if( is_same::value) { status = 0; return static_cast(value->GetFloat()); } + if( is_same::value) { status = 0; return static_cast(value->GetDouble()); } + if( is_same::value) { status = 0; return static_cast(value->GetDouble()); } + if(value->IsString()) { status = 1; return lexical_cast(value->GetString()); } } catch(bad_lexical_cast const&) {} From dc00e924922f3d262be93ca9f4712c699a31a158 Mon Sep 17 00:00:00 2001 From: leongor Date: Thu, 7 Sep 2017 10:50:31 +0300 Subject: [PATCH 4/4] Detect numeric value type by an agrument's SPL type. --- .../impl/include/JsonReader.h | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/com.ibm.streamsx.json/impl/include/JsonReader.h b/com.ibm.streamsx.json/impl/include/JsonReader.h index e66a889..443f619 100644 --- a/com.ibm.streamsx.json/impl/include/JsonReader.h +++ b/com.ibm.streamsx.json/impl/include/JsonReader.h @@ -510,30 +510,37 @@ namespace com { namespace ibm { namespace streamsx { namespace json { mpl::bool_< is_same::value> >::type, void*>::type t = NULL) { - if(!value) { status = 4; } - else if(value->IsNull()) { status = 3; } - else { + if(!value) + status = 4; + else if(value->IsNull()) + status = 3; + else if(value->IsNumber()) { + status = 0; + + if( is_same::value) return static_cast(value->GetInt()); + if( is_same::value) return static_cast(value->GetInt()); + if( is_same::value) return static_cast(value->GetInt()); + if( is_same::value) return static_cast(value->GetInt64()); + if( is_same::value) return static_cast(value->GetUint()); + if( is_same::value) return static_cast(value->GetUint()); + if( is_same::value) return static_cast(value->GetUint()); + if( is_same::value) return static_cast(value->GetUint64()); + if( is_same::value) return static_cast(value->GetFloat()); + if( is_same::value) return static_cast(value->GetDouble()); + if( is_same::value) return static_cast(value->GetFloat()); + if( is_same::value) return static_cast(value->GetDouble()); + if( is_same::value) return static_cast(value->GetDouble()); + } + else if(value->IsString()) { + status = 1; + try { - if( is_same::value) { status = 0; return static_cast(value->GetInt()); } - if( is_same::value) { status = 0; return static_cast(value->GetInt()); } - if( is_same::value) { status = 0; return static_cast(value->GetInt()); } - if( is_same::value) { status = 0; return static_cast(value->GetInt64()); } - if( is_same::value) { status = 0; return static_cast(value->GetUint()); } - if( is_same::value) { status = 0; return static_cast(value->GetUint()); } - if( is_same::value) { status = 0; return static_cast(value->GetUint()); } - if( is_same::value) { status = 0; return static_cast(value->GetUint64()); } - if( is_same::value) { status = 0; return static_cast(value->GetFloat()); } - if( is_same::value) { status = 0; return static_cast(value->GetDouble()); } - if( is_same::value) { status = 0; return static_cast(value->GetFloat()); } - if( is_same::value) { status = 0; return static_cast(value->GetDouble()); } - if( is_same::value) { status = 0; return static_cast(value->GetDouble()); } - if(value->IsString()) { status = 1; return lexical_cast(value->GetString()); } + return lexical_cast(value->GetString()); } catch(bad_lexical_cast const&) {} - - status = 2; } + status = 2; return defaultVal; }