summaryrefslogtreecommitdiffstats
path: root/base/json
diff options
context:
space:
mode:
authorIain Merrick <husky@google.com>2010-10-19 14:37:37 +0100
committerIain Merrick <husky@google.com>2010-10-19 14:37:37 +0100
commit3345a6884c488ff3a535c2c9acdd33d74b37e311 (patch)
tree7784b988ef1698cb6967ea1bdf07616237716c6c /base/json
parentefc8475837ec58186051f23bb03542620424f6ce (diff)
downloadexternal_chromium-3345a6884c488ff3a535c2c9acdd33d74b37e311.zip
external_chromium-3345a6884c488ff3a535c2c9acdd33d74b37e311.tar.gz
external_chromium-3345a6884c488ff3a535c2c9acdd33d74b37e311.tar.bz2
Merge Chromium at 7.0.540.0 : Initial merge by git
Not including third_party/icu as it contains huge data files that break Gerrit, and aren't actually used. Change-Id: I428a386e70f3b58cacd28677b8cfda282e891e15
Diffstat (limited to 'base/json')
-rw-r--r--base/json/json_reader.cc35
-rw-r--r--base/json/json_reader.h1
-rw-r--r--base/json/json_reader_unittest.cc44
-rw-r--r--base/json/json_writer.cc9
-rw-r--r--base/json/json_writer.h7
-rw-r--r--base/json/json_writer_unittest.cc18
-rw-r--r--base/json/string_escape.h1
7 files changed, 56 insertions, 59 deletions
diff --git a/base/json/json_reader.cc b/base/json/json_reader.cc
index cf9ce02..391f58b 100644
--- a/base/json/json_reader.cc
+++ b/base/json/json_reader.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -7,6 +7,7 @@
#include "base/float_util.h"
#include "base/logging.h"
#include "base/scoped_ptr.h"
+#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
@@ -19,18 +20,6 @@ static const int kStackLimit = 100;
namespace {
-inline int HexToInt(wchar_t c) {
- if ('0' <= c && c <= '9') {
- return c - '0';
- } else if ('A' <= c && c <= 'F') {
- return c - 'A' + 10;
- } else if ('a' <= c && c <= 'f') {
- return c - 'a' + 10;
- }
- NOTREACHED();
- return 0;
-}
-
// A helper method for ParseNumberToken. It reads an int from the end of
// token. The method returns false if there is no valid integer at the end of
// the token.
@@ -306,7 +295,7 @@ Value* JSONReader::BuildValue(bool is_root) {
return NULL;
// Convert the key into a wstring.
- std::wstring dict_key;
+ std::string dict_key;
bool success = dict_key_value->GetAsString(&dict_key);
DCHECK(success);
@@ -401,11 +390,11 @@ Value* JSONReader::DecodeNumber(const Token& token) {
const std::wstring num_string(token.begin, token.length);
int num_int;
- if (StringToInt(WideToUTF16Hack(num_string), &num_int))
+ if (StringToInt(WideToUTF8(num_string), &num_int))
return Value::CreateIntegerValue(num_int);
double num_double;
- if (StringToDouble(WideToUTF16Hack(num_string), &num_double) &&
+ if (StringToDouble(WideToUTF8(num_string), &num_double) &&
base::IsFinite(num_double))
return Value::CreateRealValue(num_double);
@@ -492,15 +481,15 @@ Value* JSONReader::DecodeString(const Token& token) {
break;
case 'x':
- decoded_str.push_back((HexToInt(*(token.begin + i + 1)) << 4) +
- HexToInt(*(token.begin + i + 2)));
+ decoded_str.push_back((HexDigitToInt(*(token.begin + i + 1)) << 4) +
+ HexDigitToInt(*(token.begin + i + 2)));
i += 2;
break;
case 'u':
- decoded_str.push_back((HexToInt(*(token.begin + i + 1)) << 12 ) +
- (HexToInt(*(token.begin + i + 2)) << 8) +
- (HexToInt(*(token.begin + i + 3)) << 4) +
- HexToInt(*(token.begin + i + 4)));
+ decoded_str.push_back((HexDigitToInt(*(token.begin + i + 1)) << 12 ) +
+ (HexDigitToInt(*(token.begin + i + 2)) << 8) +
+ (HexDigitToInt(*(token.begin + i + 3)) << 4) +
+ HexDigitToInt(*(token.begin + i + 4)));
i += 4;
break;
@@ -515,7 +504,7 @@ Value* JSONReader::DecodeString(const Token& token) {
decoded_str.push_back(c);
}
}
- return Value::CreateStringValue(decoded_str);
+ return Value::CreateStringValue(WideToUTF16Hack(decoded_str));
}
JSONReader::Token JSONReader::ParseToken() {
diff --git a/base/json/json_reader.h b/base/json/json_reader.h
index aa0c2a7..33bd8f2 100644
--- a/base/json/json_reader.h
+++ b/base/json/json_reader.h
@@ -30,6 +30,7 @@
#ifndef BASE_JSON_JSON_READER_H_
#define BASE_JSON_JSON_READER_H_
+#pragma once
#include <string>
diff --git a/base/json/json_reader_unittest.cc b/base/json/json_reader_unittest.cc
index 71649df..c00c976 100644
--- a/base/json/json_reader_unittest.cc
+++ b/base/json/json_reader_unittest.cc
@@ -1,10 +1,12 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "testing/gtest/include/gtest/gtest.h"
#include "base/json/json_reader.h"
#include "base/scoped_ptr.h"
+#include "base/string_piece.h"
+#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
@@ -168,9 +170,9 @@ TEST(JSONReaderTest, Reading) {
root.reset(JSONReader().JsonToValue("\"hello world\"", false, false));
ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
- std::wstring str_val;
+ std::string str_val;
ASSERT_TRUE(root->GetAsString(&str_val));
- ASSERT_EQ(L"hello world", str_val);
+ ASSERT_EQ("hello world", str_val);
// Empty string
root.reset(JSONReader().JsonToValue("\"\"", false, false));
@@ -178,7 +180,7 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
- ASSERT_EQ(L"", str_val);
+ ASSERT_EQ("", str_val);
// Test basic string escapes
root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"",
@@ -187,7 +189,7 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
- ASSERT_EQ(L" \"\\/\b\f\n\r\t\v", str_val);
+ ASSERT_EQ(" \"\\/\b\f\n\r\t\v", str_val);
// Test hex and unicode escapes including the null character.
root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false,
@@ -196,7 +198,7 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
- ASSERT_EQ(std::wstring(L"A\0\x1234", 3), str_val);
+ ASSERT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val));
// Test invalid strings
root.reset(JSONReader().JsonToValue("\"no closing quote", false, false));
@@ -302,14 +304,14 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
real_val = 0.0;
- ASSERT_TRUE(dict_val->GetReal(L"number", &real_val));
+ ASSERT_TRUE(dict_val->GetReal("number", &real_val));
ASSERT_DOUBLE_EQ(9.87654321, real_val);
Value* null_val = NULL;
- ASSERT_TRUE(dict_val->Get(L"null", &null_val));
+ ASSERT_TRUE(dict_val->Get("null", &null_val));
ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL));
str_val.clear();
- ASSERT_TRUE(dict_val->GetString(L"S", &str_val));
- ASSERT_EQ(L"str", str_val);
+ ASSERT_TRUE(dict_val->GetString("S", &str_val));
+ ASSERT_EQ("str", str_val);
root2.reset(JSONReader::Read(
"{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true));
@@ -342,15 +344,15 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
dict_val = static_cast<DictionaryValue*>(root.get());
DictionaryValue* inner_dict = NULL;
- ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict));
+ ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict));
ListValue* inner_array = NULL;
- ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array));
+ ASSERT_TRUE(inner_dict->GetList("array", &inner_array));
ASSERT_EQ(1U, inner_array->GetSize());
bool_value = true;
- ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value));
+ ASSERT_TRUE(dict_val->GetBoolean("false", &bool_value));
ASSERT_FALSE(bool_value);
inner_dict = NULL;
- ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict));
+ ASSERT_TRUE(dict_val->GetDictionary("d", &inner_dict));
root2.reset(JSONReader::Read(
"{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true));
@@ -363,15 +365,15 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
dict_val = static_cast<DictionaryValue*>(root.get());
int integer_value = 0;
- EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
+ EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
EXPECT_EQ(3, integer_value);
- EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"c", &integer_value));
+ EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value));
EXPECT_EQ(2, integer_value);
inner_dict = NULL;
- ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion(L"d.e.f",
+ ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f",
&inner_dict));
ASSERT_EQ(1U, inner_dict->size());
- EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion(L"g.h.i.j",
+ EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j",
&integer_value));
EXPECT_EQ(1, integer_value);
@@ -379,9 +381,9 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
dict_val = static_cast<DictionaryValue*>(root.get());
- EXPECT_TRUE(dict_val->GetInteger(L"a.b", &integer_value));
+ EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value));
EXPECT_EQ(2, integer_value);
- EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
+ EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
EXPECT_EQ(1, integer_value);
// Invalid, no closing brace
@@ -444,7 +446,7 @@ TEST(JSONReaderTest, Reading) {
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
- ASSERT_EQ(L"\x7f51\x9875", str_val);
+ ASSERT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val));
// Test invalid utf8 encoded input
root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"",
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc
index ffdad76..dbf43ec 100644
--- a/base/json/json_writer.cc
+++ b/base/json/json_writer.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -7,6 +7,7 @@
#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/string_util.h"
+#include "base/string_number_conversions.h"
#include "base/values.h"
#include "base/utf_string_conversions.h"
@@ -187,8 +188,10 @@ void JSONWriter::BuildJSONString(const Value* const node,
}
}
-void JSONWriter::AppendQuotedString(const std::wstring& str) {
- JsonDoubleQuote(WideToUTF16Hack(str), true, json_string_);
+void JSONWriter::AppendQuotedString(const std::string& str) {
+ // TODO(viettrungluu): |str| is UTF-8, not ASCII, so to properly escape it we
+ // have to convert it to UTF-16. This round-trip is suboptimal.
+ JsonDoubleQuote(UTF8ToUTF16(str), true, json_string_);
}
void JSONWriter::IndentLine(int depth) {
diff --git a/base/json/json_writer.h b/base/json/json_writer.h
index 0ebee0a..eb17145 100644
--- a/base/json/json_writer.h
+++ b/base/json/json_writer.h
@@ -1,9 +1,10 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_JSON_JSON_WRITER_H_
#define BASE_JSON_JSON_WRITER_H_
+#pragma once
#include <string>
@@ -44,8 +45,8 @@ class JSONWriter {
// json_string_ will contain the JSON.
void BuildJSONString(const Value* const node, int depth, bool escape);
- // Appends a quoted, escaped, version of str to json_string_.
- void AppendQuotedString(const std::wstring& str);
+ // Appends a quoted, escaped, version of (UTF-8) str to json_string_.
+ void AppendQuotedString(const std::string& str);
// Adds space to json_string_ for the indent level.
void IndentLine(int depth);
diff --git a/base/json/json_writer_unittest.cc b/base/json/json_writer_unittest.cc
index e7d0f05..937d083 100644
--- a/base/json/json_writer_unittest.cc
+++ b/base/json/json_writer_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -50,10 +50,10 @@ TEST(JSONWriterTest, Writing) {
// list list nesting, etc.
DictionaryValue root_dict;
ListValue* list = new ListValue;
- root_dict.Set(L"list", list);
+ root_dict.Set("list", list);
DictionaryValue* inner_dict = new DictionaryValue;
list->Append(inner_dict);
- inner_dict->SetInteger(L"inner int", 10);
+ inner_dict->SetInteger("inner int", 10);
ListValue* inner_list = new ListValue;
list->Append(inner_list);
list->Append(Value::CreateBooleanValue(true));
@@ -79,18 +79,18 @@ TEST(JSONWriterTest, Writing) {
// Test keys with periods
DictionaryValue period_dict;
- period_dict.SetWithoutPathExpansion(L"a.b", Value::CreateIntegerValue(3));
- period_dict.SetWithoutPathExpansion(L"c", Value::CreateIntegerValue(2));
+ period_dict.SetWithoutPathExpansion("a.b", Value::CreateIntegerValue(3));
+ period_dict.SetWithoutPathExpansion("c", Value::CreateIntegerValue(2));
DictionaryValue* period_dict2 = new DictionaryValue;
- period_dict2->SetWithoutPathExpansion(L"g.h.i.j",
+ period_dict2->SetWithoutPathExpansion("g.h.i.j",
Value::CreateIntegerValue(1));
- period_dict.SetWithoutPathExpansion(L"d.e.f", period_dict2);
+ period_dict.SetWithoutPathExpansion("d.e.f", period_dict2);
JSONWriter::Write(&period_dict, false, &output_js);
ASSERT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js);
DictionaryValue period_dict3;
- period_dict3.Set(L"a.b", Value::CreateIntegerValue(2));
- period_dict3.SetWithoutPathExpansion(L"a.b", Value::CreateIntegerValue(1));
+ period_dict3.Set("a.b", Value::CreateIntegerValue(2));
+ period_dict3.SetWithoutPathExpansion("a.b", Value::CreateIntegerValue(1));
JSONWriter::Write(&period_dict3, false, &output_js);
ASSERT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js);
}
diff --git a/base/json/string_escape.h b/base/json/string_escape.h
index 7c64c29..2d7206b 100644
--- a/base/json/string_escape.h
+++ b/base/json/string_escape.h
@@ -6,6 +6,7 @@
#ifndef BASE_JSON_STRING_ESCAPE_H_
#define BASE_JSON_STRING_ESCAPE_H_
+#pragma once
#include <string>