diff options
author | tsepez@chromium.org <tsepez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-06 09:24:48 +0000 |
---|---|---|
committer | tsepez@chromium.org <tsepez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-06 09:24:48 +0000 |
commit | e959b8dc83df8c79b0560d79dd17dc29131ab1e2 (patch) | |
tree | 44e2300fcfc19d493afe63280a4016ad4ab20fcf /base/json | |
parent | fe3beef0a468d2a7cdb0ca7576efd2eac23ff7b4 (diff) | |
download | chromium_src-e959b8dc83df8c79b0560d79dd17dc29131ab1e2.zip chromium_src-e959b8dc83df8c79b0560d79dd17dc29131ab1e2.tar.gz chromium_src-e959b8dc83df8c79b0560d79dd17dc29131ab1e2.tar.bz2 |
Fix braces and indentation in json_writer.cc prior as a precursor for subsequent modifications.
R=gab@chromium.org
TBR=thakis@chromium.org
Review URL: https://codereview.chromium.org/150003005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249308 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/json')
-rw-r--r-- | base/json/json_writer.cc | 233 |
1 files changed, 114 insertions, 119 deletions
diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc index e32a716..b606c10 100644 --- a/base/json/json_writer.cc +++ b/base/json/json_writer.cc @@ -50,152 +50,147 @@ JSONWriter::JSONWriter(int options, std::string* json) void JSONWriter::BuildJSONString(const Value* const node, size_t depth) { switch (node->GetType()) { - case Value::TYPE_NULL: + case Value::TYPE_NULL: { json_string_->append("null"); break; + } - case Value::TYPE_BOOLEAN: - { - bool value; - bool result = node->GetAsBoolean(&value); - DCHECK(result); - json_string_->append(value ? "true" : "false"); - break; - } + case Value::TYPE_BOOLEAN: { + bool value; + bool result = node->GetAsBoolean(&value); + DCHECK(result); + json_string_->append(value ? "true" : "false"); + break; + } - case Value::TYPE_INTEGER: - { - int value; - bool result = node->GetAsInteger(&value); - DCHECK(result); - json_string_->append(IntToString(value)); + case Value::TYPE_INTEGER: { + int value; + bool result = node->GetAsInteger(&value); + DCHECK(result); + json_string_->append(IntToString(value)); + break; + } + + case Value::TYPE_DOUBLE: { + double value; + bool result = node->GetAsDouble(&value); + DCHECK(result); + if (omit_double_type_preservation_ && + value <= kint64max && + value >= kint64min && + std::floor(value) == value) { + json_string_->append(Int64ToString(static_cast<int64>(value))); break; } - - case Value::TYPE_DOUBLE: - { - double value; - bool result = node->GetAsDouble(&value); - DCHECK(result); - if (omit_double_type_preservation_ && - value <= kint64max && - value >= kint64min && - std::floor(value) == value) { - json_string_->append(Int64ToString(static_cast<int64>(value))); - break; - } - std::string real = DoubleToString(value); - // Ensure that the number has a .0 if there's no decimal or 'e'. This - // makes sure that when we read the JSON back, it's interpreted as a - // real rather than an int. - if (real.find('.') == std::string::npos && - real.find('e') == std::string::npos && - real.find('E') == std::string::npos) { - real.append(".0"); - } - // The JSON spec requires that non-integer values in the range (-1,1) - // have a zero before the decimal point - ".52" is not valid, "0.52" is. - if (real[0] == '.') { - real.insert(0U, 1U, '0'); - } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { - // "-.1" bad "-0.1" good - real.insert(1U, 1U, '0'); - } - json_string_->append(real); - break; + std::string real = DoubleToString(value); + // Ensure that the number has a .0 if there's no decimal or 'e'. This + // makes sure that when we read the JSON back, it's interpreted as a + // real rather than an int. + if (real.find('.') == std::string::npos && + real.find('e') == std::string::npos && + real.find('E') == std::string::npos) { + real.append(".0"); } - - case Value::TYPE_STRING: - { - std::string value; - bool result = node->GetAsString(&value); - DCHECK(result); - EscapeJSONString(value, true, json_string_); - break; + // The JSON spec requires that non-integer values in the range (-1,1) + // have a zero before the decimal point - ".52" is not valid, "0.52" is. + if (real[0] == '.') { + real.insert(0U, 1U, '0'); + } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { + // "-.1" bad "-0.1" good + real.insert(1U, 1U, '0'); } + json_string_->append(real); + break; + } - case Value::TYPE_LIST: - { - json_string_->push_back('['); - if (pretty_print_) - json_string_->push_back(' '); + case Value::TYPE_STRING: { + std::string value; + bool result = node->GetAsString(&value); + DCHECK(result); + EscapeJSONString(value, true, json_string_); + break; + } - const ListValue* list = static_cast<const ListValue*>(node); - for (ListValue::const_iterator it = list->begin(); it != list->end(); - ++it) { - const Value* value = *it; + case Value::TYPE_LIST: { + json_string_->push_back('['); + if (pretty_print_) + json_string_->push_back(' '); - if (omit_binary_values_ && value->GetType() == Value::TYPE_BINARY) - continue; + const ListValue* list = static_cast<const ListValue*>(node); + for (ListValue::const_iterator it = list->begin(); it != list->end(); + ++it) { + const Value* value = *it; - if (it != list->begin()) { - json_string_->push_back(','); - if (pretty_print_) - json_string_->push_back(' '); - } + if (omit_binary_values_ && value->GetType() == Value::TYPE_BINARY) + continue; - BuildJSONString(value, depth); + if (it != list->begin()) { + json_string_->push_back(','); + if (pretty_print_) + json_string_->push_back(' '); } - if (pretty_print_) - json_string_->push_back(' '); - json_string_->push_back(']'); - break; + BuildJSONString(value, depth); } - case Value::TYPE_DICTIONARY: - { - json_string_->push_back('{'); - if (pretty_print_) - json_string_->append(kPrettyPrintLineEnding); - - const DictionaryValue* dict = - static_cast<const DictionaryValue*>(node); - bool first_entry = true; - for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); - itr.Advance(), first_entry = false) { - if (omit_binary_values_ && - itr.value().GetType() == Value::TYPE_BINARY) { - continue; - } - - if (!first_entry) { - json_string_->push_back(','); - if (pretty_print_) - json_string_->append(kPrettyPrintLineEnding); - } + if (pretty_print_) + json_string_->push_back(' '); + json_string_->push_back(']'); + break; + } + + case Value::TYPE_DICTIONARY: { + json_string_->push_back('{'); + if (pretty_print_) + json_string_->append(kPrettyPrintLineEnding); + + const DictionaryValue* dict = + static_cast<const DictionaryValue*>(node); + bool first_entry = true; + for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); + itr.Advance(), first_entry = false) { + if (omit_binary_values_ && + itr.value().GetType() == Value::TYPE_BINARY) { + continue; + } + if (!first_entry) { + json_string_->push_back(','); if (pretty_print_) - IndentLine(depth + 1U); + json_string_->append(kPrettyPrintLineEnding); + } - EscapeJSONString(itr.key(), true, json_string_); + if (pretty_print_) + IndentLine(depth + 1U); - json_string_->push_back(':'); - if (pretty_print_) - json_string_->push_back(' '); - BuildJSONString(&itr.value(), depth + 1U); - } + EscapeJSONString(itr.key(), true, json_string_); - if (pretty_print_) { - json_string_->append(kPrettyPrintLineEnding); - IndentLine(depth); - json_string_->push_back('}'); - } else { - json_string_->push_back('}'); - } - break; + json_string_->push_back(':'); + if (pretty_print_) + json_string_->push_back(' '); + BuildJSONString(&itr.value(), depth + 1U); } - case Value::TYPE_BINARY: - { - if (!omit_binary_values_) { - NOTREACHED() << "Cannot serialize binary value."; - } - break; + if (pretty_print_) { + json_string_->append(kPrettyPrintLineEnding); + IndentLine(depth); + json_string_->push_back('}'); + } else { + json_string_->push_back('}'); } + break; + } + + case Value::TYPE_BINARY: { + if (!omit_binary_values_) { + NOTREACHED() << "Cannot serialize binary value."; + } + break; + } - default: + default: { NOTREACHED() << "unknown json type"; + } } } |