diff options
author | gspencer@google.com <gspencer@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-27 23:15:42 +0000 |
---|---|---|
committer | gspencer@google.com <gspencer@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-27 23:15:42 +0000 |
commit | 05b47f7a8c5451f858dc220df0e3a97542edace6 (patch) | |
tree | a2273d619f0625c9d44d40842845ccce2eac1045 /o3d/utils/cross/json_writer.cc | |
parent | 5cdc8bdb4c847cefe7f4542bd10c9880c2c557a0 (diff) | |
download | chromium_src-05b47f7a8c5451f858dc220df0e3a97542edace6.zip chromium_src-05b47f7a8c5451f858dc220df0e3a97542edace6.tar.gz chromium_src-05b47f7a8c5451f858dc220df0e3a97542edace6.tar.bz2 |
This is the O3D source tree's initial commit to the Chromium tree. It
is not built or referenced at all by the chrome build yet, and doesn't
yet build in it's new home. We'll change that shortly.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17035 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/utils/cross/json_writer.cc')
-rw-r--r-- | o3d/utils/cross/json_writer.cc | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/o3d/utils/cross/json_writer.cc b/o3d/utils/cross/json_writer.cc new file mode 100644 index 0000000..784d728 --- /dev/null +++ b/o3d/utils/cross/json_writer.cc @@ -0,0 +1,246 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +// This file contains the definition of class JsonWriter. + +#include "utils/cross/json_writer.h" +#include "base/logging.h" +#include "base/string_util.h" + +using std::string; + +namespace o3d { +JsonWriter::JsonWriter(TextWriter* writer, int indent_spaces) + : writer_(writer), + indent_spaces_(indent_spaces), + compacting_level_(0), + current_indentation_(0), + new_line_pending_(false), + comma_pending_(false) { + DCHECK(writer_); +} + +JsonWriter::~JsonWriter() { + Close(); +} + +void JsonWriter::OpenObject() { + DCHECK(writer_); + WritePending(); + writer_->WriteChar('{'); + IncreaseIndentation(); + ScheduleNewLine(); +} + +void JsonWriter::CloseObject() { + DCHECK(writer_); + CancelComma(); + DecreaseIndentation(); + WritePending(); + writer_->WriteChar('}'); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::OpenArray() { + DCHECK(writer_); + WritePending(); + writer_->WriteChar('['); + IncreaseIndentation(); + ScheduleNewLine(); +} + +void JsonWriter::CloseArray() { + DCHECK(writer_); + CancelComma(); + DecreaseIndentation(); + WritePending(); + writer_->WriteChar(']'); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::BeginCompacting() { + WritePending(); + ++compacting_level_; +} + +void JsonWriter::EndCompacting() { + DCHECK_GT(compacting_level_, 0); + --compacting_level_; +} + +void JsonWriter::WritePropertyName(const string& name) { + DCHECK(writer_); + WritePending(); + writer_->WriteChar('\"'); + WriteEscapedString(name); + writer_->WriteChar('\"'); + if (compacting_level_ > 0) { + writer_->WriteChar(':'); + } else { + writer_->WriteString(string(": ")); + } +} + +void JsonWriter::WriteBool(bool value) { + DCHECK(writer_); + WritePending(); + writer_->WriteBool(value); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::WriteInt(int value) { + DCHECK(writer_); + WritePending(); + writer_->WriteInt(value); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::WriteUnsignedInt(unsigned int value) { + DCHECK(writer_); + WritePending(); + writer_->WriteUnsignedInt(value); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::WriteFloat(float value) { + DCHECK(writer_); + WritePending(); + writer_->WriteFloat(value); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::WriteString(const string& value) { + DCHECK(writer_); + WritePending(); + writer_->WriteChar('\"'); + WriteEscapedString(value); + writer_->WriteChar('\"'); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::WriteNull() { + DCHECK(writer_); + WritePending(); + writer_->WriteString(string("null")); + ScheduleComma(); + ScheduleNewLine(); +} + +void JsonWriter::Close() { + if (writer_) { + CancelComma(); + WritePending(); + writer_->Close(); + writer_ = NULL; + } +} + +void JsonWriter::IncreaseIndentation() { + ++current_indentation_; +} + +void JsonWriter::DecreaseIndentation() { + DCHECK_GT(current_indentation_, 0); + --current_indentation_; +} + +void JsonWriter::ScheduleNewLine() { + new_line_pending_ = true; +} + +void JsonWriter::ScheduleComma() { + comma_pending_ = true; +} + +void JsonWriter::CancelComma() { + comma_pending_ = false; +} + +void JsonWriter::WritePending() { + if (comma_pending_) { + writer_->WriteChar(','); + comma_pending_ = false; + } + + if (new_line_pending_) { + if (compacting_level_ == 0) { + writer_->WriteNewLine(); + for (int i = 0; i < current_indentation_ * indent_spaces_; ++i) { + writer_->WriteChar(' '); + } + } + new_line_pending_ = false; + } +} + +void JsonWriter::WriteEscapedString(const string& unescaped) { + for (int i = 0; i < unescaped.length(); ++i) { + char c = unescaped[i]; + switch (c) { + case '\"': + writer_->WriteString("\\\""); + break; + case '\\': + writer_->WriteString("\\\\"); + break; + case '\b': + writer_->WriteString("\\b"); + break; + case '\f': + writer_->WriteString("\\f"); + break; + case '\n': + writer_->WriteString("\\n"); + break; + case '\r': + writer_->WriteString("\\r"); + break; + case '\t': + writer_->WriteString("\\t"); + break; + default: + if (c < ' ') { + writer_->WriteString(StringPrintf("\\u%04x", static_cast<int>(c))); + } else { + writer_->WriteChar(c); + } + } + } +} +} // namespace o3d |