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 <math.h>
34
35 #include <google/protobuf/stubs/casts.h>
36 #include <google/protobuf/stubs/logging.h>
37 #include <google/protobuf/stubs/common.h>
38 #include <google/protobuf/util/internal/utility.h>
39
40 #include <google/protobuf/util/internal/json_escaping.h>
41 #include <google/protobuf/stubs/strutil.h>
42 #include <google/protobuf/stubs/mathlimits.h>
43
44 namespace google {
45 namespace protobuf {
46 namespace util {
47 namespace converter {
48
49 using strings::ArrayByteSource;
50 ;
51
~JsonObjectWriter()52 JsonObjectWriter::~JsonObjectWriter() {
53 if (element_ && !element_->is_root()) {
54 GOOGLE_LOG(WARNING) << "JsonObjectWriter was not fully closed.";
55 }
56 }
57
StartObject(StringPiece name)58 JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) {
59 WritePrefix(name);
60 WriteChar('{');
61 PushObject();
62 return this;
63 }
64
EndObject()65 JsonObjectWriter* JsonObjectWriter::EndObject() {
66 Pop();
67 WriteChar('}');
68 if (element() && element()->is_root()) NewLine();
69 return this;
70 }
71
StartList(StringPiece name)72 JsonObjectWriter* JsonObjectWriter::StartList(StringPiece name) {
73 WritePrefix(name);
74 WriteChar('[');
75 PushArray();
76 return this;
77 }
78
EndList()79 JsonObjectWriter* JsonObjectWriter::EndList() {
80 Pop();
81 WriteChar(']');
82 if (element()->is_root()) NewLine();
83 return this;
84 }
85
RenderBool(StringPiece name,bool value)86 JsonObjectWriter* JsonObjectWriter::RenderBool(StringPiece name,
87 bool value) {
88 return RenderSimple(name, value ? "true" : "false");
89 }
90
RenderInt32(StringPiece name,int32 value)91 JsonObjectWriter* JsonObjectWriter::RenderInt32(StringPiece name,
92 int32 value) {
93 return RenderSimple(name, StrCat(value));
94 }
95
RenderUint32(StringPiece name,uint32 value)96 JsonObjectWriter* JsonObjectWriter::RenderUint32(StringPiece name,
97 uint32 value) {
98 return RenderSimple(name, StrCat(value));
99 }
100
RenderInt64(StringPiece name,int64 value)101 JsonObjectWriter* JsonObjectWriter::RenderInt64(StringPiece name,
102 int64 value) {
103 WritePrefix(name);
104 WriteChar('"');
105 stream_->WriteString(StrCat(value));
106 WriteChar('"');
107 return this;
108 }
109
RenderUint64(StringPiece name,uint64 value)110 JsonObjectWriter* JsonObjectWriter::RenderUint64(StringPiece name,
111 uint64 value) {
112 WritePrefix(name);
113 WriteChar('"');
114 stream_->WriteString(StrCat(value));
115 WriteChar('"');
116 return this;
117 }
118
RenderDouble(StringPiece name,double value)119 JsonObjectWriter* JsonObjectWriter::RenderDouble(StringPiece name,
120 double value) {
121 if (MathLimits<double>::IsFinite(value)) {
122 return RenderSimple(name, SimpleDtoa(value));
123 }
124
125 // Render quoted with NaN/Infinity-aware DoubleAsString.
126 return RenderString(name, DoubleAsString(value));
127 }
128
RenderFloat(StringPiece name,float value)129 JsonObjectWriter* JsonObjectWriter::RenderFloat(StringPiece name,
130 float value) {
131 if (MathLimits<float>::IsFinite(value)) {
132 return RenderSimple(name, SimpleFtoa(value));
133 }
134
135 // Render quoted with NaN/Infinity-aware FloatAsString.
136 return RenderString(name, FloatAsString(value));
137 }
138
RenderString(StringPiece name,StringPiece value)139 JsonObjectWriter* JsonObjectWriter::RenderString(StringPiece name,
140 StringPiece value) {
141 WritePrefix(name);
142 WriteChar('"');
143 ArrayByteSource source(value);
144 JsonEscaping::Escape(&source, &sink_);
145 WriteChar('"');
146 return this;
147 }
148
RenderBytes(StringPiece name,StringPiece value)149 JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name,
150 StringPiece value) {
151 WritePrefix(name);
152 std::string base64;
153
154 if (use_websafe_base64_for_bytes_)
155 WebSafeBase64EscapeWithPadding(value.ToString(), &base64);
156 else
157 Base64Escape(value, &base64);
158
159 WriteChar('"');
160 // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes
161 // directly to the stream, rather than first putting them
162 // into a string and then writing them to the stream.
163 stream_->WriteRaw(base64.data(), base64.size());
164 WriteChar('"');
165 return this;
166 }
167
RenderNull(StringPiece name)168 JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) {
169 return RenderSimple(name, "null");
170 }
171
RenderNullAsEmpty(StringPiece name)172 JsonObjectWriter* JsonObjectWriter::RenderNullAsEmpty(StringPiece name) {
173 return RenderSimple(name, "");
174 }
175
WritePrefix(StringPiece name)176 void JsonObjectWriter::WritePrefix(StringPiece name) {
177 bool not_first = !element()->is_first();
178 if (not_first) WriteChar(',');
179 if (not_first || !element()->is_root()) NewLine();
180 if (!name.empty() || element()->is_json_object()) {
181 WriteChar('"');
182 if (!name.empty()) {
183 ArrayByteSource source(name);
184 JsonEscaping::Escape(&source, &sink_);
185 }
186 stream_->WriteString("\":");
187 if (!indent_string_.empty()) WriteChar(' ');
188 }
189 }
190
191 } // namespace converter
192 } // namespace util
193 } // namespace protobuf
194 } // namespace google
195