Skip to content

Commit a7baf4f

Browse files
authored
Merge pull request #88 from leongor/master
Support for auto conversion of boolean/numeric values to strings
2 parents 44aafb3 + dc00e92 commit a7baf4f

1 file changed

Lines changed: 58 additions & 14 deletions

File tree

com.ibm.streamsx.json/impl/include/JsonReader.h

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "rapidjson/document.h"
1515
#include "rapidjson/pointer.h"
1616
#include "rapidjson/reader.h"
17+
#include "rapidjson/stringbuffer.h"
18+
#include "rapidjson/writer.h"
1719

1820
#include <stack>
1921
#include <streams_boost/lexical_cast.hpp>
@@ -452,6 +454,7 @@ namespace com { namespace ibm { namespace streamsx { namespace json {
452454
template<typename Status, typename Index>
453455
inline bool parseJSON(rstring const& jsonString, Status & status, uint32_t & offset, const Index & jsonIndex) {
454456
Document & json = getDocument<Index, OperatorInstance>();
457+
Document(kObjectType).Swap(json);
455458

456459
if(json.Parse<kParseStopWhenDoneFlag>(jsonString.c_str()).HasParseError()) {
457460
json.SetObject();
@@ -507,23 +510,37 @@ namespace com { namespace ibm { namespace streamsx { namespace json {
507510
mpl::bool_< is_same<decimal128, T>::value>
508511
>::type, void*>::type t = NULL) {
509512

510-
if(!value) { status = 4; }
511-
else if(value->IsNull()) { status = 3; }
512-
else {
513+
if(!value)
514+
status = 4;
515+
else if(value->IsNull())
516+
status = 3;
517+
else if(value->IsNumber()) {
518+
status = 0;
519+
520+
if( is_same<SPL::int8, T>::value) return static_cast<T>(value->GetInt());
521+
if( is_same<SPL::int16, T>::value) return static_cast<T>(value->GetInt());
522+
if( is_same<SPL::int32, T>::value) return static_cast<T>(value->GetInt());
523+
if( is_same<SPL::int64, T>::value) return static_cast<T>(value->GetInt64());
524+
if( is_same<SPL::uint8, T>::value) return static_cast<T>(value->GetUint());
525+
if( is_same<SPL::uint16, T>::value) return static_cast<T>(value->GetUint());
526+
if( is_same<SPL::uint32, T>::value) return static_cast<T>(value->GetUint());
527+
if( is_same<SPL::uint64, T>::value) return static_cast<T>(value->GetUint64());
528+
if( is_same<SPL::float32, T>::value) return static_cast<T>(value->GetFloat());
529+
if( is_same<SPL::float64, T>::value) return static_cast<T>(value->GetDouble());
530+
if( is_same<SPL::decimal32, T>::value) return static_cast<T>(value->GetFloat());
531+
if( is_same<SPL::decimal64, T>::value) return static_cast<T>(value->GetDouble());
532+
if( is_same<SPL::decimal128, T>::value) return static_cast<T>(value->GetDouble());
533+
}
534+
else if(value->IsString()) {
535+
status = 1;
536+
513537
try {
514-
if(value->IsInt()) { status = 0; return static_cast<T>(value->GetInt()); }
515-
if(value->IsUint()) { status = 0; return static_cast<T>(value->GetUint()); }
516-
if(value->IsInt64()) { status = 0; return static_cast<T>(value->GetInt64()); }
517-
if(value->IsUint64()) { status = 0; return static_cast<T>(value->GetUint64()); }
518-
if(value->IsFloat()) { status = 0; return static_cast<T>(value->GetFloat()); }
519-
if(value->IsDouble()) { status = 0; return static_cast<T>(value->GetDouble()); }
520-
if(value->IsString()) { status = 1; return lexical_cast<T>(value->GetString()); }
538+
return lexical_cast<T>(value->GetString());
521539
}
522540
catch(bad_lexical_cast const&) {}
523-
524-
status = 2;
525541
}
526542

543+
status = 2;
527544
return defaultVal;
528545
}
529546

@@ -538,8 +555,35 @@ namespace com { namespace ibm { namespace streamsx { namespace json {
538555

539556
if(!value) status = 4;
540557
else if(value->IsNull()) status = 3;
541-
else if(!value->IsString()) status = 2;
542-
else return value->GetString();
558+
else {
559+
try {
560+
switch (value->GetType()) {
561+
case kStringType: {
562+
status = 0;
563+
return value->GetString();
564+
}
565+
case kFalseType: {
566+
status = 1;
567+
return "false";
568+
}
569+
case kTrueType: {
570+
status = 1;
571+
return "true";
572+
}
573+
case kNumberType: {
574+
status = 1;
575+
StringBuffer str;
576+
Writer<StringBuffer> writer(str);
577+
value->Accept(writer);
578+
return str.GetString();
579+
}
580+
default:;
581+
}
582+
}
583+
catch(bad_lexical_cast const&) {}
584+
585+
status = 2;
586+
}
543587

544588
return defaultVal;
545589
}

0 commit comments

Comments
 (0)