2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/JSONValues.h"
34 #include "platform/Decimal.h"
35 #include "wtf/MathExtras.h"
36 #include "wtf/text/StringBuilder.h"
42 const char* const nullString = "null";
43 const char* const trueString = "true";
44 const char* const falseString = "false";
46 inline bool escapeChar(UChar c, StringBuilder* dst)
49 case '\b': dst->appendLiteral("\\b"); break;
50 case '\f': dst->appendLiteral("\\f"); break;
51 case '\n': dst->appendLiteral("\\n"); break;
52 case '\r': dst->appendLiteral("\\r"); break;
53 case '\t': dst->appendLiteral("\\t"); break;
54 case '\\': dst->appendLiteral("\\\\"); break;
55 case '"': dst->appendLiteral("\\\""); break;
62 inline void doubleQuoteString(const String& str, StringBuilder* dst)
65 for (unsigned i = 0; i < str.length(); ++i) {
67 if (!escapeChar(c, dst)) {
68 if (c < 32 || c > 126 || c == '<' || c == '>') {
69 // 1. Escaping <, > to prevent script execution.
70 // 2. Technically, we could also pass through c > 126 as UTF8, but this
71 // is also optional. It would also be a pain to implement here.
72 unsigned symbol = static_cast<unsigned>(c);
73 String symbolCode = String::format("\\u%04X", symbol);
74 dst->append(symbolCode);
83 void writeIndent(int depth, StringBuilder* output)
85 for (int i = 0; i < depth; ++i)
86 output->appendLiteral(" ");
89 } // anonymous namespace
91 bool JSONValue::asBoolean(bool*) const
96 bool JSONValue::asNumber(double*) const
101 bool JSONValue::asNumber(long*) const
106 bool JSONValue::asNumber(int*) const
111 bool JSONValue::asNumber(unsigned long*) const
116 bool JSONValue::asNumber(unsigned*) const
121 bool JSONValue::asString(String*) const
126 bool JSONValue::asValue(RefPtr<JSONValue>* output)
132 bool JSONValue::asObject(RefPtr<JSONObject>*)
137 bool JSONValue::asArray(RefPtr<JSONArray>*)
142 PassRefPtr<JSONObject> JSONValue::asObject()
147 PassRefPtr<JSONArray> JSONValue::asArray()
152 String JSONValue::toJSONString() const
154 StringBuilder result;
155 result.reserveCapacity(512);
157 return result.toString();
160 String JSONValue::toPrettyJSONString() const
162 StringBuilder result;
163 result.reserveCapacity(512);
164 prettyWriteJSON(&result);
165 return result.toString();
168 void JSONValue::writeJSON(StringBuilder* output) const
170 ASSERT(m_type == TypeNull);
171 output->append(nullString, 4);
174 void JSONValue::prettyWriteJSON(StringBuilder* output) const
176 prettyWriteJSONInternal(output, 0);
177 output->append('\n');
180 void JSONValue::prettyWriteJSONInternal(StringBuilder* output, int depth) const
185 bool JSONBasicValue::asBoolean(bool* output) const
187 if (type() != TypeBoolean)
189 *output = m_boolValue;
193 bool JSONBasicValue::asNumber(double* output) const
195 if (type() != TypeNumber)
197 *output = m_doubleValue;
201 bool JSONBasicValue::asNumber(long* output) const
203 if (type() != TypeNumber)
205 *output = static_cast<long>(m_doubleValue);
209 bool JSONBasicValue::asNumber(int* output) const
211 if (type() != TypeNumber)
213 *output = static_cast<int>(m_doubleValue);
217 bool JSONBasicValue::asNumber(unsigned long* output) const
219 if (type() != TypeNumber)
221 *output = static_cast<unsigned long>(m_doubleValue);
225 bool JSONBasicValue::asNumber(unsigned* output) const
227 if (type() != TypeNumber)
229 *output = static_cast<unsigned>(m_doubleValue);
233 void JSONBasicValue::writeJSON(StringBuilder* output) const
235 ASSERT(type() == TypeBoolean || type() == TypeNumber);
236 if (type() == TypeBoolean) {
238 output->append(trueString, 4);
240 output->append(falseString, 5);
241 } else if (type() == TypeNumber) {
242 if (!std::isfinite(m_doubleValue)) {
243 output->append(nullString, 4);
246 output->append(Decimal::fromDouble(m_doubleValue).toString());
250 bool JSONString::asString(String* output) const
252 *output = m_stringValue;
256 void JSONString::writeJSON(StringBuilder* output) const
258 ASSERT(type() == TypeString);
259 doubleQuoteString(m_stringValue, output);
262 JSONObjectBase::~JSONObjectBase()
266 bool JSONObjectBase::asObject(RefPtr<JSONObject>* output)
268 COMPILE_ASSERT(sizeof(JSONObject) == sizeof(JSONObjectBase), cannot_cast);
269 *output = static_cast<JSONObject*>(this);
273 PassRefPtr<JSONObject> JSONObjectBase::asObject()
275 return openAccessors();
278 void JSONObjectBase::setBoolean(const String& name, bool value)
280 setValue(name, JSONBasicValue::create(value));
283 void JSONObjectBase::setNumber(const String& name, double value)
285 setValue(name, JSONBasicValue::create(value));
288 void JSONObjectBase::setString(const String& name, const String& value)
290 setValue(name, JSONString::create(value));
293 void JSONObjectBase::setValue(const String& name, PassRefPtr<JSONValue> value)
296 if (m_data.set(name, value).isNewEntry)
297 m_order.append(name);
300 void JSONObjectBase::setObject(const String& name, PassRefPtr<JSONObject> value)
303 if (m_data.set(name, value).isNewEntry)
304 m_order.append(name);
307 void JSONObjectBase::setArray(const String& name, PassRefPtr<JSONArray> value)
310 if (m_data.set(name, value).isNewEntry)
311 m_order.append(name);
314 JSONObject* JSONObjectBase::openAccessors()
316 COMPILE_ASSERT(sizeof(JSONObject) == sizeof(JSONObjectBase), cannot_cast);
317 return static_cast<JSONObject*>(this);
320 JSONObjectBase::iterator JSONObjectBase::find(const String& name)
322 return m_data.find(name);
325 JSONObjectBase::const_iterator JSONObjectBase::find(const String& name) const
327 return m_data.find(name);
330 bool JSONObjectBase::getBoolean(const String& name, bool* output) const
332 RefPtr<JSONValue> value = get(name);
335 return value->asBoolean(output);
338 bool JSONObjectBase::getString(const String& name, String* output) const
340 RefPtr<JSONValue> value = get(name);
343 return value->asString(output);
346 PassRefPtr<JSONObject> JSONObjectBase::getObject(const String& name) const
348 RefPtr<JSONValue> value = get(name);
351 return value->asObject();
354 PassRefPtr<JSONArray> JSONObjectBase::getArray(const String& name) const
356 RefPtr<JSONValue> value = get(name);
359 return value->asArray();
362 PassRefPtr<JSONValue> JSONObjectBase::get(const String& name) const
364 Dictionary::const_iterator it = m_data.find(name);
365 if (it == m_data.end())
370 void JSONObjectBase::remove(const String& name)
373 for (size_t i = 0; i < m_order.size(); ++i) {
374 if (m_order[i] == name) {
381 void JSONObjectBase::writeJSON(StringBuilder* output) const
384 for (size_t i = 0; i < m_order.size(); ++i) {
385 Dictionary::const_iterator it = m_data.find(m_order[i]);
386 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end());
389 doubleQuoteString(it->key, output);
391 it->value->writeJSON(output);
396 void JSONObjectBase::prettyWriteJSONInternal(StringBuilder* output, int depth) const
398 output->appendLiteral("{\n");
399 for (size_t i = 0; i < m_order.size(); ++i) {
400 Dictionary::const_iterator it = m_data.find(m_order[i]);
401 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end());
403 output->appendLiteral(",\n");
404 writeIndent(depth + 1, output);
405 doubleQuoteString(it->key, output);
406 output->appendLiteral(": ");
407 it->value->prettyWriteJSONInternal(output, depth + 1);
409 output->append('\n');
410 writeIndent(depth, output);
414 JSONObjectBase::JSONObjectBase()
415 : JSONValue(TypeObject)
421 JSONArrayBase::~JSONArrayBase()
425 bool JSONArrayBase::asArray(RefPtr<JSONArray>* output)
427 COMPILE_ASSERT(sizeof(JSONArrayBase) == sizeof(JSONArray), cannot_cast);
428 *output = static_cast<JSONArray*>(this);
432 PassRefPtr<JSONArray> JSONArrayBase::asArray()
434 COMPILE_ASSERT(sizeof(JSONArrayBase) == sizeof(JSONArray), cannot_cast);
435 return static_cast<JSONArray*>(this);
438 void JSONArrayBase::writeJSON(StringBuilder* output) const
441 for (Vector<RefPtr<JSONValue> >::const_iterator it = m_data.begin(); it != m_data.end(); ++it) {
442 if (it != m_data.begin())
444 (*it)->writeJSON(output);
449 void JSONArrayBase::prettyWriteJSONInternal(StringBuilder* output, int depth) const
452 bool lastIsArrayOrObject = false;
453 for (Vector<RefPtr<JSONValue> >::const_iterator it = m_data.begin(); it != m_data.end(); ++it) {
454 bool isArrayOrObject = (*it)->type() == JSONValue::TypeObject || (*it)->type() == JSONValue::TypeArray;
455 if (it == m_data.begin()) {
456 if (isArrayOrObject) {
457 output->append('\n');
458 writeIndent(depth + 1, output);
462 if (lastIsArrayOrObject) {
463 output->append('\n');
464 writeIndent(depth + 1, output);
469 (*it)->prettyWriteJSONInternal(output, depth + 1);
470 lastIsArrayOrObject = isArrayOrObject;
472 if (lastIsArrayOrObject) {
473 output->append('\n');
474 writeIndent(depth, output);
479 JSONArrayBase::JSONArrayBase()
480 : JSONValue(TypeArray)
485 void JSONArrayBase::pushBoolean(bool value)
487 m_data.append(JSONBasicValue::create(value));
490 void JSONArrayBase::pushInt(int value)
492 m_data.append(JSONBasicValue::create(value));
495 void JSONArrayBase::pushNumber(double value)
497 m_data.append(JSONBasicValue::create(value));
500 void JSONArrayBase::pushString(const String& value)
502 m_data.append(JSONString::create(value));
505 void JSONArrayBase::pushValue(PassRefPtr<JSONValue> value)
508 m_data.append(value);
511 void JSONArrayBase::pushObject(PassRefPtr<JSONObject> value)
514 m_data.append(value);
517 void JSONArrayBase::pushArray(PassRefPtr<JSONArray> value)
520 m_data.append(value);
523 PassRefPtr<JSONValue> JSONArrayBase::get(size_t index)
525 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size());
526 return m_data[index];