Skip to content

Commit

Permalink
Move the json-related files into a separate json directory. This hope…
Browse files Browse the repository at this point in the history
…fully also

makes the naming of string_escape more clear (it's actually JSON-specific).
Move the files into the base namespace.

TEST=none
BUG=none
Review URL: http://codereview.chromium.org/316016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29934 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Oct 23, 2009
1 parent a9f607e commit 93d49d7
Show file tree
Hide file tree
Showing 41 changed files with 195 additions and 169 deletions.
18 changes: 9 additions & 9 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@
'id_map.h',
'image_util.cc',
'image_util.h',
'json_reader.cc',
'json_reader.h',
'json_writer.cc',
'json_writer.h',
'json/json_reader.cc',
'json/json_reader.h',
'json/json_writer.cc',
'json/json_writer.h',
'json/string_escape.cc',
'json/string_escape.h',
'keyboard_code_conversion_gtk.cc',
'keyboard_code_conversion_gtk.h',
'keyboard_codes.h',
Expand Down Expand Up @@ -279,8 +281,6 @@
'stl_util-inl.h',
'string16.cc',
'string16.h',
'string_escape.cc',
'string_escape.h',
'string_piece.cc',
'string_piece.h',
'string_tokenizer.h',
Expand Down Expand Up @@ -578,8 +578,9 @@
'i18n/file_util_icu_unittest.cc',
'i18n/icu_string_conversions_unittest.cc',
'i18n/word_iterator_unittest.cc',
'json_reader_unittest.cc',
'json_writer_unittest.cc',
'json/json_reader_unittest.cc',
'json/json_writer_unittest.cc',
'json/string_escape_unittest.cc',
'lazy_instance_unittest.cc',
'leak_tracker_unittest.cc',
'linked_list_unittest.cc',
Expand Down Expand Up @@ -607,7 +608,6 @@
'singleton_unittest.cc',
'stack_container_unittest.cc',
'stats_table_unittest.cc',
'string_escape_unittest.cc',
'string_piece_unittest.cc',
'string_tokenizer_unittest.cc',
'string_util_unittest.cc',
Expand Down
6 changes: 5 additions & 1 deletion base/json_reader.cc → base/json/json_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/json_reader.h"
#include "base/json/json_reader.h"

#include "base/float_util.h"
#include "base/logging.h"
Expand All @@ -11,6 +11,8 @@
#include "base/utf_string_conversions.h"
#include "base/values.h"

namespace base {

static const JSONReader::Token kInvalidToken(JSONReader::Token::INVALID_TOKEN,
0, 0);
static const int kStackLimit = 100;
Expand Down Expand Up @@ -636,3 +638,5 @@ void JSONReader::SetErrorMessage(const char* description,

error_message_ = FormatErrorMessage(line_number, column_number, description);
}

} // namespace base
12 changes: 8 additions & 4 deletions base/json_reader.h → base/json/json_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
// only a convenience for the common uses with more complex configuration going
// on the instance.

#ifndef BASE_JSON_READER_H_
#define BASE_JSON_READER_H_
#ifndef BASE_JSON_JSON_READER_H_
#define BASE_JSON_JSON_READER_H_

#include <string>

Expand All @@ -38,6 +38,8 @@

class Value;

namespace base {

class JSONReader {
public:
// A struct to hold a JS token.
Expand Down Expand Up @@ -119,7 +121,7 @@ class JSONReader {
static std::string FormatErrorMessage(int line, int column,
const char* description);

DISALLOW_EVIL_CONSTRUCTORS(JSONReader);
DISALLOW_COPY_AND_ASSIGN(JSONReader);

FRIEND_TEST(JSONReaderTest, Reading);
FRIEND_TEST(JSONReaderTest, ErrorMessages);
Expand Down Expand Up @@ -183,4 +185,6 @@ class JSONReader {
std::string error_message_;
};

#endif // BASE_JSON_READER_H_
} // namespace base

#endif // BASE_JSON_JSON_READER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "base/json_reader.h"
#include "base/json/json_reader.h"
#include "base/scoped_ptr.h"
#include "base/values.h"
#include "build/build_config.h"

namespace base {

TEST(JSONReaderTest, Reading) {
// some whitespace checking
scoped_ptr<Value> root;
Expand Down Expand Up @@ -514,3 +516,5 @@ TEST(JSONReaderTest, ErrorMessages) {
error_message);

}

} // namespace base
18 changes: 9 additions & 9 deletions base/json_writer.cc → base/json/json_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/json_writer.h"
#include "base/json/json_writer.h"

#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/values.h"
#include "base/string_escape.h"
#include "base/utf_string_conversions.h"

namespace base {

#if defined(OS_WIN)
static const char kPrettyPrintLineEnding[] = "\r\n";
#else
Expand Down Expand Up @@ -101,11 +103,9 @@ void JSONWriter::BuildJSONString(const Value* const node,
bool result = node->GetAsString(&value);
DCHECK(result);
if (escape) {
string_escape::JsonDoubleQuote(UTF8ToUTF16(value),
true,
json_string_);
JsonDoubleQuote(UTF8ToUTF16(value), true, json_string_);
} else {
string_escape::JsonDoubleQuote(value, true, json_string_);
JsonDoubleQuote(value, true, json_string_);
}
break;
}
Expand Down Expand Up @@ -186,13 +186,13 @@ void JSONWriter::BuildJSONString(const Value* const node,
}

void JSONWriter::AppendQuotedString(const std::wstring& str) {
string_escape::JsonDoubleQuote(WideToUTF16Hack(str),
true,
json_string_);
JsonDoubleQuote(WideToUTF16Hack(str), true, json_string_);
}

void JSONWriter::IndentLine(int depth) {
// It may be faster to keep an indent string so we don't have to keep
// reallocating.
json_string_->append(std::string(depth * 3, ' '));
}

} // namespace base
10 changes: 7 additions & 3 deletions base/json_writer.h → base/json/json_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_JSON_WRITER_H_
#define BASE_JSON_WRITER_H_
#ifndef BASE_JSON_JSON_WRITER_H_
#define BASE_JSON_JSON_WRITER_H_

#include <string>

#include "base/basictypes.h"

class Value;

namespace base {

class JSONWriter {
public:
// Given a root node, generates a JSON string and puts it into |json|.
Expand Down Expand Up @@ -52,4 +54,6 @@ class JSONWriter {
DISALLOW_COPY_AND_ASSIGN(JSONWriter);
};

#endif // BASE_JSON_WRITER_H_
} // namespace base

#endif // BASE_JSON_JSON_WRITER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// 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_writer.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

TEST(JSONWriterTest, Writing) {
// Test null
Expand Down Expand Up @@ -75,3 +77,5 @@ TEST(JSONWriterTest, Writing) {
output_js);
#undef JSON_NEWLINE
}

} // namespace base
10 changes: 7 additions & 3 deletions base/string_escape.cc → base/json/string_escape.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/string_escape.h"
#include "base/json/string_escape.h"

#include <string>

#include "base/string_util.h"

namespace string_escape {
namespace base {

namespace {

// Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful,
// returns true and appends the escape sequence to |dst|. This isn't required
Expand Down Expand Up @@ -72,6 +74,8 @@ void JsonDoubleQuoteT(const STR& str,
dst->push_back('"');
}

} // namespace

void JsonDoubleQuote(const std::string& str,
bool put_in_quotes,
std::string* dst) {
Expand All @@ -84,4 +88,4 @@ void JsonDoubleQuote(const string16& str,
JsonDoubleQuoteT(str, put_in_quotes, dst);
}

} // namespace string_escape
} // namespace base
10 changes: 5 additions & 5 deletions base/string_escape.h → base/json/string_escape.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
//
// This file defines utility functions for escaping strings.

#ifndef BASE_STRING_ESCAPE_H__
#define BASE_STRING_ESCAPE_H__
#ifndef BASE_JSON_STRING_ESCAPE_H_
#define BASE_JSON_STRING_ESCAPE_H_

#include <string>

#include "base/string16.h"

namespace string_escape {
namespace base {

// Escape |str| appropriately for a JSON string litereal, _appending_ the
// result to |dst|. This will create unicode escape sequences (\uXXXX).
Expand All @@ -27,6 +27,6 @@ void JsonDoubleQuote(const string16& str,
std::string* dst);


} // namespace string_escape
} // namespace base

#endif // BASE_STRING_ESCAPE_H__
#endif // BASE_JSON_STRING_ESCAPE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// 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/string_escape.h"
#include "base/json/string_escape.h"
#include "base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

Expand All @@ -24,17 +26,17 @@ TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) {
std::string in = json_narrow_cases[i].to_escape;
std::string out;
string_escape::JsonDoubleQuote(in, false, &out);
JsonDoubleQuote(in, false, &out);
EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
}

std::string in = json_narrow_cases[0].to_escape;
std::string out;
string_escape::JsonDoubleQuote(in, false, &out);
JsonDoubleQuote(in, false, &out);

// test quoting
std::string out_quoted;
string_escape::JsonDoubleQuote(in, true, &out_quoted);
JsonDoubleQuote(in, true, &out_quoted);
EXPECT_EQ(out.length() + 2, out_quoted.length());
EXPECT_EQ(out_quoted.find(out), 1U);

Expand All @@ -45,7 +47,7 @@ TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
std::string expected = "test\\u0000";
expected += json_narrow_cases[0].escaped;
out.clear();
string_escape::JsonDoubleQuote(in, false, &out);
JsonDoubleQuote(in, false, &out);
EXPECT_EQ(expected, out);
}

Expand All @@ -69,17 +71,17 @@ TEST(StringEscapeTest, JsonDoubleQuoteWide) {
for (size_t i = 0; i < arraysize(json_wide_cases); ++i) {
std::string out;
string16 in = WideToUTF16(json_wide_cases[i].to_escape);
string_escape::JsonDoubleQuote(in, false, &out);
JsonDoubleQuote(in, false, &out);
EXPECT_EQ(std::string(json_wide_cases[i].escaped), out);
}

string16 in = WideToUTF16(json_wide_cases[0].to_escape);
std::string out;
string_escape::JsonDoubleQuote(in, false, &out);
JsonDoubleQuote(in, false, &out);

// test quoting
std::string out_quoted;
string_escape::JsonDoubleQuote(in, true, &out_quoted);
JsonDoubleQuote(in, true, &out_quoted);
EXPECT_EQ(out.length() + 2, out_quoted.length());
EXPECT_EQ(out_quoted.find(out), 1U);

Expand All @@ -90,6 +92,8 @@ TEST(StringEscapeTest, JsonDoubleQuoteWide) {
std::string expected = "test\\u0000";
expected += json_wide_cases[0].escaped;
out.clear();
string_escape::JsonDoubleQuote(in, false, &out);
JsonDoubleQuote(in, false, &out);
EXPECT_EQ(expected, out);
}

} // namespace base
10 changes: 5 additions & 5 deletions chrome/browser/automation/automation_extension_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "chrome/browser/automation/automation_extension_function.h"

#include "base/json_reader.h"
#include "base/json_writer.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "chrome/browser/automation/extension_automation_constants.h"
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "chrome/browser/renderer_host/render_view_host.h"
Expand All @@ -16,7 +16,7 @@
bool AutomationExtensionFunction::enabled_ = false;

void AutomationExtensionFunction::SetArgs(const Value* args) {
JSONWriter::Write(args, false, &args_);
base::JSONWriter::Write(args, false, &args_);
}

const std::string AutomationExtensionFunction::GetResult() {
Expand All @@ -42,7 +42,7 @@ void AutomationExtensionFunction::Run() {
message_to_host.SetBoolean(keys::kAutomationHasCallbackKey, has_callback_);

std::string message;
JSONWriter::Write(&message_to_host, false, &message);
base::JSONWriter::Write(&message_to_host, false, &message);
dispatcher()->render_view_host_->delegate()->ProcessExternalHostMessage(
message, keys::kAutomationOrigin, keys::kAutomationRequestTarget);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ bool AutomationExtensionFunction::InterceptMessageFromExternalHost(
target == keys::kAutomationResponseTarget) {
// This is an extension API response being sent back via postMessage,
// so redirect it.
scoped_ptr<Value> message_value(JSONReader::Read(message, false));
scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
DCHECK(message_value->IsType(Value::TYPE_DICTIONARY));
if (message_value->IsType(Value::TYPE_DICTIONARY)) {
DictionaryValue* message_dict =
Expand Down
Loading

0 comments on commit 93d49d7

Please sign in to comment.