diff options
author | Kristian Monsen <kristianm@google.com> | 2011-05-31 20:30:28 +0100 |
---|---|---|
committer | Kristian Monsen <kristianm@google.com> | 2011-06-14 20:31:41 -0700 |
commit | 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801 (patch) | |
tree | 382278a54ce7a744d62fa510a9a80688cc12434b /base/json | |
parent | c4becdd46e31d261b930e4b5a539cbc1d45c23a6 (diff) | |
download | external_chromium-72a454cd3513ac24fbdd0e0cb9ad70b86a99b801.zip external_chromium-72a454cd3513ac24fbdd0e0cb9ad70b86a99b801.tar.gz external_chromium-72a454cd3513ac24fbdd0e0cb9ad70b86a99b801.tar.bz2 |
Merge Chromium.org at r11.0.672.0: Initial merge by git.
Change-Id: I8b4aaf611a2a405fe3fe10e8a94ea7658645c192
Diffstat (limited to 'base/json')
-rw-r--r-- | base/json/json_reader.cc | 52 | ||||
-rw-r--r-- | base/json/json_reader.h | 10 | ||||
-rw-r--r-- | base/json/json_reader_unittest.cc | 72 | ||||
-rw-r--r-- | base/json/json_writer.cc | 4 | ||||
-rw-r--r-- | base/json/json_writer_unittest.cc | 6 |
5 files changed, 72 insertions, 72 deletions
diff --git a/base/json/json_reader.cc b/base/json/json_reader.cc index 391f58b..c8fe78d 100644 --- a/base/json/json_reader.cc +++ b/base/json/json_reader.cc @@ -81,6 +81,11 @@ const char* JSONReader::kUnsupportedEncoding = const char* JSONReader::kUnquotedDictionaryKey = "Dictionary keys must be quoted."; +JSONReader::JSONReader() + : start_pos_(NULL), json_pos_(NULL), stack_depth_(0), + allow_trailing_comma_(false), + error_code_(JSON_NO_ERROR), error_line_(0), error_col_(0) {} + /* static */ Value* JSONReader::Read(const std::string& json, bool allow_trailing_comma) { @@ -106,16 +111,6 @@ Value* JSONReader::ReadAndReturnError(const std::string& json, } /* static */ -std::string JSONReader::FormatErrorMessage(int line, int column, - const std::string& description) { - if (line || column) { - return StringPrintf("Line: %i, column: %i, %s", - line, column, description.c_str()); - } - return description; -} - -/* static */ std::string JSONReader::ErrorCodeToString(JsonParseError error_code) { switch (error_code) { case JSON_NO_ERROR: @@ -147,11 +142,6 @@ std::string JSONReader::GetErrorMessage() const { ErrorCodeToString(error_code_)); } -JSONReader::JSONReader() - : start_pos_(NULL), json_pos_(NULL), stack_depth_(0), - allow_trailing_comma_(false), - error_code_(JSON_NO_ERROR), error_line_(0), error_col_(0) {} - Value* JSONReader::JsonToValue(const std::string& json, bool check_root, bool allow_trailing_comma) { // The input must be in UTF-8. @@ -195,6 +185,16 @@ Value* JSONReader::JsonToValue(const std::string& json, bool check_root, return NULL; } +/* static */ +std::string JSONReader::FormatErrorMessage(int line, int column, + const std::string& description) { + if (line || column) { + return StringPrintf("Line: %i, column: %i, %s", + line, column, description.c_str()); + } + return description; +} + Value* JSONReader::BuildValue(bool is_root) { ++stack_depth_; if (stack_depth_ > kStackLimit) { @@ -396,7 +396,7 @@ Value* JSONReader::DecodeNumber(const Token& token) { double num_double; if (StringToDouble(WideToUTF8(num_string), &num_double) && base::IsFinite(num_double)) - return Value::CreateRealValue(num_double); + return Value::CreateDoubleValue(num_double); return NULL; } @@ -580,16 +580,6 @@ JSONReader::Token JSONReader::ParseToken() { return token; } -bool JSONReader::NextStringMatch(const std::wstring& str) { - for (size_t i = 0; i < str.length(); ++i) { - if ('\0' == *json_pos_) - return false; - if (*(json_pos_ + i) != str[i]) - return false; - } - return true; -} - void JSONReader::EatWhitespaceAndComments() { while ('\0' != *json_pos_) { switch (*json_pos_) { @@ -645,6 +635,16 @@ bool JSONReader::EatComment() { return true; } +bool JSONReader::NextStringMatch(const std::wstring& str) { + for (size_t i = 0; i < str.length(); ++i) { + if ('\0' == *json_pos_) + return false; + if (*(json_pos_ + i) != str[i]) + return false; + } + return true; +} + void JSONReader::SetErrorCode(JsonParseError error, const wchar_t* error_pos) { int line_number = 1; diff --git a/base/json/json_reader.h b/base/json/json_reader.h index 77c4e74..a6f0686 100644 --- a/base/json/json_reader.h +++ b/base/json/json_reader.h @@ -146,14 +146,12 @@ class JSONReader { bool allow_trailing_comma); private: - static std::string FormatErrorMessage(int line, int column, - const std::string& description); - - DISALLOW_COPY_AND_ASSIGN(JSONReader); - FRIEND_TEST(JSONReaderTest, Reading); FRIEND_TEST(JSONReaderTest, ErrorMessages); + static std::string FormatErrorMessage(int line, int column, + const std::string& description); + // Recursively build Value. Returns NULL if we don't have a valid JSON // string. If |is_root| is true, we verify that the root element is either // an object or an array. @@ -213,6 +211,8 @@ class JSONReader { JsonParseError error_code_; int error_line_; int error_col_; + + DISALLOW_COPY_AND_ASSIGN(JSONReader); }; } // namespace base diff --git a/base/json/json_reader_unittest.cc b/base/json/json_reader_unittest.cc index c00c976..db0ab63 100644 --- a/base/json/json_reader_unittest.cc +++ b/base/json/json_reader_unittest.cc @@ -75,60 +75,60 @@ TEST(JSONReaderTest, Reading) { // storage as doubles root.reset(JSONReader().JsonToValue("2147483648", false, false)); ASSERT_TRUE(root.get()); - double real_val; - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(2147483648.0, real_val); + double double_val; + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(2147483648.0, double_val); root.reset(JSONReader().JsonToValue("-2147483649", false, false)); ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(-2147483649.0, real_val); + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(-2147483649.0, double_val); // Parse a double root.reset(JSONReader().JsonToValue("43.1", false, false)); ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(43.1, real_val); + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(43.1, double_val); root.reset(JSONReader().JsonToValue("4.3e-1", false, false)); ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(.43, real_val); + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(.43, double_val); root.reset(JSONReader().JsonToValue("2.1e0", false, false)); ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(2.1, real_val); + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(2.1, double_val); root.reset(JSONReader().JsonToValue("2.1e+0001", false, false)); ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(21.0, real_val); + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(21.0, double_val); root.reset(JSONReader().JsonToValue("0.01", false, false)); ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(0.01, real_val); + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(0.01, double_val); root.reset(JSONReader().JsonToValue("1.00", false, false)); ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); - real_val = 0.0; - ASSERT_TRUE(root->GetAsReal(&real_val)); - ASSERT_DOUBLE_EQ(1.0, real_val); + ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); + double_val = 0.0; + ASSERT_TRUE(root->GetAsDouble(&double_val)); + ASSERT_DOUBLE_EQ(1.0, double_val); // Fractional parts must have a digit before and after the decimal point. root.reset(JSONReader().JsonToValue("1.", false, false)); @@ -303,9 +303,9 @@ TEST(JSONReaderTest, Reading) { ASSERT_TRUE(root.get()); ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); - real_val = 0.0; - ASSERT_TRUE(dict_val->GetReal("number", &real_val)); - ASSERT_DOUBLE_EQ(9.87654321, real_val); + double_val = 0.0; + ASSERT_TRUE(dict_val->GetDouble("number", &double_val)); + ASSERT_DOUBLE_EQ(9.87654321, double_val); Value* null_val = NULL; ASSERT_TRUE(dict_val->Get("null", &null_val)); ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc index dbf43ec..fa8f239 100644 --- a/base/json/json_writer.cc +++ b/base/json/json_writer.cc @@ -75,10 +75,10 @@ void JSONWriter::BuildJSONString(const Value* const node, break; } - case Value::TYPE_REAL: + case Value::TYPE_DOUBLE: { double value; - bool result = node->GetAsReal(&value); + bool result = node->GetAsDouble(&value); DCHECK(result); std::string real = DoubleToString(value); // Ensure that the number has a .0 if there's no decimal or 'e'. This diff --git a/base/json/json_writer_unittest.cc b/base/json/json_writer_unittest.cc index 937d083..6d7714b 100644 --- a/base/json/json_writer_unittest.cc +++ b/base/json/json_writer_unittest.cc @@ -29,19 +29,19 @@ TEST(JSONWriterTest, Writing) { delete root; // Test Real values should always have a decimal or an 'e'. - root = Value::CreateRealValue(1.0); + root = Value::CreateDoubleValue(1.0); JSONWriter::Write(root, false, &output_js); ASSERT_EQ("1.0", output_js); delete root; // Test Real values in the the range (-1, 1) must have leading zeros - root = Value::CreateRealValue(0.2); + root = Value::CreateDoubleValue(0.2); JSONWriter::Write(root, false, &output_js); ASSERT_EQ("0.2", output_js); delete root; // Test Real values in the the range (-1, 1) must have leading zeros - root = Value::CreateRealValue(-0.8); + root = Value::CreateDoubleValue(-0.8); JSONWriter::Write(root, false, &output_js); ASSERT_EQ("-0.8", output_js); delete root; |