Use float instead of double to reduce the size of JsonVariant (issue #134)

This commit is contained in:
Benoit Blanchon
2015-10-30 23:03:16 +01:00
parent 9f3ce18f06
commit c0cf9c3fcc
15 changed files with 170 additions and 81 deletions

View File

@ -16,6 +16,29 @@ using namespace ArduinoJson::Internals;
namespace ArduinoJson {
template <typename TFloat>
static TFloat parse(const char *);
template <>
FORCE_INLINE float parse<float>(const char *s) {
return static_cast<float>(strtod(s, NULL));
}
template <>
FORCE_INLINE double parse<double>(const char *s) {
return strtod(s, NULL);
}
template <>
FORCE_INLINE long parse<long>(const char *s) {
return strtol(s, NULL, 10);
}
template <>
FORCE_INLINE int parse<int>(const char *s) {
return atoi(s);
}
template <>
const char *JsonVariant::as<const char *>() const {
if (_type == JSON_UNPARSED && _content.asString &&
@ -25,29 +48,27 @@ const char *JsonVariant::as<const char *>() const {
return NULL;
}
template <>
double JsonVariant::as<double>() const {
if (_type >= JSON_DOUBLE_0_DECIMALS) return _content.asDouble;
JsonFloat JsonVariant::asFloat() const {
if (_type >= JSON_FLOAT_0_DECIMALS) return _content.asFloat;
if (_type == JSON_LONG || _type == JSON_BOOLEAN)
return static_cast<double>(_content.asLong);
if (_type == JSON_INTEGER || _type == JSON_BOOLEAN)
return static_cast<JsonFloat>(_content.asInteger);
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString)
return strtod(_content.asString, NULL);
return parse<JsonFloat>(_content.asString);
return 0.0;
}
template <>
long JsonVariant::as<long>() const {
if (_type == JSON_LONG || _type == JSON_BOOLEAN) return _content.asLong;
JsonInteger JsonVariant::asInteger() const {
if (_type == JSON_INTEGER || _type == JSON_BOOLEAN) return _content.asInteger;
if (_type >= JSON_DOUBLE_0_DECIMALS)
return static_cast<long>(_content.asDouble);
if (_type >= JSON_FLOAT_0_DECIMALS)
return static_cast<JsonInteger>(_content.asFloat);
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) {
if (!strcmp("true", _content.asString)) return 1;
return strtol(_content.asString, NULL, 10);
return parse<JsonInteger>(_content.asString);
}
return 0L;
@ -59,12 +80,12 @@ String JsonVariant::as<String>() const {
_content.asString != NULL)
return String(_content.asString);
if (_type == JSON_LONG || _type == JSON_BOOLEAN)
return String(_content.asLong);
if (_type == JSON_INTEGER || _type == JSON_BOOLEAN)
return String(_content.asInteger);
if (_type >= JSON_DOUBLE_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
return String(_content.asDouble, decimals);
if (_type >= JSON_FLOAT_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
return String(_content.asFloat, decimals);
}
String s;
@ -74,7 +95,7 @@ String JsonVariant::as<String>() const {
template <>
bool JsonVariant::is<signed long>() const {
if (_type == JSON_LONG) return true;
if (_type == JSON_INTEGER) return true;
if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
@ -87,7 +108,7 @@ bool JsonVariant::is<signed long>() const {
template <>
bool JsonVariant::is<double>() const {
if (_type >= JSON_DOUBLE_0_DECIMALS) return true;
if (_type >= JSON_FLOAT_0_DECIMALS) return true;
if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
@ -111,15 +132,15 @@ void JsonVariant::writeTo(JsonWriter &writer) const {
else if (_type == JSON_UNPARSED)
writer.writeRaw(_content.asString);
else if (_type == JSON_LONG)
writer.writeLong(_content.asLong);
else if (_type == JSON_INTEGER)
writer.writeInteger(_content.asInteger);
else if (_type == JSON_BOOLEAN)
writer.writeBoolean(_content.asLong != 0);
writer.writeBoolean(_content.asInteger != 0);
else if (_type >= JSON_DOUBLE_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
writer.writeDouble(_content.asDouble, decimals);
else if (_type >= JSON_FLOAT_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
writer.writeFloat(_content.asFloat, decimals);
}
}
}