Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: base/json/json_writer.cc

Issue 9016024: Add option for not adding '.0' to the end of doubles that have no fractional (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/json/json_writer.h ('k') | base/json/json_writer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 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>
8
7 #include "base/json/string_escape.h" 9 #include "base/json/string_escape.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
10 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
11 #include "base/values.h" 13 #include "base/values.h"
12 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
13 15
14 namespace base { 16 namespace base {
15 17
16 #if defined(OS_WIN) 18 #if defined(OS_WIN)
(...skipping 16 matching lines...) Expand all
33 void JSONWriter::WriteWithOptions(const Value* const node, 35 void JSONWriter::WriteWithOptions(const Value* const node,
34 bool pretty_print, 36 bool pretty_print,
35 int options, 37 int options,
36 std::string* json) { 38 std::string* json) {
37 json->clear(); 39 json->clear();
38 // Is there a better way to estimate the size of the output? 40 // Is there a better way to estimate the size of the output?
39 json->reserve(1024); 41 json->reserve(1024);
40 JSONWriter writer(pretty_print, json); 42 JSONWriter writer(pretty_print, json);
41 bool escape = !(options & OPTIONS_DO_NOT_ESCAPE); 43 bool escape = !(options & OPTIONS_DO_NOT_ESCAPE);
42 bool omit_binary_values = !!(options & OPTIONS_OMIT_BINARY_VALUES); 44 bool omit_binary_values = !!(options & OPTIONS_OMIT_BINARY_VALUES);
43 writer.BuildJSONString(node, 0, escape, omit_binary_values); 45 bool omit_double_type_preservation =
46 !!(options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION);
47 writer.BuildJSONString(node, 0, escape, omit_binary_values,
48 omit_double_type_preservation);
44 if (pretty_print) 49 if (pretty_print)
45 json->append(kPrettyPrintLineEnding); 50 json->append(kPrettyPrintLineEnding);
46 } 51 }
47 52
48 JSONWriter::JSONWriter(bool pretty_print, std::string* json) 53 JSONWriter::JSONWriter(bool pretty_print, std::string* json)
49 : json_string_(json), 54 : json_string_(json),
50 pretty_print_(pretty_print) { 55 pretty_print_(pretty_print) {
51 DCHECK(json); 56 DCHECK(json);
52 } 57 }
53 58
54 void JSONWriter::BuildJSONString(const Value* const node, 59 void JSONWriter::BuildJSONString(const Value* const node,
55 int depth, 60 int depth,
56 bool escape, 61 bool escape,
57 bool omit_binary_values) { 62 bool omit_binary_values,
63 bool omit_double_type_preservation) {
58 switch (node->GetType()) { 64 switch (node->GetType()) {
59 case Value::TYPE_NULL: 65 case Value::TYPE_NULL:
60 json_string_->append("null"); 66 json_string_->append("null");
61 break; 67 break;
62 68
63 case Value::TYPE_BOOLEAN: 69 case Value::TYPE_BOOLEAN:
64 { 70 {
65 bool value; 71 bool value;
66 bool result = node->GetAsBoolean(&value); 72 bool result = node->GetAsBoolean(&value);
67 DCHECK(result); 73 DCHECK(result);
68 json_string_->append(value ? "true" : "false"); 74 json_string_->append(value ? "true" : "false");
69 break; 75 break;
70 } 76 }
71 77
72 case Value::TYPE_INTEGER: 78 case Value::TYPE_INTEGER:
73 { 79 {
74 int value; 80 int value;
75 bool result = node->GetAsInteger(&value); 81 bool result = node->GetAsInteger(&value);
76 DCHECK(result); 82 DCHECK(result);
77 base::StringAppendF(json_string_, "%d", value); 83 base::StringAppendF(json_string_, "%d", value);
78 break; 84 break;
79 } 85 }
80 86
81 case Value::TYPE_DOUBLE: 87 case Value::TYPE_DOUBLE:
82 { 88 {
83 double value; 89 double value;
84 bool result = node->GetAsDouble(&value); 90 bool result = node->GetAsDouble(&value);
85 DCHECK(result); 91 DCHECK(result);
92 if (omit_double_type_preservation &&
93 value <= kint64max &&
94 value >= kint64min &&
95 std::floor(value) == value) {
96 json_string_->append(Int64ToString(static_cast<int64>(value)));
97 break;
98 }
86 std::string real = DoubleToString(value); 99 std::string real = DoubleToString(value);
87 // Ensure that the number has a .0 if there's no decimal or 'e'. This 100 // Ensure that the number has a .0 if there's no decimal or 'e'. This
88 // makes sure that when we read the JSON back, it's interpreted as a 101 // makes sure that when we read the JSON back, it's interpreted as a
89 // real rather than an int. 102 // real rather than an int.
90 if (real.find('.') == std::string::npos && 103 if (real.find('.') == std::string::npos &&
91 real.find('e') == std::string::npos && 104 real.find('e') == std::string::npos &&
92 real.find('E') == std::string::npos) { 105 real.find('E') == std::string::npos) {
93 real.append(".0"); 106 real.append(".0");
94 } 107 }
95 // The JSON spec requires that non-integer values in the range (-1,1) 108 // The JSON spec requires that non-integer values in the range (-1,1)
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) { 145 if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) {
133 continue; 146 continue;
134 } 147 }
135 148
136 if (i != 0) { 149 if (i != 0) {
137 json_string_->append(","); 150 json_string_->append(",");
138 if (pretty_print_) 151 if (pretty_print_)
139 json_string_->append(" "); 152 json_string_->append(" ");
140 } 153 }
141 154
142 BuildJSONString(value, depth, escape, omit_binary_values); 155 BuildJSONString(value, depth, escape, omit_binary_values,
156 omit_double_type_preservation);
143 } 157 }
144 158
145 if (pretty_print_) 159 if (pretty_print_)
146 json_string_->append(" "); 160 json_string_->append(" ");
147 json_string_->append("]"); 161 json_string_->append("]");
148 break; 162 break;
149 } 163 }
150 164
151 case Value::TYPE_DICTIONARY: 165 case Value::TYPE_DICTIONARY:
152 { 166 {
(...skipping 21 matching lines...) Expand all
174 } 188 }
175 189
176 if (pretty_print_) 190 if (pretty_print_)
177 IndentLine(depth + 1); 191 IndentLine(depth + 1);
178 AppendQuotedString(*key_itr); 192 AppendQuotedString(*key_itr);
179 if (pretty_print_) { 193 if (pretty_print_) {
180 json_string_->append(": "); 194 json_string_->append(": ");
181 } else { 195 } else {
182 json_string_->append(":"); 196 json_string_->append(":");
183 } 197 }
184 BuildJSONString(value, depth + 1, escape, omit_binary_values); 198 BuildJSONString(value, depth + 1, escape, omit_binary_values,
199 omit_double_type_preservation);
185 } 200 }
186 201
187 if (pretty_print_) { 202 if (pretty_print_) {
188 json_string_->append(kPrettyPrintLineEnding); 203 json_string_->append(kPrettyPrintLineEnding);
189 IndentLine(depth); 204 IndentLine(depth);
190 json_string_->append("}"); 205 json_string_->append("}");
191 } else { 206 } else {
192 json_string_->append("}"); 207 json_string_->append("}");
193 } 208 }
194 break; 209 break;
(...skipping 18 matching lines...) Expand all
213 JsonDoubleQuote(UTF8ToUTF16(str), true, json_string_); 228 JsonDoubleQuote(UTF8ToUTF16(str), true, json_string_);
214 } 229 }
215 230
216 void JSONWriter::IndentLine(int depth) { 231 void JSONWriter::IndentLine(int depth) {
217 // It may be faster to keep an indent string so we don't have to keep 232 // It may be faster to keep an indent string so we don't have to keep
218 // reallocating. 233 // reallocating.
219 json_string_->append(std::string(depth * 3, ' ')); 234 json_string_->append(std::string(depth * 3, ' '));
220 } 235 }
221 236
222 } // namespace base 237 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_writer.h ('k') | base/json/json_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698