mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-02 00:38:26 +01:00
Added overflow handling in JsonVariant::as<T>() and JsonVariant::is<T>()
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Misc/SerializedValue.hpp"
|
||||
#include "../Numbers/convertNumber.hpp"
|
||||
#include "../Polyfills/gsl/not_null.hpp"
|
||||
#include "VariantContent.hpp"
|
||||
|
||||
@@ -63,9 +64,7 @@ class VariantData {
|
||||
|
||||
const char *asString() const;
|
||||
|
||||
bool asBoolean() const {
|
||||
return asIntegral<int>() != 0;
|
||||
}
|
||||
bool asBoolean() const;
|
||||
|
||||
CollectionData *asArray() {
|
||||
return isArray() ? &_content.asCollection : 0;
|
||||
@@ -147,9 +146,18 @@ class VariantData {
|
||||
return (_flags & COLLECTION_MASK) != 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool isInteger() const {
|
||||
return type() == VALUE_IS_POSITIVE_INTEGER ||
|
||||
type() == VALUE_IS_NEGATIVE_INTEGER;
|
||||
switch (type()) {
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
return canStorePositiveInteger<T>(_content.asInteger);
|
||||
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return canStoreNegativeInteger<T>(_content.asInteger);
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isFloat() const {
|
||||
@@ -225,14 +233,22 @@ class VariantData {
|
||||
template <typename T>
|
||||
void setSignedInteger(T value) {
|
||||
if (value >= 0) {
|
||||
setType(VALUE_IS_POSITIVE_INTEGER);
|
||||
_content.asInteger = static_cast<UInt>(value);
|
||||
setPositiveInteger(static_cast<UInt>(value));
|
||||
} else {
|
||||
setType(VALUE_IS_NEGATIVE_INTEGER);
|
||||
_content.asInteger = ~static_cast<UInt>(value) + 1;
|
||||
setNegativeInteger(~static_cast<UInt>(value) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void setPositiveInteger(UInt value) {
|
||||
setType(VALUE_IS_POSITIVE_INTEGER);
|
||||
_content.asInteger = value;
|
||||
}
|
||||
|
||||
void setNegativeInteger(UInt value) {
|
||||
setType(VALUE_IS_NEGATIVE_INTEGER);
|
||||
_content.asInteger = value;
|
||||
}
|
||||
|
||||
void setLinkedString(const char *value) {
|
||||
if (value) {
|
||||
setType(VALUE_IS_LINKED_STRING);
|
||||
|
||||
@@ -52,8 +52,9 @@ inline bool variantIsBoolean(const VariantData *var) {
|
||||
return var && var->isBoolean();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool variantIsInteger(const VariantData *var) {
|
||||
return var && var->isInteger();
|
||||
return var && var->isInteger<T>();
|
||||
}
|
||||
|
||||
inline bool variantIsFloat(const VariantData *var) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Numbers/convertNumber.hpp"
|
||||
#include "../Numbers/parseFloat.hpp"
|
||||
#include "../Numbers/parseInteger.hpp"
|
||||
#include "VariantRef.hpp"
|
||||
@@ -18,19 +19,35 @@ inline T VariantData::asIntegral() const {
|
||||
switch (type()) {
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
case VALUE_IS_BOOLEAN:
|
||||
return T(_content.asInteger);
|
||||
return convertPositiveInteger<T>(_content.asInteger);
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return T(~_content.asInteger + 1);
|
||||
return convertNegativeInteger<T>(_content.asInteger);
|
||||
case VALUE_IS_LINKED_STRING:
|
||||
case VALUE_IS_OWNED_STRING:
|
||||
return parseInteger<T>(_content.asString);
|
||||
case VALUE_IS_FLOAT:
|
||||
return T(_content.asFloat);
|
||||
return convertFloat<T>(_content.asFloat);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool VariantData::asBoolean() const {
|
||||
switch (type()) {
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
case VALUE_IS_BOOLEAN:
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return _content.asInteger != 0;
|
||||
case VALUE_IS_FLOAT:
|
||||
return _content.asFloat != 0;
|
||||
case VALUE_IS_LINKED_STRING:
|
||||
case VALUE_IS_OWNED_STRING:
|
||||
return strcmp("true", _content.asString) == 0;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// T = float/double
|
||||
template <typename T>
|
||||
inline T VariantData::asFloat() const {
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Misc/Visitable.hpp"
|
||||
#include "../Numbers/parseFloat.hpp"
|
||||
#include "../Numbers/parseInteger.hpp"
|
||||
#include "../Operators/VariantOperators.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "VariantAs.hpp"
|
||||
@@ -45,7 +43,7 @@ class VariantRefBase {
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<is_integral<T>::value, bool>::type is()
|
||||
const {
|
||||
return variantIsInteger(_data);
|
||||
return variantIsInteger<T>(_data);
|
||||
}
|
||||
//
|
||||
// bool is<double>() const;
|
||||
|
||||
Reference in New Issue
Block a user