From f48933a50d4b9f60e5c606e3558cef1d5de193e1 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 19 Dec 2017 00:04:49 +0100 Subject: [PATCH] JSON lib support for double values --- lib/json.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++-- lib/json.h | 9 +++++- 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/lib/json.cpp b/lib/json.cpp index 5f5752f1..c7ea0ca0 100644 --- a/lib/json.cpp +++ b/lib/json.cpp @@ -451,9 +451,27 @@ JSON::Value::Value(std::istream & fromstream) { case '8': case '9': c = fromstream.get(); - myType = INTEGER; - intVal *= 10; - intVal += c - '0'; + if (myType != INTEGER && myType != DOUBLE){ + myType = INTEGER; + } + if (myType == INTEGER){ + intVal *= 10; + intVal += c - '0'; + }else{ + dblDivider *= 10; + dblVal += ((double)((c - '0')) / dblDivider); + } + break; + case '.': + c = fromstream.get(); + myType = DOUBLE; + if (negative){ + dblVal = -intVal; + dblDivider = -1; + }else{ + dblVal = intVal; + dblDivider = 1; + } break; case ',': if (!reading_object && !reading_array) { @@ -528,6 +546,12 @@ JSON::Value::Value(long long int val) { intVal = val; } +/// Sets this JSON::Value to the given double. +JSON::Value::Value(double val) { + myType = DOUBLE; + dblVal = val; +} + /// Sets this JSON::Value to the given integer. JSON::Value::Value(bool val) { myType = BOOL; @@ -546,6 +570,9 @@ bool JSON::Value::operator==(const JSON::Value & rhs) const { if (myType == INTEGER || myType == BOOL) { return intVal == rhs.intVal; } + if (myType == DOUBLE) { + return dblVal == rhs.dblVal; + } if (myType == STRING) { return strVal == rhs.strVal; } @@ -619,6 +646,8 @@ void JSON::Value::null() { shrink(0); strVal.clear(); intVal = 0; + dblVal = 0; + dblDivider = 1; myType = EMPTY; } @@ -632,6 +661,9 @@ JSON::Value & JSON::Value::assignFrom(const Value & rhs, const std::set 126 || strVal.size() > 200) { @@ -1319,6 +1398,11 @@ bool JSON::Value::isInt() const { return (myType == INTEGER); } +/// Returns true if this object is a double. +bool JSON::Value::isDouble() const { + return (myType == DOUBLE); +} + /// Returns true if this object is a string. bool JSON::Value::isString() const { return (myType == STRING); diff --git a/lib/json.h b/lib/json.h index 21966ec2..e3168d46 100644 --- a/lib/json.h +++ b/lib/json.h @@ -14,7 +14,7 @@ namespace JSON { /// Lists all types of JSON::Value. enum ValueType { - EMPTY, BOOL, INTEGER, STRING, ARRAY, OBJECT + EMPTY, BOOL, INTEGER, DOUBLE, STRING, ARRAY, OBJECT }; /// JSON-string-escapes a value @@ -28,6 +28,8 @@ namespace JSON { ValueType myType; long long int intVal; std::string strVal; + double dblVal; + double dblDivider; std::deque arrVal; std::map objVal; public: @@ -39,6 +41,7 @@ namespace JSON { Value(const std::string & val); Value(const char * val); Value(long long int val); + Value(double val); Value(bool val); //comparison operators bool operator==(const Value & rhs) const; @@ -52,14 +55,17 @@ namespace JSON { Value & operator=(const char * rhs); Value & operator=(const long long int & rhs); Value & operator=(const int & rhs); + Value & operator=(const double & rhs); Value & operator=(const unsigned int & rhs); Value & operator=(const bool & rhs); //converts to basic types operator long long int() const; operator std::string() const; operator bool() const; + operator double() const; const std::string asString() const; const long long int asInt() const; + const double asDouble() const; const bool asBool() const; const std::string & asStringRef() const; const char * c_str() const; @@ -87,6 +93,7 @@ namespace JSON { void removeNullMembers(); bool isMember(const std::string & name) const; bool isInt() const; + bool isDouble() const; bool isString() const; bool isBool() const; bool isObject() const;