1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <google/protobuf/util/internal/json_objectwriter.h>
32 
33 #include <cmath>
34 #include <limits>
35 
36 #include <google/protobuf/stubs/casts.h>
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/util/internal/utility.h>
40 #include <google/protobuf/util/internal/json_escaping.h>
41 #include <google/protobuf/stubs/strutil.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace util {
46 namespace converter {
47 
48 
~JsonObjectWriter()49 JsonObjectWriter::~JsonObjectWriter() {
50   if (element_ && !element_->is_root()) {
51     GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed.";
52   }
53 }
54 
StartObject(StringPiece name)55 JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) {
56   WritePrefix(name);
57   WriteChar('{');
58   PushObject();
59   return this;
60 }
61 
EndObject()62 JsonObjectWriter* JsonObjectWriter::EndObject() {
63   Pop();
64   WriteChar('}');
65   if (element() && element()->is_root()) NewLine();
66   return this;
67 }
68 
StartList(StringPiece name)69 JsonObjectWriter* JsonObjectWriter::StartList(StringPiece name) {
70   WritePrefix(name);
71   WriteChar('[');
72   PushArray();
73   return this;
74 }
75 
EndList()76 JsonObjectWriter* JsonObjectWriter::EndList() {
77   Pop();
78   WriteChar(']');
79   if (element()->is_root()) NewLine();
80   return this;
81 }
82 
RenderBool(StringPiece name,bool value)83 JsonObjectWriter* JsonObjectWriter::RenderBool(StringPiece name,
84                                                bool value) {
85   return RenderSimple(name, value ? "true" : "false");
86 }
87 
RenderInt32(StringPiece name,int32 value)88 JsonObjectWriter* JsonObjectWriter::RenderInt32(StringPiece name,
89                                                 int32 value) {
90   return RenderSimple(name, StrCat(value));
91 }
92 
RenderUint32(StringPiece name,uint32 value)93 JsonObjectWriter* JsonObjectWriter::RenderUint32(StringPiece name,
94                                                  uint32 value) {
95   return RenderSimple(name, StrCat(value));
96 }
97 
RenderInt64(StringPiece name,int64 value)98 JsonObjectWriter* JsonObjectWriter::RenderInt64(StringPiece name,
99                                                 int64 value) {
100   WritePrefix(name);
101   WriteChar('"');
102   WriteRawString(StrCat(value));
103   WriteChar('"');
104   return this;
105 }
106 
RenderUint64(StringPiece name,uint64 value)107 JsonObjectWriter* JsonObjectWriter::RenderUint64(StringPiece name,
108                                                  uint64 value) {
109   WritePrefix(name);
110   WriteChar('"');
111   WriteRawString(StrCat(value));
112   WriteChar('"');
113   return this;
114 }
115 
RenderDouble(StringPiece name,double value)116 JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name,
117                                                  double value) {
118   if (std::isfinite(value)) {
119     return RenderSimple(name, SimpleDtoa(value));
120   }
121 
122   // Render quoted with NaN/Infinity-aware DoubleAsString.
123   return RenderString(name, DoubleAsString(value));
124 }
125 
RenderFloat(StringPiece name,float value)126 JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name,
127                                                 float value) {
128   if (std::isfinite(value)) {
129     return RenderSimple(name, SimpleFtoa(value));
130   }
131 
132   // Render quoted with NaN/Infinity-aware FloatAsString.
133   return RenderString(name, FloatAsString(value));
134 }
135 
RenderString(StringPiece name,StringPiece value)136 JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name,
137                                                  StringPiece value) {
138   WritePrefix(name);
139   WriteChar('"');
140   JsonEscaping::Escape(value, &sink_);
141   WriteChar('"');
142   return this;
143 }
144 
RenderBytes(StringPiece name,StringPiece value)145 JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name,
146                                                 StringPiece value) {
147   WritePrefix(name);
148   std::string base64;
149 
150   if (use_websafe_base64_for_bytes_)
151     WebSafeBase64EscapeWithPadding(value.ToString(), &base64);
152   else
153     Base64Escape(value, &base64);
154 
155   WriteChar('"');
156   // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes
157   //              directly to the stream, rather than first putting them
158   //              into a string and then writing them to the stream.
159   stream_->WriteRaw(base64.data(), base64.size());
160   WriteChar('"');
161   return this;
162 }
163 
RenderNull(StringPiece name)164 JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) {
165   return RenderSimple(name, "null");
166 }
167 
RenderNullAsEmpty(StringPiece name)168 JsonObjectWriter* JsonObjectWriter::RenderNullAsEmpty(StringPiece name) {
169   return RenderSimple(name, "");
170 }
171 
WritePrefix(StringPiece name)172 void JsonObjectWriter::WritePrefix(StringPiece name) {
173   bool not_first = !element()->is_first();
174   if (not_first) WriteChar(',');
175   if (not_first || !element()->is_root()) NewLine();
176   if (!name.empty() || element()->is_json_object()) {
177     WriteChar('"');
178     if (!name.empty()) {
179       JsonEscaping::Escape(name, &sink_);
180     }
181     WriteRawString("\":");
182     if (!indent_string_.empty()) WriteChar(' ');
183   }
184 }
185 
186 }  // namespace converter
187 }  // namespace util
188 }  // namespace protobuf
189 }  // namespace google
190