| 6 |
| 7 | #include <stdint.h> |
| 8 |
| 9 | #include <cmath> |
| 10 | #include <limits> |
| 11 |
| 12 | #include "base/json/string_escape.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/strings/utf_string_conversions.h" |
| 16 | #include "base/values.h" |
| 17 | #include "build/build_config.h" |
| 18 |
| 19 | namespace base { |
| 20 |
| 21 | #if defined(OS_WIN) |
| 22 | const char kPrettyPrintLineEnding[] = "\r\n"; |
| 23 | #else |
| 24 | const char kPrettyPrintLineEnding[] = "\n"; |
| 25 | #endif |
| 26 |
| 27 | // static |
| 28 | bool JSONWriter::Write(const Value& node, std::string* json, size_t max_depth) { |
| 29 | return WriteWithOptions(node, 0, json, max_depth); |
| 30 | } |
| 31 |
| 32 | // static |
| 33 | bool JSONWriter::WriteWithOptions(const Value& node, |
| 34 | int options, |
| 35 | std::string* json, |
| 36 | size_t max_depth) { |
| 37 | json->clear(); |
| 38 | // Is there a better way to estimate the size of the output? |
| 39 | json->reserve(1024); |
| 40 |
| 41 | JSONWriter writer(options, json, max_depth); |
| 42 | bool result = writer.BuildJSONString(node, 0U); |
| 43 |
| 44 | if (options & OPTIONS_PRETTY_PRINT) |
| 45 | json->append(kPrettyPrintLineEnding); |
| 46 |
| 47 | return result; |
| 48 | } |
| 49 |
| 50 | JSONWriter::JSONWriter(int options, std::string* json, size_t max_depth) |
| 51 | : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0), |
| 52 | omit_double_type_preservation_( |
| 53 | (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0), |
| 54 | pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0), |
| 55 | json_string_(json), |
| 56 | max_depth_(max_depth), |
| 57 | stack_depth_(0) { |
| 58 | DCHECK(json); |
| 59 | CHECK_LE(max_depth, internal::kAbsoluteMaxDepth); |
| 60 | } |
| 61 |
| 62 | bool JSONWriter::BuildJSONString(const Value& node, size_t depth) { |
| 63 | internal::StackMarker depth_check(max_depth_, &stack_depth_); |
| 64 | if (depth_check.IsTooDeep()) |
| 65 | return false; |
| 66 |
| 67 | switch (node.type()) { |
| 68 | case Value::Type::NONE: |
| 69 | json_string_->append("null"); |
| 70 | return true; |
| 71 |
| 72 | case Value::Type::BOOLEAN: |
| 73 | // Note: We are explicitly invoking the std::string constructor in order |
| 74 | // to avoid ASAN errors on Windows: https://crbug.com/900041 |
| 75 | json_string_->append(node.GetBool() ? std::string("true") |
| 76 | : std::string("false")); |
| 77 | return true; |
| 78 |
| 79 | case Value::Type::INTEGER: |
| 80 | json_string_->append(NumberToString(node.GetInt())); |
| 81 | return true; |
| 82 |
| 83 | case Value::Type::DOUBLE: { |
| 84 | double value = node.GetDouble(); |
| 85 | if (omit_double_type_preservation_ && |
| 86 | value <= std::numeric_limits<int64_t>::max() && |
| 87 | value >= std::numeric_limits<int64_t>::min() && |
| 88 | std::floor(value) == value) { |
| 89 | json_string_->append(NumberToString(static_cast<int64_t>(value))); |
| 90 | return true; |
| 91 | } |
| 92 | std::string real = NumberToString(value); |
| 93 | // Ensure that the number has a .0 if there's no decimal or 'e'. This |
| 94 | // makes sure that when we read the JSON back, it's interpreted as a |
| 95 | // real rather than an int. |
| 96 | if (real.find_first_of(".eE") == std::string::npos) |
| 97 | real.append(".0"); |
| 98 |
| 99 | // The JSON spec requires that non-integer values in the range (-1,1) |
| 100 | // have a zero before the decimal point - ".52" is not valid, "0.52" is. |
| 101 | if (real[0] == '.') { |
| 102 | real.insert(static_cast<size_t>(0), static_cast<size_t>(1), '0'); |
| 103 | } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { |
| 104 | // "-.1" bad "-0.1" good |
| 105 | real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0'); |
| 106 | } |
| 107 | json_string_->append(real); |
| 108 | return true; |
| 109 | } |
| 110 |
| 111 | case Value::Type::STRING: |
| 112 | EscapeJSONString(node.GetString(), true, json_string_); |
| 113 | return true; |
| 114 |
| 115 | case Value::Type::LIST: { |
| 116 | json_string_->push_back('['); |
| 117 | if (pretty_print_) |
| 118 | json_string_->push_back(' '); |
| 119 |
| 120 | bool first_value_has_been_output = false; |
| 121 | bool result = true; |
| 122 | for (const auto& value : node.GetList()) { |
| 123 | if (omit_binary_values_ && value.type() == Value::Type::BINARY) |
| 124 | continue; |
| 125 |
| 126 | if (first_value_has_been_output) { |
| 127 | json_string_->push_back(','); |
| 128 | if (pretty_print_) |
| 129 | json_string_->push_back(' '); |
| 130 | } |
| 131 |
| 132 | if (!BuildJSONString(value, depth)) |
| 133 | result = false; |
| 134 |
| 135 | first_value_has_been_output = true; |
| 136 | } |
| 137 |
| 138 | if (pretty_print_) |
| 139 | json_string_->push_back(' '); |
| 140 | json_string_->push_back(']'); |
| 141 | return result; |
| 142 | } |
| 143 |
| 144 | case Value::Type::DICTIONARY: { |
| 145 | json_string_->push_back('{'); |
| 146 | if (pretty_print_) |
| 147 | json_string_->append(kPrettyPrintLineEnding); |
| 148 |
| 149 | bool first_value_has_been_output = false; |
| 150 | bool result = true; |
| 151 | for (const auto& pair : node.DictItems()) { |
| 152 | const auto& key = pair.first; |
| 153 | const auto& value = pair.second; |
| 154 | if (omit_binary_values_ && value.type() == Value::Type::BINARY) |
| 155 | continue; |
| 156 |
| 157 | if (first_value_has_been_output) { |
| 158 | json_string_->push_back(','); |
| 159 | if (pretty_print_) |
| 160 | json_string_->append(kPrettyPrintLineEnding); |
| 161 | } |
| 162 |
| 163 | if (pretty_print_) |
| 164 | IndentLine(depth + 1U); |
| 165 |
| 166 | EscapeJSONString(key, true, json_string_); |
| 167 | json_string_->push_back(':'); |
| 168 | if (pretty_print_) |
| 169 | json_string_->push_back(' '); |
| 170 |
| 171 | if (!BuildJSONString(value, depth + 1U)) |
| 172 | result = false; |
| 173 |
| 174 | first_value_has_been_output = true; |
| 175 | } |
| 176 |
| 177 | if (pretty_print_) { |
| 178 | json_string_->append(kPrettyPrintLineEnding); |
| 179 | IndentLine(depth); |
| 180 | } |
| 181 |
| 182 | json_string_->push_back('}'); |
| 183 | return result; |
| 184 | } |
| 185 |
| 186 | case Value::Type::BINARY: |
| 187 | // Successful only if we're allowed to omit it. |
| 188 | DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value."; |
| 189 | return omit_binary_values_; |
| 190 |
| 191 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 192 | case Value::Type::DEAD: |
| 193 | CHECK(false); |
| 194 | return false; |
| 195 | } |
| 196 |
| 197 | // TODO(crbug.com/859477): Revert to NOTREACHED() after root cause is found. |
| 198 | CHECK(false); |
| 199 | return false; |
| 200 | } |
| 201 |
| 202 | void JSONWriter::IndentLine(size_t depth) { |
| 203 | json_string_->append(depth * 3U, ' '); |
| 204 | } |
| 205 |
| 206 | } // namespace base |
| 207 |