diff options
author | Alexander Tkachev | 2016-05-30 02:01:48 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 701b07adfb578a6e23cedd7a310fbc1776e0a8a9 (patch) | |
tree | 207e2b2a15d5ce60ebc4b54b851b7833e629a6a4 /common | |
parent | c235aa29f997b9bd000be5424662b114957f1640 (diff) | |
download | scummvm-rg350-701b07adfb578a6e23cedd7a310fbc1776e0a8a9.tar.gz scummvm-rg350-701b07adfb578a6e23cedd7a310fbc1776e0a8a9.tar.bz2 scummvm-rg350-701b07adfb578a6e23cedd7a310fbc1776e0a8a9.zip |
COMMON: Fix JSON to understand integers correctly
Diffstat (limited to 'common')
-rw-r--r-- | common/json.cpp | 55 | ||||
-rw-r--r-- | common/json.h | 6 |
2 files changed, 59 insertions, 2 deletions
diff --git a/common/json.cpp b/common/json.cpp index 95c9123b4e..779f99aae0 100644 --- a/common/json.cpp +++ b/common/json.cpp @@ -300,13 +300,15 @@ JSONValue *JSONValue::parse(const char **data) { bool neg = **data == '-'; if (neg) (*data)++; + int integer = 0; double number = 0.0; + bool onlyInteger = true; // Parse the whole part of the number - only if it wasn't 0 if (**data == '0') (*data)++; else if (**data >= '1' && **data <= '9') - number = JSON::parseInt(data); + number = integer = JSON::parseInt(data); else return NULL; @@ -325,6 +327,7 @@ JSONValue *JSONValue::parse(const char **data) { // Save the number number += decimal; + onlyInteger = false; } // Could be an exponent now... @@ -346,11 +349,15 @@ JSONValue *JSONValue::parse(const char **data) { double expo = JSON::parseInt(data); for (double i = 0.0; i < expo; i++) number = neg_expo ? (number / 10.0) : (number * 10.0); + onlyInteger = false; } // Was it neg? if (neg) number *= -1; + if (onlyInteger) + return new JSONValue(neg ? -integer : integer); + return new JSONValue(number); } @@ -555,6 +562,18 @@ JSONValue::JSONValue(double numberValue) { } /** +* Basic constructor for creating a JSON Value of type Number (Integer) +* +* @access public +* +* @param int numberValue The number to use as the value +*/ +JSONValue::JSONValue(int numberValue) { + _type = JSONType_IntegerNumber; + _integerValue = numberValue; +} + +/** * Basic constructor for creating a JSON Value of type Array * * @access public @@ -601,6 +620,10 @@ JSONValue::JSONValue(const JSONValue &source) { _numberValue = source._numberValue; break; + case JSONType_IntegerNumber: + _integerValue = source._integerValue; + break; + case JSONType_Array: { JSONArray source_array = *source._arrayValue; JSONArray::iterator iter; @@ -695,6 +718,17 @@ bool JSONValue::isNumber() const { } /** +* Checks if the value is an Integer +* +* @access public +* +* @return bool Returns true if it is an Integer value, false otherwise +*/ +bool JSONValue::isIntegerNumber() const { + return _type == JSONType_IntegerNumber; +} + +/** * Checks if the value is an Array * * @access public @@ -753,6 +787,18 @@ double JSONValue::asNumber() const { } /** +* Retrieves the Integer value of this JSONValue +* Use isIntegerNumber() before using this method. +* +* @access public +* +* @return int Returns the number value +*/ +int JSONValue::asIntegerNumber() const { + return _integerValue; +} + +/** * Retrieves the Array value of this JSONValue * Use isArray() before using this method. * @@ -940,6 +986,13 @@ String JSONValue::stringifyImpl(size_t const indentDepth) const { break; } + case JSONType_IntegerNumber: { + char str[80]; + sprintf(str, "%d", _integerValue); + ret_string = str; + break; + } + case JSONType_Array: { ret_string = indentDepth ? "[\n" + indentStr1 : "["; JSONArray::const_iterator iter = _arrayValue->begin(); diff --git a/common/json.h b/common/json.h index c9debf05af..35571935ee 100644 --- a/common/json.h +++ b/common/json.h @@ -85,7 +85,7 @@ typedef HashMap<String, JSONValue*> JSONObject; class JSON; -enum JSONType { JSONType_Null, JSONType_String, JSONType_Bool, JSONType_Number, JSONType_Array, JSONType_Object }; +enum JSONType { JSONType_Null, JSONType_String, JSONType_Bool, JSONType_Number, JSONType_IntegerNumber, JSONType_Array, JSONType_Object }; class JSONValue { friend class JSON; @@ -96,6 +96,7 @@ public: JSONValue(const String &stringValue); JSONValue(bool boolValue); JSONValue(double numberValue); + JSONValue(int numberValue); JSONValue(const JSONArray &arrayValue); JSONValue(const JSONObject &objectValue); JSONValue(const JSONValue &source); @@ -105,12 +106,14 @@ public: bool isString() const; bool isBool() const; bool isNumber() const; + bool isIntegerNumber() const; bool isArray() const; bool isObject() const; const String &asString() const; bool asBool() const; double asNumber() const; + int asIntegerNumber() const; const JSONArray &asArray() const; const JSONObject &asObject() const; @@ -135,6 +138,7 @@ private: union { bool _boolValue; double _numberValue; + int _integerValue; String *_stringValue; JSONArray *_arrayValue; JSONObject *_objectValue; |