1// Copyright (c) 2012 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.
4
5#include "base/json/json_writer.h"
6
7#include <stdint.h>
8
9#include <cmath>
10#include <limits>
11
12#include "base/json/string_escape.h"
13#include "base/logging.h"
14#include "base/strings/string_number_conversions.h"
15#include "base/strings/utf_string_conversions.h"
16#include "base/values.h"
17#include "build/build_config.h"
18
19namespace base {
20
21#if defined(OS_WIN)
22const char kPrettyPrintLineEnding[] = "\r\n";
23#else
24const char kPrettyPrintLineEnding[] = "\n";
25#endif
26
27// static
28bool JSONWriter::Write(const Value& node, std::string* json) {
29  return WriteWithOptions(node, 0, json);
30}
31
32// static
33bool JSONWriter::WriteWithOptions(const Value& node,
34                                  int options,
35                                  std::string* json) {
36  json->clear();
37  // Is there a better way to estimate the size of the output?
38  json->reserve(1024);
39
40  JSONWriter writer(options, json);
41  bool result = writer.BuildJSONString(node, 0U);
42
43  if (options & OPTIONS_PRETTY_PRINT)
44    json->append(kPrettyPrintLineEnding);
45
46  return result;
47}
48
49JSONWriter::JSONWriter(int options, std::string* json)
50    : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0),
51      omit_double_type_preservation_(
52          (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0),
53      pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0),
54      json_string_(json) {
55  DCHECK(json);
56}
57
58bool JSONWriter::BuildJSONString(const Value& node, size_t depth) {
59  switch (node.GetType()) {
60    case Value::Type::NONE: {
61      json_string_->append("null");
62      return true;
63    }
64
65    case Value::Type::BOOLEAN: {
66      bool value;
67      bool result = node.GetAsBoolean(&value);
68      DCHECK(result);
69      json_string_->append(value ? "true" : "false");
70      return result;
71    }
72
73    case Value::Type::INTEGER: {
74      int value;
75      bool result = node.GetAsInteger(&value);
76      DCHECK(result);
77      json_string_->append(IntToString(value));
78      return result;
79    }
80
81    case Value::Type::DOUBLE: {
82      double value;
83      bool result = node.GetAsDouble(&value);
84      DCHECK(result);
85      if (omit_double_type_preservation_ &&
86          value <= std::numeric_limits<int64_t>::max() &&
87          value >= std::numeric_limits<int64_t>::min() &&
88          std::floor(value) == value) {
89        json_string_->append(Int64ToString(static_cast<int64_t>(value)));
90        return result;
91      }
92      std::string real = DoubleToString(value);
93      // Ensure that the number has a .0 if there's no decimal or 'e'.  This
94      // makes sure that when we read the JSON back, it's interpreted as a
95      // real rather than an int.
96      if (real.find('.') == std::string::npos &&
97          real.find('e') == std::string::npos &&
98          real.find('E') == std::string::npos) {
99        real.append(".0");
100      }
101      // The JSON spec requires that non-integer values in the range (-1,1)
102      // have a zero before the decimal point - ".52" is not valid, "0.52" is.
103      if (real[0] == '.') {
104        real.insert(static_cast<size_t>(0), static_cast<size_t>(1), '0');
105      } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
106        // "-.1" bad "-0.1" good
107        real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0');
108      }
109      json_string_->append(real);
110      return result;
111    }
112
113    case Value::Type::STRING: {
114      std::string value;
115      bool result = node.GetAsString(&value);
116      DCHECK(result);
117      EscapeJSONString(value, true, json_string_);
118      return result;
119    }
120
121    case Value::Type::LIST: {
122      json_string_->push_back('[');
123      if (pretty_print_)
124        json_string_->push_back(' ');
125
126      const ListValue* list = NULL;
127      bool first_value_has_been_output = false;
128      bool result = node.GetAsList(&list);
129      DCHECK(result);
130      for (const auto& value : *list) {
131        if (omit_binary_values_ && value->GetType() == Value::Type::BINARY)
132          continue;
133
134        if (first_value_has_been_output) {
135          json_string_->push_back(',');
136          if (pretty_print_)
137            json_string_->push_back(' ');
138        }
139
140        if (!BuildJSONString(*value, depth))
141          result = false;
142
143        first_value_has_been_output = true;
144      }
145
146      if (pretty_print_)
147        json_string_->push_back(' ');
148      json_string_->push_back(']');
149      return result;
150    }
151
152    case Value::Type::DICTIONARY: {
153      json_string_->push_back('{');
154      if (pretty_print_)
155        json_string_->append(kPrettyPrintLineEnding);
156
157      const DictionaryValue* dict = NULL;
158      bool first_value_has_been_output = false;
159      bool result = node.GetAsDictionary(&dict);
160      DCHECK(result);
161      for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd();
162           itr.Advance()) {
163        if (omit_binary_values_ &&
164            itr.value().GetType() == Value::Type::BINARY) {
165          continue;
166        }
167
168        if (first_value_has_been_output) {
169          json_string_->push_back(',');
170          if (pretty_print_)
171            json_string_->append(kPrettyPrintLineEnding);
172        }
173
174        if (pretty_print_)
175          IndentLine(depth + 1U);
176
177        EscapeJSONString(itr.key(), true, json_string_);
178        json_string_->push_back(':');
179        if (pretty_print_)
180          json_string_->push_back(' ');
181
182        if (!BuildJSONString(itr.value(), depth + 1U))
183          result = false;
184
185        first_value_has_been_output = true;
186      }
187
188      if (pretty_print_) {
189        json_string_->append(kPrettyPrintLineEnding);
190        IndentLine(depth);
191      }
192
193      json_string_->push_back('}');
194      return result;
195    }
196
197    case Value::Type::BINARY:
198      // Successful only if we're allowed to omit it.
199      DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value.";
200      return omit_binary_values_;
201  }
202  NOTREACHED();
203  return false;
204}
205
206void JSONWriter::IndentLine(size_t depth) {
207  json_string_->append(depth * 3U, ' ');
208}
209
210}  // namespace base
211