| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/json_writer.h" |
| 6 | 6 |
| 7 #include "base/json/string_escape.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "base/string_escape.h" | |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 | 12 |
| 13 namespace base { |
| 14 |
| 13 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 14 static const char kPrettyPrintLineEnding[] = "\r\n"; | 16 static const char kPrettyPrintLineEnding[] = "\r\n"; |
| 15 #else | 17 #else |
| 16 static const char kPrettyPrintLineEnding[] = "\n"; | 18 static const char kPrettyPrintLineEnding[] = "\n"; |
| 17 #endif | 19 #endif |
| 18 | 20 |
| 19 /* static */ | 21 /* static */ |
| 20 void JSONWriter::Write(const Value* const node, | 22 void JSONWriter::Write(const Value* const node, |
| 21 bool pretty_print, | 23 bool pretty_print, |
| 22 std::string* json) { | 24 std::string* json) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 json_string_->append(real); | 96 json_string_->append(real); |
| 95 break; | 97 break; |
| 96 } | 98 } |
| 97 | 99 |
| 98 case Value::TYPE_STRING: | 100 case Value::TYPE_STRING: |
| 99 { | 101 { |
| 100 std::string value; | 102 std::string value; |
| 101 bool result = node->GetAsString(&value); | 103 bool result = node->GetAsString(&value); |
| 102 DCHECK(result); | 104 DCHECK(result); |
| 103 if (escape) { | 105 if (escape) { |
| 104 string_escape::JsonDoubleQuote(UTF8ToUTF16(value), | 106 JsonDoubleQuote(UTF8ToUTF16(value), true, json_string_); |
| 105 true, | |
| 106 json_string_); | |
| 107 } else { | 107 } else { |
| 108 string_escape::JsonDoubleQuote(value, true, json_string_); | 108 JsonDoubleQuote(value, true, json_string_); |
| 109 } | 109 } |
| 110 break; | 110 break; |
| 111 } | 111 } |
| 112 | 112 |
| 113 case Value::TYPE_LIST: | 113 case Value::TYPE_LIST: |
| 114 { | 114 { |
| 115 json_string_->append("["); | 115 json_string_->append("["); |
| 116 if (pretty_print_) | 116 if (pretty_print_) |
| 117 json_string_->append(" "); | 117 json_string_->append(" "); |
| 118 | 118 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 break; | 179 break; |
| 180 } | 180 } |
| 181 | 181 |
| 182 default: | 182 default: |
| 183 // TODO(jhughes): handle TYPE_BINARY | 183 // TODO(jhughes): handle TYPE_BINARY |
| 184 NOTREACHED() << "unknown json type"; | 184 NOTREACHED() << "unknown json type"; |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 void JSONWriter::AppendQuotedString(const std::wstring& str) { | 188 void JSONWriter::AppendQuotedString(const std::wstring& str) { |
| 189 string_escape::JsonDoubleQuote(WideToUTF16Hack(str), | 189 JsonDoubleQuote(WideToUTF16Hack(str), true, json_string_); |
| 190 true, | |
| 191 json_string_); | |
| 192 } | 190 } |
| 193 | 191 |
| 194 void JSONWriter::IndentLine(int depth) { | 192 void JSONWriter::IndentLine(int depth) { |
| 195 // It may be faster to keep an indent string so we don't have to keep | 193 // It may be faster to keep an indent string so we don't have to keep |
| 196 // reallocating. | 194 // reallocating. |
| 197 json_string_->append(std::string(depth * 3, ' ')); | 195 json_string_->append(std::string(depth * 3, ' ')); |
| 198 } | 196 } |
| 197 |
| 198 } // namespace base |
| OLD | NEW |