blob: 1a9f1b6db4c9e2e0f7204d8a075efaa325c33b86 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include "base/json_writer.h"
6
7#include "base/logging.h"
8#include "base/string_util.h"
9#include "base/values.h"
10#include "base/string_escape.h"
11
[email protected]b81637c32009-06-26 21:17:2412#if defined(OS_WIN)
13static const char kPrettyPrintLineEnding[] = "\r\n";
14#else
15static const char kPrettyPrintLineEnding[] = "\n";
16#endif
initial.commitd7cae122008-07-26 21:49:3817
18/* static */
[email protected]3388d4c2009-05-15 10:13:2819void JSONWriter::Write(const Value* const node,
20 bool pretty_print,
initial.commitd7cae122008-07-26 21:49:3821 std::string* json) {
[email protected]3388d4c2009-05-15 10:13:2822 WriteWithOptionalEscape(node, pretty_print, true, json);
23}
24
25/* static */
26void JSONWriter::WriteWithOptionalEscape(const Value* const node,
27 bool pretty_print,
28 bool escape,
29 std::string* json) {
initial.commitd7cae122008-07-26 21:49:3830 json->clear();
31 // Is there a better way to estimate the size of the output?
32 json->reserve(1024);
33 JSONWriter writer(pretty_print, json);
[email protected]3388d4c2009-05-15 10:13:2834 writer.BuildJSONString(node, 0, escape);
initial.commitd7cae122008-07-26 21:49:3835 if (pretty_print)
36 json->append(kPrettyPrintLineEnding);
37}
38
39JSONWriter::JSONWriter(bool pretty_print, std::string* json)
[email protected]294300a2008-08-06 15:46:4940 : json_string_(json),
41 pretty_print_(pretty_print) {
initial.commitd7cae122008-07-26 21:49:3842 DCHECK(json);
43}
44
[email protected]3388d4c2009-05-15 10:13:2845void JSONWriter::BuildJSONString(const Value* const node,
46 int depth,
47 bool escape) {
initial.commitd7cae122008-07-26 21:49:3848 switch(node->GetType()) {
49 case Value::TYPE_NULL:
50 json_string_->append("null");
51 break;
52
53 case Value::TYPE_BOOLEAN:
54 {
55 bool value;
56 bool result = node->GetAsBoolean(&value);
57 DCHECK(result);
58 json_string_->append(value ? "true" : "false");
59 break;
60 }
61
62 case Value::TYPE_INTEGER:
63 {
64 int value;
65 bool result = node->GetAsInteger(&value);
66 DCHECK(result);
67 StringAppendF(json_string_, "%d", value);
68 break;
69 }
70
71 case Value::TYPE_REAL:
72 {
73 double value;
74 bool result = node->GetAsReal(&value);
75 DCHECK(result);
[email protected]6dce8ade2008-11-18 00:14:2876 std::string real = DoubleToString(value);
initial.commitd7cae122008-07-26 21:49:3877 // Ensure that the number has a .0 if there's no decimal or 'e'. This
78 // makes sure that when we read the JSON back, it's interpreted as a
79 // real rather than an int.
80 if (real.find('.') == std::string::npos &&
81 real.find('e') == std::string::npos &&
82 real.find('E') == std::string::npos) {
83 real.append(".0");
84 }
[email protected]bcf60bb2009-04-20 19:14:4385 // The JSON spec requires that non-integer values in the range (-1,1)
86 // have a zero before the decimal point - ".52" is not valid, "0.52" is.
87 if (real[0] == '.') {
88 real.insert(0, "0");
89 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
90 // "-.1" bad "-0.1" good
91 real.insert(1, "0");
92 }
initial.commitd7cae122008-07-26 21:49:3893 json_string_->append(real);
94 break;
95 }
96
97 case Value::TYPE_STRING:
98 {
[email protected]d36519b2009-05-20 16:43:4999 std::string value;
100 bool result = node->GetAsString(&value);
101 DCHECK(result);
[email protected]3388d4c2009-05-15 10:13:28102 if (escape) {
[email protected]d36519b2009-05-20 16:43:49103 string_escape::JsonDoubleQuote(UTF8ToUTF16(value),
104 true,
105 json_string_);
[email protected]3388d4c2009-05-15 10:13:28106 } else {
[email protected]d36519b2009-05-20 16:43:49107 string_escape::JsonDoubleQuote(value, true, json_string_);
[email protected]3388d4c2009-05-15 10:13:28108 }
initial.commitd7cae122008-07-26 21:49:38109 break;
110 }
111
112 case Value::TYPE_LIST:
113 {
114 json_string_->append("[");
115 if (pretty_print_)
116 json_string_->append(" ");
117
118 const ListValue* list = static_cast<const ListValue*>(node);
119 for (size_t i = 0; i < list->GetSize(); ++i) {
120 if (i != 0) {
121 json_string_->append(",");
122 if (pretty_print_)
123 json_string_->append(" ");
124 }
125
126 Value* value = NULL;
127 bool result = list->Get(i, &value);
128 DCHECK(result);
[email protected]3388d4c2009-05-15 10:13:28129 BuildJSONString(value, depth, escape);
initial.commitd7cae122008-07-26 21:49:38130 }
131
132 if (pretty_print_)
133 json_string_->append(" ");
134 json_string_->append("]");
135 break;
136 }
137
138 case Value::TYPE_DICTIONARY:
139 {
140 json_string_->append("{");
141 if (pretty_print_)
142 json_string_->append(kPrettyPrintLineEnding);
143
144 const DictionaryValue* dict =
145 static_cast<const DictionaryValue*>(node);
146 for (DictionaryValue::key_iterator key_itr = dict->begin_keys();
147 key_itr != dict->end_keys();
148 ++key_itr) {
149
150 if (key_itr != dict->begin_keys()) {
151 json_string_->append(",");
152 if (pretty_print_)
153 json_string_->append(kPrettyPrintLineEnding);
154 }
155
156 Value* value = NULL;
157 bool result = dict->Get(*key_itr, &value);
158 DCHECK(result);
159
160 if (pretty_print_)
161 IndentLine(depth + 1);
162 AppendQuotedString(*key_itr);
163 if (pretty_print_) {
164 json_string_->append(": ");
165 } else {
166 json_string_->append(":");
167 }
[email protected]3388d4c2009-05-15 10:13:28168 BuildJSONString(value, depth + 1, escape);
initial.commitd7cae122008-07-26 21:49:38169 }
170
171 if (pretty_print_) {
172 json_string_->append(kPrettyPrintLineEnding);
173 IndentLine(depth);
174 json_string_->append("}");
175 } else {
176 json_string_->append("}");
177 }
178 break;
179 }
180
181 default:
182 // TODO(jhughes): handle TYPE_BINARY
183 NOTREACHED() << "unknown json type";
184 }
185}
186
[email protected]8e50b602009-03-03 22:59:43187void JSONWriter::AppendQuotedString(const std::wstring& str) {
[email protected]d36519b2009-05-20 16:43:49188 string_escape::JsonDoubleQuote(WideToUTF16Hack(str),
189 true,
190 json_string_);
initial.commitd7cae122008-07-26 21:49:38191}
192
193void JSONWriter::IndentLine(int depth) {
194 // It may be faster to keep an indent string so we don't have to keep
195 // reallocating.
196 json_string_->append(std::string(depth * 3, ' '));
197}