| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/json/json_writer.h" | 5 #include "base/json/json_writer.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0), | 50 : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0), |
| 51 omit_double_type_preservation_( | 51 omit_double_type_preservation_( |
| 52 (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0), | 52 (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0), |
| 53 pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0), | 53 pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0), |
| 54 json_string_(json) { | 54 json_string_(json) { |
| 55 DCHECK(json); | 55 DCHECK(json); |
| 56 } | 56 } |
| 57 | 57 |
| 58 bool JSONWriter::BuildJSONString(const Value& node, size_t depth) { | 58 bool JSONWriter::BuildJSONString(const Value& node, size_t depth) { |
| 59 switch (node.GetType()) { | 59 switch (node.GetType()) { |
| 60 case Value::TYPE_NULL: { | 60 case Value::Type::NONE: { |
| 61 json_string_->append("null"); | 61 json_string_->append("null"); |
| 62 return true; | 62 return true; |
| 63 } | 63 } |
| 64 | 64 |
| 65 case Value::TYPE_BOOLEAN: { | 65 case Value::Type::BOOLEAN: { |
| 66 bool value; | 66 bool value; |
| 67 bool result = node.GetAsBoolean(&value); | 67 bool result = node.GetAsBoolean(&value); |
| 68 DCHECK(result); | 68 DCHECK(result); |
| 69 json_string_->append(value ? "true" : "false"); | 69 json_string_->append(value ? "true" : "false"); |
| 70 return result; | 70 return result; |
| 71 } | 71 } |
| 72 | 72 |
| 73 case Value::TYPE_INTEGER: { | 73 case Value::Type::INTEGER: { |
| 74 int value; | 74 int value; |
| 75 bool result = node.GetAsInteger(&value); | 75 bool result = node.GetAsInteger(&value); |
| 76 DCHECK(result); | 76 DCHECK(result); |
| 77 json_string_->append(IntToString(value)); | 77 json_string_->append(IntToString(value)); |
| 78 return result; | 78 return result; |
| 79 } | 79 } |
| 80 | 80 |
| 81 case Value::TYPE_DOUBLE: { | 81 case Value::Type::DOUBLE: { |
| 82 double value; | 82 double value; |
| 83 bool result = node.GetAsDouble(&value); | 83 bool result = node.GetAsDouble(&value); |
| 84 DCHECK(result); | 84 DCHECK(result); |
| 85 if (omit_double_type_preservation_ && | 85 if (omit_double_type_preservation_ && |
| 86 value <= std::numeric_limits<int64_t>::max() && | 86 value <= std::numeric_limits<int64_t>::max() && |
| 87 value >= std::numeric_limits<int64_t>::min() && | 87 value >= std::numeric_limits<int64_t>::min() && |
| 88 std::floor(value) == value) { | 88 std::floor(value) == value) { |
| 89 json_string_->append(Int64ToString(static_cast<int64_t>(value))); | 89 json_string_->append(Int64ToString(static_cast<int64_t>(value))); |
| 90 return result; | 90 return result; |
| 91 } | 91 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 103 if (real[0] == '.') { | 103 if (real[0] == '.') { |
| 104 real.insert(static_cast<size_t>(0), static_cast<size_t>(1), '0'); | 104 real.insert(static_cast<size_t>(0), static_cast<size_t>(1), '0'); |
| 105 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { | 105 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { |
| 106 // "-.1" bad "-0.1" good | 106 // "-.1" bad "-0.1" good |
| 107 real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0'); | 107 real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0'); |
| 108 } | 108 } |
| 109 json_string_->append(real); | 109 json_string_->append(real); |
| 110 return result; | 110 return result; |
| 111 } | 111 } |
| 112 | 112 |
| 113 case Value::TYPE_STRING: { | 113 case Value::Type::STRING: { |
| 114 std::string value; | 114 std::string value; |
| 115 bool result = node.GetAsString(&value); | 115 bool result = node.GetAsString(&value); |
| 116 DCHECK(result); | 116 DCHECK(result); |
| 117 EscapeJSONString(value, true, json_string_); | 117 EscapeJSONString(value, true, json_string_); |
| 118 return result; | 118 return result; |
| 119 } | 119 } |
| 120 | 120 |
| 121 case Value::TYPE_LIST: { | 121 case Value::Type::LIST: { |
| 122 json_string_->push_back('['); | 122 json_string_->push_back('['); |
| 123 if (pretty_print_) | 123 if (pretty_print_) |
| 124 json_string_->push_back(' '); | 124 json_string_->push_back(' '); |
| 125 | 125 |
| 126 const ListValue* list = NULL; | 126 const ListValue* list = NULL; |
| 127 bool first_value_has_been_output = false; | 127 bool first_value_has_been_output = false; |
| 128 bool result = node.GetAsList(&list); | 128 bool result = node.GetAsList(&list); |
| 129 DCHECK(result); | 129 DCHECK(result); |
| 130 for (const auto& value : *list) { | 130 for (const auto& value : *list) { |
| 131 if (omit_binary_values_ && value->GetType() == Value::TYPE_BINARY) | 131 if (omit_binary_values_ && value->GetType() == Value::Type::BINARY) |
| 132 continue; | 132 continue; |
| 133 | 133 |
| 134 if (first_value_has_been_output) { | 134 if (first_value_has_been_output) { |
| 135 json_string_->push_back(','); | 135 json_string_->push_back(','); |
| 136 if (pretty_print_) | 136 if (pretty_print_) |
| 137 json_string_->push_back(' '); | 137 json_string_->push_back(' '); |
| 138 } | 138 } |
| 139 | 139 |
| 140 if (!BuildJSONString(*value, depth)) | 140 if (!BuildJSONString(*value, depth)) |
| 141 result = false; | 141 result = false; |
| 142 | 142 |
| 143 first_value_has_been_output = true; | 143 first_value_has_been_output = true; |
| 144 } | 144 } |
| 145 | 145 |
| 146 if (pretty_print_) | 146 if (pretty_print_) |
| 147 json_string_->push_back(' '); | 147 json_string_->push_back(' '); |
| 148 json_string_->push_back(']'); | 148 json_string_->push_back(']'); |
| 149 return result; | 149 return result; |
| 150 } | 150 } |
| 151 | 151 |
| 152 case Value::TYPE_DICTIONARY: { | 152 case Value::Type::DICTIONARY: { |
| 153 json_string_->push_back('{'); | 153 json_string_->push_back('{'); |
| 154 if (pretty_print_) | 154 if (pretty_print_) |
| 155 json_string_->append(kPrettyPrintLineEnding); | 155 json_string_->append(kPrettyPrintLineEnding); |
| 156 | 156 |
| 157 const DictionaryValue* dict = NULL; | 157 const DictionaryValue* dict = NULL; |
| 158 bool first_value_has_been_output = false; | 158 bool first_value_has_been_output = false; |
| 159 bool result = node.GetAsDictionary(&dict); | 159 bool result = node.GetAsDictionary(&dict); |
| 160 DCHECK(result); | 160 DCHECK(result); |
| 161 for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); | 161 for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); |
| 162 itr.Advance()) { | 162 itr.Advance()) { |
| 163 if (omit_binary_values_ && | 163 if (omit_binary_values_ && |
| 164 itr.value().GetType() == Value::TYPE_BINARY) { | 164 itr.value().GetType() == Value::Type::BINARY) { |
| 165 continue; | 165 continue; |
| 166 } | 166 } |
| 167 | 167 |
| 168 if (first_value_has_been_output) { | 168 if (first_value_has_been_output) { |
| 169 json_string_->push_back(','); | 169 json_string_->push_back(','); |
| 170 if (pretty_print_) | 170 if (pretty_print_) |
| 171 json_string_->append(kPrettyPrintLineEnding); | 171 json_string_->append(kPrettyPrintLineEnding); |
| 172 } | 172 } |
| 173 | 173 |
| 174 if (pretty_print_) | 174 if (pretty_print_) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 187 | 187 |
| 188 if (pretty_print_) { | 188 if (pretty_print_) { |
| 189 json_string_->append(kPrettyPrintLineEnding); | 189 json_string_->append(kPrettyPrintLineEnding); |
| 190 IndentLine(depth); | 190 IndentLine(depth); |
| 191 } | 191 } |
| 192 | 192 |
| 193 json_string_->push_back('}'); | 193 json_string_->push_back('}'); |
| 194 return result; | 194 return result; |
| 195 } | 195 } |
| 196 | 196 |
| 197 case Value::TYPE_BINARY: | 197 case Value::Type::BINARY: |
| 198 // Successful only if we're allowed to omit it. | 198 // Successful only if we're allowed to omit it. |
| 199 DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value."; | 199 DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value."; |
| 200 return omit_binary_values_; | 200 return omit_binary_values_; |
| 201 } | 201 } |
| 202 NOTREACHED(); | 202 NOTREACHED(); |
| 203 return false; | 203 return false; |
| 204 } | 204 } |
| 205 | 205 |
| 206 void JSONWriter::IndentLine(size_t depth) { | 206 void JSONWriter::IndentLine(size_t depth) { |
| 207 json_string_->append(depth * 3U, ' '); | 207 json_string_->append(depth * 3U, ' '); |
| 208 } | 208 } |
| 209 | 209 |
| 210 } // namespace base | 210 } // namespace base |
| OLD | NEW |