JSON lib support for double values
This commit is contained in:
parent
e4ac68db54
commit
f48933a50d
2 changed files with 95 additions and 4 deletions
90
lib/json.cpp
90
lib/json.cpp
|
@ -451,9 +451,27 @@ JSON::Value::Value(std::istream & fromstream) {
|
||||||
case '8':
|
case '8':
|
||||||
case '9':
|
case '9':
|
||||||
c = fromstream.get();
|
c = fromstream.get();
|
||||||
myType = INTEGER;
|
if (myType != INTEGER && myType != DOUBLE){
|
||||||
intVal *= 10;
|
myType = INTEGER;
|
||||||
intVal += c - '0';
|
}
|
||||||
|
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;
|
break;
|
||||||
case ',':
|
case ',':
|
||||||
if (!reading_object && !reading_array) {
|
if (!reading_object && !reading_array) {
|
||||||
|
@ -528,6 +546,12 @@ JSON::Value::Value(long long int val) {
|
||||||
intVal = 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.
|
/// Sets this JSON::Value to the given integer.
|
||||||
JSON::Value::Value(bool val) {
|
JSON::Value::Value(bool val) {
|
||||||
myType = BOOL;
|
myType = BOOL;
|
||||||
|
@ -546,6 +570,9 @@ bool JSON::Value::operator==(const JSON::Value & rhs) const {
|
||||||
if (myType == INTEGER || myType == BOOL) {
|
if (myType == INTEGER || myType == BOOL) {
|
||||||
return intVal == rhs.intVal;
|
return intVal == rhs.intVal;
|
||||||
}
|
}
|
||||||
|
if (myType == DOUBLE) {
|
||||||
|
return dblVal == rhs.dblVal;
|
||||||
|
}
|
||||||
if (myType == STRING) {
|
if (myType == STRING) {
|
||||||
return strVal == rhs.strVal;
|
return strVal == rhs.strVal;
|
||||||
}
|
}
|
||||||
|
@ -619,6 +646,8 @@ void JSON::Value::null() {
|
||||||
shrink(0);
|
shrink(0);
|
||||||
strVal.clear();
|
strVal.clear();
|
||||||
intVal = 0;
|
intVal = 0;
|
||||||
|
dblVal = 0;
|
||||||
|
dblDivider = 1;
|
||||||
myType = EMPTY;
|
myType = EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -632,6 +661,9 @@ JSON::Value & JSON::Value::assignFrom(const Value & rhs, const std::set<std::str
|
||||||
if (myType == BOOL || myType == INTEGER){
|
if (myType == BOOL || myType == INTEGER){
|
||||||
intVal = rhs.intVal;
|
intVal = rhs.intVal;
|
||||||
}
|
}
|
||||||
|
if (myType == DOUBLE){
|
||||||
|
dblVal = rhs.dblVal;
|
||||||
|
}
|
||||||
if (myType == OBJECT){
|
if (myType == OBJECT){
|
||||||
jsonForEachConst(rhs, i){
|
jsonForEachConst(rhs, i){
|
||||||
if (!skip.count(i.key())){
|
if (!skip.count(i.key())){
|
||||||
|
@ -661,6 +693,9 @@ JSON::Value & JSON::Value::operator=(const JSON::Value & rhs) {
|
||||||
if (myType == BOOL || myType == INTEGER){
|
if (myType == BOOL || myType == INTEGER){
|
||||||
intVal = rhs.intVal;
|
intVal = rhs.intVal;
|
||||||
}
|
}
|
||||||
|
if (myType == DOUBLE){
|
||||||
|
dblVal = rhs.dblVal;
|
||||||
|
}
|
||||||
if (myType == OBJECT){
|
if (myType == OBJECT){
|
||||||
jsonForEachConst(rhs, i){
|
jsonForEachConst(rhs, i){
|
||||||
(*this)[i.key()] = *i;
|
(*this)[i.key()] = *i;
|
||||||
|
@ -708,6 +743,14 @@ JSON::Value & JSON::Value::operator=(const int & rhs) {
|
||||||
return ((*this) = (long long 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.
|
/// Sets this JSON::Value to the given integer.
|
||||||
JSON::Value & JSON::Value::operator=(const unsigned int & rhs) {
|
JSON::Value & JSON::Value::operator=(const unsigned int & rhs) {
|
||||||
return ((*this) = (long long int)rhs);
|
return ((*this) = (long long int)rhs);
|
||||||
|
@ -718,12 +761,29 @@ JSON::Value::operator long long int() const {
|
||||||
if (myType == INTEGER) {
|
if (myType == INTEGER) {
|
||||||
return intVal;
|
return intVal;
|
||||||
}
|
}
|
||||||
|
if (myType == DOUBLE) {
|
||||||
|
return (long long int)dblVal;
|
||||||
|
}
|
||||||
if (myType == STRING) {
|
if (myType == STRING) {
|
||||||
return atoll(strVal.c_str());
|
return atoll(strVal.c_str());
|
||||||
}
|
}
|
||||||
return 0;
|
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.
|
/// Automatic conversion to std::string.
|
||||||
/// Returns the raw string value if available, otherwise calls toString().
|
/// Returns the raw string value if available, otherwise calls toString().
|
||||||
JSON::Value::operator std::string() const {
|
JSON::Value::operator std::string() const {
|
||||||
|
@ -747,6 +807,9 @@ JSON::Value::operator bool() const {
|
||||||
if (myType == INTEGER) {
|
if (myType == INTEGER) {
|
||||||
return intVal != 0;
|
return intVal != 0;
|
||||||
}
|
}
|
||||||
|
if (myType == DOUBLE) {
|
||||||
|
return dblVal != 0;
|
||||||
|
}
|
||||||
if (myType == BOOL) {
|
if (myType == BOOL) {
|
||||||
return intVal != 0;
|
return intVal != 0;
|
||||||
}
|
}
|
||||||
|
@ -770,6 +833,10 @@ const std::string JSON::Value::asString() const {
|
||||||
const long long int JSON::Value::asInt() const {
|
const long long int JSON::Value::asInt() const {
|
||||||
return (long long int) * this;
|
return (long long int) * this;
|
||||||
}
|
}
|
||||||
|
/// Explicit conversion to double.
|
||||||
|
const double JSON::Value::asDouble() const {
|
||||||
|
return (double) * this;
|
||||||
|
}
|
||||||
/// Explicit conversion to bool.
|
/// Explicit conversion to bool.
|
||||||
const bool JSON::Value::asBool() const {
|
const bool JSON::Value::asBool() const {
|
||||||
return (bool) * this;
|
return (bool) * this;
|
||||||
|
@ -1127,6 +1194,12 @@ std::string JSON::Value::toString() const {
|
||||||
return st.str();
|
return st.str();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case DOUBLE: {
|
||||||
|
std::stringstream st;
|
||||||
|
st << dblVal;
|
||||||
|
return st.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
case BOOL: {
|
case BOOL: {
|
||||||
if (intVal != 0){
|
if (intVal != 0){
|
||||||
return "true";
|
return "true";
|
||||||
|
@ -1185,6 +1258,12 @@ std::string JSON::Value::toPrettyString(int indentation) const {
|
||||||
return st.str();
|
return st.str();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case DOUBLE: {
|
||||||
|
std::stringstream st;
|
||||||
|
st << dblVal;
|
||||||
|
return st.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
case STRING: {
|
case STRING: {
|
||||||
for (unsigned int i = 0; i < 201 && i < strVal.size(); ++i) {
|
for (unsigned int i = 0; i < 201 && i < strVal.size(); ++i) {
|
||||||
if (strVal[i] < 32 || strVal[i] > 126 || strVal.size() > 200) {
|
if (strVal[i] < 32 || strVal[i] > 126 || strVal.size() > 200) {
|
||||||
|
@ -1319,6 +1398,11 @@ bool JSON::Value::isInt() const {
|
||||||
return (myType == INTEGER);
|
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.
|
/// Returns true if this object is a string.
|
||||||
bool JSON::Value::isString() const {
|
bool JSON::Value::isString() const {
|
||||||
return (myType == STRING);
|
return (myType == STRING);
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace JSON {
|
||||||
|
|
||||||
/// Lists all types of JSON::Value.
|
/// Lists all types of JSON::Value.
|
||||||
enum ValueType {
|
enum ValueType {
|
||||||
EMPTY, BOOL, INTEGER, STRING, ARRAY, OBJECT
|
EMPTY, BOOL, INTEGER, DOUBLE, STRING, ARRAY, OBJECT
|
||||||
};
|
};
|
||||||
|
|
||||||
/// JSON-string-escapes a value
|
/// JSON-string-escapes a value
|
||||||
|
@ -28,6 +28,8 @@ namespace JSON {
|
||||||
ValueType myType;
|
ValueType myType;
|
||||||
long long int intVal;
|
long long int intVal;
|
||||||
std::string strVal;
|
std::string strVal;
|
||||||
|
double dblVal;
|
||||||
|
double dblDivider;
|
||||||
std::deque<Value*> arrVal;
|
std::deque<Value*> arrVal;
|
||||||
std::map<std::string, Value*> objVal;
|
std::map<std::string, Value*> objVal;
|
||||||
public:
|
public:
|
||||||
|
@ -39,6 +41,7 @@ namespace JSON {
|
||||||
Value(const std::string & val);
|
Value(const std::string & val);
|
||||||
Value(const char * val);
|
Value(const char * val);
|
||||||
Value(long long int val);
|
Value(long long int val);
|
||||||
|
Value(double val);
|
||||||
Value(bool val);
|
Value(bool val);
|
||||||
//comparison operators
|
//comparison operators
|
||||||
bool operator==(const Value & rhs) const;
|
bool operator==(const Value & rhs) const;
|
||||||
|
@ -52,14 +55,17 @@ namespace JSON {
|
||||||
Value & operator=(const char * rhs);
|
Value & operator=(const char * rhs);
|
||||||
Value & operator=(const long long int & rhs);
|
Value & operator=(const long long int & rhs);
|
||||||
Value & operator=(const int & rhs);
|
Value & operator=(const int & rhs);
|
||||||
|
Value & operator=(const double & rhs);
|
||||||
Value & operator=(const unsigned int & rhs);
|
Value & operator=(const unsigned int & rhs);
|
||||||
Value & operator=(const bool & rhs);
|
Value & operator=(const bool & rhs);
|
||||||
//converts to basic types
|
//converts to basic types
|
||||||
operator long long int() const;
|
operator long long int() const;
|
||||||
operator std::string() const;
|
operator std::string() const;
|
||||||
operator bool() const;
|
operator bool() const;
|
||||||
|
operator double() const;
|
||||||
const std::string asString() const;
|
const std::string asString() const;
|
||||||
const long long int asInt() const;
|
const long long int asInt() const;
|
||||||
|
const double asDouble() const;
|
||||||
const bool asBool() const;
|
const bool asBool() const;
|
||||||
const std::string & asStringRef() const;
|
const std::string & asStringRef() const;
|
||||||
const char * c_str() const;
|
const char * c_str() const;
|
||||||
|
@ -87,6 +93,7 @@ namespace JSON {
|
||||||
void removeNullMembers();
|
void removeNullMembers();
|
||||||
bool isMember(const std::string & name) const;
|
bool isMember(const std::string & name) const;
|
||||||
bool isInt() const;
|
bool isInt() const;
|
||||||
|
bool isDouble() const;
|
||||||
bool isString() const;
|
bool isString() const;
|
||||||
bool isBool() const;
|
bool isBool() const;
|
||||||
bool isObject() const;
|
bool isObject() const;
|
||||||
|
|
Loading…
Add table
Reference in a new issue