Skip to content

Commit 951787b

Browse files
authored
Merge pull request #91 from leongor/master
Decimal type is handled literally (no cast from double).
2 parents a7baf4f + 1e7f7b5 commit 951787b

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,14 @@ namespace com { namespace ibm { namespace streamsx { namespace json {
478478
return (uint32_t)status;
479479
}
480480

481+
template<typename T>
482+
inline T parseNumber(Value * value) {
483+
StringBuffer str;
484+
Writer<StringBuffer> writer(str);
485+
value->Accept(writer);
486+
return spl_cast<T,SPL::rstring>::cast( SPL::rstring(str.GetString()));
487+
}
488+
481489
template<typename Status>
482490
inline rstring getParseError(Status const& status) {
483491
return GetParseError_En((ParseErrorCode)status.getIndex());
@@ -527,20 +535,23 @@ namespace com { namespace ibm { namespace streamsx { namespace json {
527535
if( is_same<SPL::uint64, T>::value) return static_cast<T>(value->GetUint64());
528536
if( is_same<SPL::float32, T>::value) return static_cast<T>(value->GetFloat());
529537
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());
538+
if( is_same<SPL::decimal32, T>::value) return parseNumber<T>(value);
539+
if( is_same<SPL::decimal64, T>::value) return parseNumber<T>(value);
540+
if( is_same<SPL::decimal128, T>::value) return parseNumber<T>(value);
533541
}
534542
else if(value->IsString()) {
535543
status = 1;
536544

537545
try {
538546
return lexical_cast<T>(value->GetString());
539547
}
540-
catch(bad_lexical_cast const&) {}
548+
catch(bad_lexical_cast const&) {
549+
status = 2;
550+
}
541551
}
552+
else
553+
status = 2;
542554

543-
status = 2;
544555
return defaultVal;
545556
}
546557

@@ -572,10 +583,7 @@ namespace com { namespace ibm { namespace streamsx { namespace json {
572583
}
573584
case kNumberType: {
574585
status = 1;
575-
StringBuffer str;
576-
Writer<StringBuffer> writer(str);
577-
value->Accept(writer);
578-
return str.GetString();
586+
return parseNumber<T>(value);
579587
}
580588
default:;
581589
}

0 commit comments

Comments
 (0)