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

Side by Side Diff: base/json_writer.cc

Issue 113606: Add JSON-specific escaping, which has different rules from JS escaping. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 7 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 | « no previous file | base/string_escape.h » ('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) 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') { 85 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
86 // "-.1" bad "-0.1" good 86 // "-.1" bad "-0.1" good
87 real.insert(1, "0"); 87 real.insert(1, "0");
88 } 88 }
89 json_string_->append(real); 89 json_string_->append(real);
90 break; 90 break;
91 } 91 }
92 92
93 case Value::TYPE_STRING: 93 case Value::TYPE_STRING:
94 { 94 {
95 std::string value;
96 bool result = node->GetAsString(&value);
97 DCHECK(result);
95 if (escape) { 98 if (escape) {
96 std::wstring value; 99 string_escape::JsonDoubleQuote(UTF8ToUTF16(value),
97 bool result = node->GetAsString(&value); 100 true,
98 DCHECK(result); 101 json_string_);
99 AppendQuotedString(value);
100 } else { 102 } else {
101 std::string value; 103 string_escape::JsonDoubleQuote(value, true, json_string_);
102 bool result = node->GetAsString(&value);
103 DCHECK(result);
104 string_escape::JavascriptDoubleQuote(value, true, json_string_);
105 } 104 }
106 break; 105 break;
107 } 106 }
108 107
109 case Value::TYPE_LIST: 108 case Value::TYPE_LIST:
110 { 109 {
111 json_string_->append("["); 110 json_string_->append("[");
112 if (pretty_print_) 111 if (pretty_print_)
113 json_string_->append(" "); 112 json_string_->append(" ");
114 113
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 break; 174 break;
176 } 175 }
177 176
178 default: 177 default:
179 // TODO(jhughes): handle TYPE_BINARY 178 // TODO(jhughes): handle TYPE_BINARY
180 NOTREACHED() << "unknown json type"; 179 NOTREACHED() << "unknown json type";
181 } 180 }
182 } 181 }
183 182
184 void JSONWriter::AppendQuotedString(const std::wstring& str) { 183 void JSONWriter::AppendQuotedString(const std::wstring& str) {
185 string_escape::JavascriptDoubleQuote(WideToUTF16Hack(str), 184 string_escape::JsonDoubleQuote(WideToUTF16Hack(str),
186 true, 185 true,
187 json_string_); 186 json_string_);
188 } 187 }
189 188
190 void JSONWriter::IndentLine(int depth) { 189 void JSONWriter::IndentLine(int depth) {
191 // It may be faster to keep an indent string so we don't have to keep 190 // It may be faster to keep an indent string so we don't have to keep
192 // reallocating. 191 // reallocating.
193 json_string_->append(std::string(depth * 3, ' ')); 192 json_string_->append(std::string(depth * 3, ' '));
194 } 193 }
OLDNEW
« no previous file with comments | « no previous file | base/string_escape.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698