OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 WriteChar('"'); | 140 WriteChar('"'); |
141 return this; | 141 return this; |
142 } | 142 } |
143 | 143 |
144 JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name, | 144 JsonObjectWriter* JsonObjectWriter::RenderBytes(StringPiece name, |
145 StringPiece value) { | 145 StringPiece value) { |
146 WritePrefix(name); | 146 WritePrefix(name); |
147 string base64; | 147 string base64; |
148 | 148 |
149 if (use_websafe_base64_for_bytes_) | 149 if (use_websafe_base64_for_bytes_) |
150 WebSafeBase64EscapeWithPadding(value.ToString(), &base64); | 150 WebSafeBase64Escape(value.ToString(), &base64); |
151 else | 151 else |
152 Base64Escape(value, &base64); | 152 Base64Escape(value, &base64); |
153 | 153 |
154 WriteChar('"'); | 154 WriteChar('"'); |
155 // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes | 155 // TODO(wpoon): Consider a ByteSink solution that writes the base64 bytes |
156 // directly to the stream, rather than first putting them | 156 // directly to the stream, rather than first putting them |
157 // into a string and then writing them to the stream. | 157 // into a string and then writing them to the stream. |
158 stream_->WriteRaw(base64.data(), base64.size()); | 158 stream_->WriteRaw(base64.data(), base64.size()); |
159 WriteChar('"'); | 159 WriteChar('"'); |
160 return this; | 160 return this; |
161 } | 161 } |
162 | 162 |
163 JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) { | 163 JsonObjectWriter* JsonObjectWriter::RenderNull(StringPiece name) { |
164 return RenderSimple(name, "null"); | 164 return RenderSimple(name, "null"); |
165 } | 165 } |
166 | 166 |
167 JsonObjectWriter* JsonObjectWriter::RenderNullAsEmpty(StringPiece name) { | |
168 return RenderSimple(name, ""); | |
169 } | |
170 | |
171 void JsonObjectWriter::WritePrefix(StringPiece name) { | 167 void JsonObjectWriter::WritePrefix(StringPiece name) { |
172 bool not_first = !element()->is_first(); | 168 bool not_first = !element()->is_first(); |
173 if (not_first) WriteChar(','); | 169 if (not_first) WriteChar(','); |
174 if (not_first || !element()->is_root()) NewLine(); | 170 if (not_first || !element()->is_root()) NewLine(); |
175 bool empty_key_ok = GetAndResetEmptyKeyOk(); | 171 if (!name.empty()) { |
176 if (!name.empty() || empty_key_ok) { | |
177 WriteChar('"'); | 172 WriteChar('"'); |
178 if (!name.empty()) { | 173 ArrayByteSource source(name); |
179 ArrayByteSource source(name); | 174 JsonEscaping::Escape(&source, &sink_); |
180 JsonEscaping::Escape(&source, &sink_); | |
181 } | |
182 stream_->WriteString("\":"); | 175 stream_->WriteString("\":"); |
183 if (!indent_string_.empty()) WriteChar(' '); | 176 if (!indent_string_.empty()) WriteChar(' '); |
184 } | 177 } |
185 } | 178 } |
186 | 179 |
187 bool JsonObjectWriter::GetAndResetEmptyKeyOk() { | |
188 bool retval = empty_name_ok_for_next_key_; | |
189 empty_name_ok_for_next_key_ = false; | |
190 return retval; | |
191 } | |
192 | |
193 } // namespace converter | 180 } // namespace converter |
194 } // namespace util | 181 } // namespace util |
195 } // namespace protobuf | 182 } // namespace protobuf |
196 } // namespace google | 183 } // namespace google |
OLD | NEW |