kkania@chromium.org | 31679d9 | 2012-03-09 07:55:29 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
brettw@chromium.org | 7cd41eb | 2009-10-24 05:00:20 +0900 | [diff] [blame] | 5 | #include "base/json/json_writer.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 6 | |
avi | 5853c89 | 2015-12-04 01:46:54 +0900 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
kkania@chromium.org | 31679d9 | 2012-03-09 07:55:29 +0900 | [diff] [blame] | 9 | #include <cmath> |
avi | 5853c89 | 2015-12-04 01:46:54 +0900 | [diff] [blame] | 10 | #include <limits> |
kkania@chromium.org | 31679d9 | 2012-03-09 07:55:29 +0900 | [diff] [blame] | 11 | |
brettw@chromium.org | 7cd41eb | 2009-10-24 05:00:20 +0900 | [diff] [blame] | 12 | #include "base/json/string_escape.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | #include "base/logging.h" |
brettw@chromium.org | abcde5c | 2013-02-07 11:57:22 +0900 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
tfarina@chromium.org | 4af74f5 | 2013-03-21 01:50:32 +0900 | [diff] [blame] | 16 | #include "base/values.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 17 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 18 | |
brettw@chromium.org | 7cd41eb | 2009-10-24 05:00:20 +0900 | [diff] [blame] | 19 | namespace base { |
| 20 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 21 | #if defined(OS_WIN) |
gab@chromium.org | edbdd40 | 2014-01-30 07:50:28 +0900 | [diff] [blame] | 22 | const char kPrettyPrintLineEnding[] = "\r\n"; |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 23 | #else |
gab@chromium.org | edbdd40 | 2014-01-30 07:50:28 +0900 | [diff] [blame] | 24 | const char kPrettyPrintLineEnding[] = "\n"; |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 25 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 26 | |
rsesek@chromium.org | dc66ca7 | 2013-12-12 07:10:45 +0900 | [diff] [blame] | 27 | // static |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 28 | bool JSONWriter::Write(const Value& node, std::string* json) { |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 29 | return WriteWithOptions(node, 0, json); |
pfeldman@chromium.org | f68146b | 2009-05-15 19:13:28 +0900 | [diff] [blame] | 30 | } |
| 31 | |
rsesek@chromium.org | dc66ca7 | 2013-12-12 07:10:45 +0900 | [diff] [blame] | 32 | // static |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 33 | bool JSONWriter::WriteWithOptions(const Value& node, |
| 34 | int options, |
ericdingle@chromium.org | dfb9b25 | 2011-11-18 11:29:54 +0900 | [diff] [blame] | 35 | std::string* json) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 36 | json->clear(); |
| 37 | // Is there a better way to estimate the size of the output? |
| 38 | json->reserve(1024); |
ericdingle@chromium.org | 3cfd016 | 2012-03-16 10:59:55 +0900 | [diff] [blame] | 39 | |
gab@chromium.org | edbdd40 | 2014-01-30 07:50:28 +0900 | [diff] [blame] | 40 | JSONWriter writer(options, json); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 41 | bool result = writer.BuildJSONString(node, 0U); |
ericdingle@chromium.org | 3cfd016 | 2012-03-16 10:59:55 +0900 | [diff] [blame] | 42 | |
gab@chromium.org | edbdd40 | 2014-01-30 07:50:28 +0900 | [diff] [blame] | 43 | if (options & OPTIONS_PRETTY_PRINT) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | json->append(kPrettyPrintLineEnding); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 45 | |
| 46 | return result; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 47 | } |
| 48 | |
gab@chromium.org | edbdd40 | 2014-01-30 07:50:28 +0900 | [diff] [blame] | 49 | JSONWriter::JSONWriter(int options, std::string* json) |
| 50 | : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0), |
| 51 | omit_double_type_preservation_( |
| 52 | (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0), |
| 53 | pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0), |
ericdingle@chromium.org | 3cfd016 | 2012-03-16 10:59:55 +0900 | [diff] [blame] | 54 | json_string_(json) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 55 | DCHECK(json); |
| 56 | } |
| 57 | |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 58 | bool JSONWriter::BuildJSONString(const Value& node, size_t depth) { |
| 59 | switch (node.GetType()) { |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 60 | case Value::Type::NONE: { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 61 | json_string_->append("null"); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 62 | return true; |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 63 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 64 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 65 | case Value::Type::BOOLEAN: { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 66 | bool value; |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 67 | bool result = node.GetAsBoolean(&value); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 68 | DCHECK(result); |
| 69 | json_string_->append(value ? "true" : "false"); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 70 | return result; |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 71 | } |
| 72 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 73 | case Value::Type::INTEGER: { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 74 | int value; |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 75 | bool result = node.GetAsInteger(&value); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 76 | DCHECK(result); |
| 77 | json_string_->append(IntToString(value)); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 78 | return result; |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 79 | } |
| 80 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 81 | case Value::Type::DOUBLE: { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 82 | double value; |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 83 | bool result = node.GetAsDouble(&value); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 84 | DCHECK(result); |
| 85 | if (omit_double_type_preservation_ && |
avi | 5853c89 | 2015-12-04 01:46:54 +0900 | [diff] [blame] | 86 | value <= std::numeric_limits<int64_t>::max() && |
| 87 | value >= std::numeric_limits<int64_t>::min() && |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 88 | std::floor(value) == value) { |
avi | 5853c89 | 2015-12-04 01:46:54 +0900 | [diff] [blame] | 89 | json_string_->append(Int64ToString(static_cast<int64_t>(value))); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 90 | return result; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 91 | } |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 92 | std::string real = DoubleToString(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('.') == std::string::npos && |
| 97 | real.find('e') == std::string::npos && |
| 98 | real.find('E') == std::string::npos) { |
| 99 | real.append(".0"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 100 | } |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 101 | // The JSON spec requires that non-integer values in the range (-1,1) |
| 102 | // have a zero before the decimal point - ".52" is not valid, "0.52" is. |
| 103 | if (real[0] == '.') { |
yang.gu@intel.com | 103973a | 2014-03-01 00:25:31 +0900 | [diff] [blame] | 104 | real.insert(static_cast<size_t>(0), static_cast<size_t>(1), '0'); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 105 | } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { |
| 106 | // "-.1" bad "-0.1" good |
yang.gu@intel.com | 103973a | 2014-03-01 00:25:31 +0900 | [diff] [blame] | 107 | real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0'); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 108 | } |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 109 | json_string_->append(real); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 110 | return result; |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 111 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 112 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 113 | case Value::Type::STRING: { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 114 | std::string value; |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 115 | bool result = node.GetAsString(&value); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 116 | DCHECK(result); |
| 117 | EscapeJSONString(value, true, json_string_); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 118 | return result; |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 119 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 120 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 121 | case Value::Type::LIST: { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 122 | json_string_->push_back('['); |
| 123 | if (pretty_print_) |
| 124 | json_string_->push_back(' '); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 125 | |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 126 | const ListValue* list = NULL; |
| 127 | bool first_value_has_been_output = false; |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 128 | bool result = node.GetAsList(&list); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 129 | DCHECK(result); |
dcheng | 1fa44fb | 2016-05-26 03:30:47 +0900 | [diff] [blame] | 130 | for (const auto& value : *list) { |
jdoerrie | 0143ef0 | 2017-04-11 23:20:20 +0900 | [diff] [blame^] | 131 | if (omit_binary_values_ && value.GetType() == Value::Type::BINARY) |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 132 | continue; |
ericdingle@chromium.org | dfb9b25 | 2011-11-18 11:29:54 +0900 | [diff] [blame] | 133 | |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 134 | if (first_value_has_been_output) { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 135 | json_string_->push_back(','); |
gab@chromium.org | edbdd40 | 2014-01-30 07:50:28 +0900 | [diff] [blame] | 136 | if (pretty_print_) |
| 137 | json_string_->push_back(' '); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 138 | } |
| 139 | |
jdoerrie | 0143ef0 | 2017-04-11 23:20:20 +0900 | [diff] [blame^] | 140 | if (!BuildJSONString(value, depth)) |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 141 | result = false; |
| 142 | |
| 143 | first_value_has_been_output = true; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 144 | } |
| 145 | |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 146 | if (pretty_print_) |
| 147 | json_string_->push_back(' '); |
| 148 | json_string_->push_back(']'); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 149 | return result; |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 150 | } |
| 151 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 152 | case Value::Type::DICTIONARY: { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 153 | json_string_->push_back('{'); |
| 154 | if (pretty_print_) |
| 155 | json_string_->append(kPrettyPrintLineEnding); |
| 156 | |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 157 | const DictionaryValue* dict = NULL; |
| 158 | bool first_value_has_been_output = false; |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 159 | bool result = node.GetAsDictionary(&dict); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 160 | DCHECK(result); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 161 | for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 162 | itr.Advance()) { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 163 | if (omit_binary_values_ && |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 164 | itr.value().GetType() == Value::Type::BINARY) { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 165 | continue; |
ericdingle@chromium.org | dfb9b25 | 2011-11-18 11:29:54 +0900 | [diff] [blame] | 166 | } |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 167 | |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 168 | if (first_value_has_been_output) { |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 169 | json_string_->push_back(','); |
| 170 | if (pretty_print_) |
| 171 | json_string_->append(kPrettyPrintLineEnding); |
| 172 | } |
| 173 | |
| 174 | if (pretty_print_) |
| 175 | IndentLine(depth + 1U); |
| 176 | |
| 177 | EscapeJSONString(itr.key(), true, json_string_); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 178 | json_string_->push_back(':'); |
| 179 | if (pretty_print_) |
| 180 | json_string_->push_back(' '); |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 181 | |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 182 | if (!BuildJSONString(itr.value(), depth + 1U)) |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 183 | result = false; |
| 184 | |
| 185 | first_value_has_been_output = true; |
ericdingle@chromium.org | dfb9b25 | 2011-11-18 11:29:54 +0900 | [diff] [blame] | 186 | } |
| 187 | |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 188 | if (pretty_print_) { |
| 189 | json_string_->append(kPrettyPrintLineEnding); |
| 190 | IndentLine(depth); |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 191 | } |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 192 | |
| 193 | json_string_->push_back('}'); |
| 194 | return result; |
tsepez@chromium.org | 1d7a4c4 | 2014-02-06 18:24:48 +0900 | [diff] [blame] | 195 | } |
| 196 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 197 | case Value::Type::BINARY: |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 198 | // Successful only if we're allowed to omit it. |
| 199 | DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value."; |
| 200 | return omit_binary_values_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 201 | } |
tsepez@chromium.org | 71ec72b | 2014-02-08 07:47:39 +0900 | [diff] [blame] | 202 | NOTREACHED(); |
| 203 | return false; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 204 | } |
| 205 | |
gab@chromium.org | edbdd40 | 2014-01-30 07:50:28 +0900 | [diff] [blame] | 206 | void JSONWriter::IndentLine(size_t depth) { |
| 207 | json_string_->append(depth * 3U, ' '); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 208 | } |
brettw@chromium.org | 7cd41eb | 2009-10-24 05:00:20 +0900 | [diff] [blame] | 209 | |
| 210 | } // namespace base |