JSON lib support for double values

This commit is contained in:
Thulinma 2017-12-19 00:04:49 +01:00
parent e4ac68db54
commit f48933a50d
2 changed files with 95 additions and 4 deletions

View file

@ -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<std::str
if (myType == BOOL || myType == INTEGER){
intVal = rhs.intVal;
}
if (myType == DOUBLE){
dblVal = rhs.dblVal;
}
if (myType == OBJECT){
jsonForEachConst(rhs, i){
if (!skip.count(i.key())){
@ -661,6 +693,9 @@ JSON::Value & JSON::Value::operator=(const JSON::Value & rhs) {
if (myType == BOOL || myType == INTEGER){
intVal = rhs.intVal;
}
if (myType == DOUBLE){
dblVal = rhs.dblVal;
}
if (myType == OBJECT){
jsonForEachConst(rhs, i){
(*this)[i.key()] = *i;
@ -708,6 +743,14 @@ JSON::Value & JSON::Value::operator=(const int & rhs) {
return ((*this) = (long long int)rhs);
}
/// Sets this JSON::Value to the given double.
JSON::Value & JSON::Value::operator=(const double & rhs) {
null();
myType = DOUBLE;
dblVal = rhs;
return *this;
}
/// Sets this JSON::Value to the given integer.
JSON::Value & JSON::Value::operator=(const unsigned int & rhs) {
return ((*this) = (long long int)rhs);
@ -718,12 +761,29 @@ JSON::Value::operator long long int() const {
if (myType == INTEGER) {
return intVal;
}
if (myType == DOUBLE) {
return (long long int)dblVal;
}
if (myType == STRING) {
return atoll(strVal.c_str());
}
return 0;
}
/// Automatic conversion to double - returns 0 if not convertable.
JSON::Value::operator double() const {
if (myType == INTEGER) {
return (double)intVal;
}
if (myType == DOUBLE) {
return dblVal;
}
if (myType == STRING) {
return atof(strVal.c_str());
}
return 0;
}
/// Automatic conversion to std::string.
/// Returns the raw string value if available, otherwise calls toString().
JSON::Value::operator std::string() const {
@ -747,6 +807,9 @@ JSON::Value::operator bool() const {
if (myType == INTEGER) {
return intVal != 0;
}
if (myType == DOUBLE) {
return dblVal != 0;
}
if (myType == BOOL) {
return intVal != 0;
}
@ -770,6 +833,10 @@ const std::string JSON::Value::asString() const {
const long long int JSON::Value::asInt() const {
return (long long int) * this;
}
/// Explicit conversion to double.
const double JSON::Value::asDouble() const {
return (double) * this;
}
/// Explicit conversion to bool.
const bool JSON::Value::asBool() const {
return (bool) * this;
@ -1127,6 +1194,12 @@ std::string JSON::Value::toString() const {
return st.str();
break;
}
case DOUBLE: {
std::stringstream st;
st << dblVal;
return st.str();
break;
}
case BOOL: {
if (intVal != 0){
return "true";
@ -1185,6 +1258,12 @@ std::string JSON::Value::toPrettyString(int indentation) const {
return st.str();
break;
}
case DOUBLE: {
std::stringstream st;
st << dblVal;
return st.str();
break;
}
case STRING: {
for (unsigned int i = 0; i < 201 && i < strVal.size(); ++i) {
if (strVal[i] < 32 || strVal[i] > 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);

View file

@ -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<Value*> arrVal;
std::map<std::string, Value*> 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;