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 <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
18 const char kPrettyPrintLineEnding[] = "\r\n"; | 18 const char kPrettyPrintLineEnding[] = "\r\n"; |
19 #else | 19 #else |
20 const char kPrettyPrintLineEnding[] = "\n"; | 20 const char kPrettyPrintLineEnding[] = "\n"; |
21 #endif | 21 #endif |
22 | 22 |
23 // static | 23 // static |
24 bool JSONWriter::Write(const Value* const node, std::string* json) { | 24 bool JSONWriter::Write(const Value& node, std::string* json) { |
25 return WriteWithOptions(node, 0, json); | 25 return WriteWithOptions(node, 0, json); |
26 } | 26 } |
27 | 27 |
28 // static | 28 // static |
29 bool JSONWriter::WriteWithOptions(const Value* const node, int options, | 29 bool JSONWriter::WriteWithOptions(const Value& node, |
| 30 int options, |
30 std::string* json) { | 31 std::string* json) { |
31 json->clear(); | 32 json->clear(); |
32 // Is there a better way to estimate the size of the output? | 33 // Is there a better way to estimate the size of the output? |
33 json->reserve(1024); | 34 json->reserve(1024); |
34 | 35 |
35 JSONWriter writer(options, json); | 36 JSONWriter writer(options, json); |
36 bool result = writer.BuildJSONString(node, 0U); | 37 bool result = writer.BuildJSONString(node, 0U); |
37 | 38 |
38 if (options & OPTIONS_PRETTY_PRINT) | 39 if (options & OPTIONS_PRETTY_PRINT) |
39 json->append(kPrettyPrintLineEnding); | 40 json->append(kPrettyPrintLineEnding); |
40 | 41 |
41 return result; | 42 return result; |
42 } | 43 } |
43 | 44 |
44 JSONWriter::JSONWriter(int options, std::string* json) | 45 JSONWriter::JSONWriter(int options, std::string* json) |
45 : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0), | 46 : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0), |
46 omit_double_type_preservation_( | 47 omit_double_type_preservation_( |
47 (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0), | 48 (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0), |
48 pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0), | 49 pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0), |
49 json_string_(json) { | 50 json_string_(json) { |
50 DCHECK(json); | 51 DCHECK(json); |
51 } | 52 } |
52 | 53 |
53 bool JSONWriter::BuildJSONString(const Value* const node, size_t depth) { | 54 bool JSONWriter::BuildJSONString(const Value& node, size_t depth) { |
54 switch (node->GetType()) { | 55 switch (node.GetType()) { |
55 case Value::TYPE_NULL: { | 56 case Value::TYPE_NULL: { |
56 json_string_->append("null"); | 57 json_string_->append("null"); |
57 return true; | 58 return true; |
58 } | 59 } |
59 | 60 |
60 case Value::TYPE_BOOLEAN: { | 61 case Value::TYPE_BOOLEAN: { |
61 bool value; | 62 bool value; |
62 bool result = node->GetAsBoolean(&value); | 63 bool result = node.GetAsBoolean(&value); |
63 DCHECK(result); | 64 DCHECK(result); |
64 json_string_->append(value ? "true" : "false"); | 65 json_string_->append(value ? "true" : "false"); |
65 return result; | 66 return result; |
66 } | 67 } |
67 | 68 |
68 case Value::TYPE_INTEGER: { | 69 case Value::TYPE_INTEGER: { |
69 int value; | 70 int value; |
70 bool result = node->GetAsInteger(&value); | 71 bool result = node.GetAsInteger(&value); |
71 DCHECK(result); | 72 DCHECK(result); |
72 json_string_->append(IntToString(value)); | 73 json_string_->append(IntToString(value)); |
73 return result; | 74 return result; |
74 } | 75 } |
75 | 76 |
76 case Value::TYPE_DOUBLE: { | 77 case Value::TYPE_DOUBLE: { |
77 double value; | 78 double value; |
78 bool result = node->GetAsDouble(&value); | 79 bool result = node.GetAsDouble(&value); |
79 DCHECK(result); | 80 DCHECK(result); |
80 if (omit_double_type_preservation_ && | 81 if (omit_double_type_preservation_ && |
81 value <= kint64max && | 82 value <= kint64max && |
82 value >= kint64min && | 83 value >= kint64min && |
83 std::floor(value) == value) { | 84 std::floor(value) == value) { |
84 json_string_->append(Int64ToString(static_cast<int64>(value))); | 85 json_string_->append(Int64ToString(static_cast<int64>(value))); |
85 return result; | 86 return result; |
86 } | 87 } |
87 std::string real = DoubleToString(value); | 88 std::string real = DoubleToString(value); |
88 // Ensure that the number has a .0 if there's no decimal or 'e'. This | 89 // Ensure that the number has a .0 if there's no decimal or 'e'. This |
(...skipping 11 matching lines...) Expand all Loading... |
100 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { | 101 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { |
101 // "-.1" bad "-0.1" good | 102 // "-.1" bad "-0.1" good |
102 real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0'); | 103 real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0'); |
103 } | 104 } |
104 json_string_->append(real); | 105 json_string_->append(real); |
105 return result; | 106 return result; |
106 } | 107 } |
107 | 108 |
108 case Value::TYPE_STRING: { | 109 case Value::TYPE_STRING: { |
109 std::string value; | 110 std::string value; |
110 bool result = node->GetAsString(&value); | 111 bool result = node.GetAsString(&value); |
111 DCHECK(result); | 112 DCHECK(result); |
112 EscapeJSONString(value, true, json_string_); | 113 EscapeJSONString(value, true, json_string_); |
113 return result; | 114 return result; |
114 } | 115 } |
115 | 116 |
116 case Value::TYPE_LIST: { | 117 case Value::TYPE_LIST: { |
117 json_string_->push_back('['); | 118 json_string_->push_back('['); |
118 if (pretty_print_) | 119 if (pretty_print_) |
119 json_string_->push_back(' '); | 120 json_string_->push_back(' '); |
120 | 121 |
121 const ListValue* list = NULL; | 122 const ListValue* list = NULL; |
122 bool first_value_has_been_output = false; | 123 bool first_value_has_been_output = false; |
123 bool result = node->GetAsList(&list); | 124 bool result = node.GetAsList(&list); |
124 DCHECK(result); | 125 DCHECK(result); |
125 for (ListValue::const_iterator it = list->begin(); it != list->end(); | 126 for (ListValue::const_iterator it = list->begin(); it != list->end(); |
126 ++it) { | 127 ++it) { |
127 const Value* value = *it; | 128 const Value* value = *it; |
128 if (omit_binary_values_ && value->GetType() == Value::TYPE_BINARY) | 129 if (omit_binary_values_ && value->GetType() == Value::TYPE_BINARY) |
129 continue; | 130 continue; |
130 | 131 |
131 if (first_value_has_been_output) { | 132 if (first_value_has_been_output) { |
132 json_string_->push_back(','); | 133 json_string_->push_back(','); |
133 if (pretty_print_) | 134 if (pretty_print_) |
134 json_string_->push_back(' '); | 135 json_string_->push_back(' '); |
135 } | 136 } |
136 | 137 |
137 if (!BuildJSONString(value, depth)) | 138 if (!BuildJSONString(*value, depth)) |
138 result = false; | 139 result = false; |
139 | 140 |
140 first_value_has_been_output = true; | 141 first_value_has_been_output = true; |
141 } | 142 } |
142 | 143 |
143 if (pretty_print_) | 144 if (pretty_print_) |
144 json_string_->push_back(' '); | 145 json_string_->push_back(' '); |
145 json_string_->push_back(']'); | 146 json_string_->push_back(']'); |
146 return result; | 147 return result; |
147 } | 148 } |
148 | 149 |
149 case Value::TYPE_DICTIONARY: { | 150 case Value::TYPE_DICTIONARY: { |
150 json_string_->push_back('{'); | 151 json_string_->push_back('{'); |
151 if (pretty_print_) | 152 if (pretty_print_) |
152 json_string_->append(kPrettyPrintLineEnding); | 153 json_string_->append(kPrettyPrintLineEnding); |
153 | 154 |
154 const DictionaryValue* dict = NULL; | 155 const DictionaryValue* dict = NULL; |
155 bool first_value_has_been_output = false; | 156 bool first_value_has_been_output = false; |
156 bool result = node->GetAsDictionary(&dict); | 157 bool result = node.GetAsDictionary(&dict); |
157 DCHECK(result); | 158 DCHECK(result); |
158 for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); | 159 for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd(); |
159 itr.Advance()) { | 160 itr.Advance()) { |
160 if (omit_binary_values_ && | 161 if (omit_binary_values_ && |
161 itr.value().GetType() == Value::TYPE_BINARY) { | 162 itr.value().GetType() == Value::TYPE_BINARY) { |
162 continue; | 163 continue; |
163 } | 164 } |
164 | 165 |
165 if (first_value_has_been_output) { | 166 if (first_value_has_been_output) { |
166 json_string_->push_back(','); | 167 json_string_->push_back(','); |
167 if (pretty_print_) | 168 if (pretty_print_) |
168 json_string_->append(kPrettyPrintLineEnding); | 169 json_string_->append(kPrettyPrintLineEnding); |
169 } | 170 } |
170 | 171 |
171 if (pretty_print_) | 172 if (pretty_print_) |
172 IndentLine(depth + 1U); | 173 IndentLine(depth + 1U); |
173 | 174 |
174 EscapeJSONString(itr.key(), true, json_string_); | 175 EscapeJSONString(itr.key(), true, json_string_); |
175 json_string_->push_back(':'); | 176 json_string_->push_back(':'); |
176 if (pretty_print_) | 177 if (pretty_print_) |
177 json_string_->push_back(' '); | 178 json_string_->push_back(' '); |
178 | 179 |
179 if (!BuildJSONString(&itr.value(), depth + 1U)) | 180 if (!BuildJSONString(itr.value(), depth + 1U)) |
180 result = false; | 181 result = false; |
181 | 182 |
182 first_value_has_been_output = true; | 183 first_value_has_been_output = true; |
183 } | 184 } |
184 | 185 |
185 if (pretty_print_) { | 186 if (pretty_print_) { |
186 json_string_->append(kPrettyPrintLineEnding); | 187 json_string_->append(kPrettyPrintLineEnding); |
187 IndentLine(depth); | 188 IndentLine(depth); |
188 } | 189 } |
189 | 190 |
190 json_string_->push_back('}'); | 191 json_string_->push_back('}'); |
191 return result; | 192 return result; |
192 } | 193 } |
193 | 194 |
194 case Value::TYPE_BINARY: | 195 case Value::TYPE_BINARY: |
195 // Successful only if we're allowed to omit it. | 196 // Successful only if we're allowed to omit it. |
196 DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value."; | 197 DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value."; |
197 return omit_binary_values_; | 198 return omit_binary_values_; |
198 } | 199 } |
199 NOTREACHED(); | 200 NOTREACHED(); |
200 return false; | 201 return false; |
201 } | 202 } |
202 | 203 |
203 void JSONWriter::IndentLine(size_t depth) { | 204 void JSONWriter::IndentLine(size_t depth) { |
204 json_string_->append(depth * 3U, ' '); | 205 json_string_->append(depth * 3U, ' '); |
205 } | 206 } |
206 | 207 |
207 } // namespace base | 208 } // namespace base |
OLD | NEW |