OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_writer.h" | 5 #include "base/json_writer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "base/string_escape.h" | 10 #include "base/string_escape.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 DCHECK(result); | 51 DCHECK(result); |
52 StringAppendF(json_string_, "%d", value); | 52 StringAppendF(json_string_, "%d", value); |
53 break; | 53 break; |
54 } | 54 } |
55 | 55 |
56 case Value::TYPE_REAL: | 56 case Value::TYPE_REAL: |
57 { | 57 { |
58 double value; | 58 double value; |
59 bool result = node->GetAsReal(&value); | 59 bool result = node->GetAsReal(&value); |
60 DCHECK(result); | 60 DCHECK(result); |
61 std::string real = base::DoubleToString(value, base::LOCALE_INDEPENDENT)
; | 61 std::string real = DoubleToString(value); |
62 // Ensure that the number has a .0 if there's no decimal or 'e'. This | 62 // Ensure that the number has a .0 if there's no decimal or 'e'. This |
63 // makes sure that when we read the JSON back, it's interpreted as a | 63 // makes sure that when we read the JSON back, it's interpreted as a |
64 // real rather than an int. | 64 // real rather than an int. |
65 if (real.find('.') == std::string::npos && | 65 if (real.find('.') == std::string::npos && |
66 real.find('e') == std::string::npos && | 66 real.find('e') == std::string::npos && |
67 real.find('E') == std::string::npos) { | 67 real.find('E') == std::string::npos) { |
68 real.append(".0"); | 68 real.append(".0"); |
69 } | 69 } |
70 json_string_->append(real); | 70 json_string_->append(real); |
71 break; | 71 break; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 void JSONWriter::AppendQuotedString(const std::wstring& str) { | 158 void JSONWriter::AppendQuotedString(const std::wstring& str) { |
159 string_escape::JavascriptDoubleQuote(str, true, json_string_); | 159 string_escape::JavascriptDoubleQuote(str, true, json_string_); |
160 } | 160 } |
161 | 161 |
162 void JSONWriter::IndentLine(int depth) { | 162 void JSONWriter::IndentLine(int depth) { |
163 // It may be faster to keep an indent string so we don't have to keep | 163 // It may be faster to keep an indent string so we don't have to keep |
164 // reallocating. | 164 // reallocating. |
165 json_string_->append(std::string(depth * 3, ' ')); | 165 json_string_->append(std::string(depth * 3, ' ')); |
166 } | 166 } |
167 | 167 |
OLD | NEW |