| 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 WebSafeBase64Escape(value.ToString(), &base64); | 150 WebSafeBase64EscapeWithPadding(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 |
| 167 void JsonObjectWriter::WritePrefix(StringPiece name) { | 171 void JsonObjectWriter::WritePrefix(StringPiece name) { |
| 168 bool not_first = !element()->is_first(); | 172 bool not_first = !element()->is_first(); |
| 169 if (not_first) WriteChar(','); | 173 if (not_first) WriteChar(','); |
| 170 if (not_first || !element()->is_root()) NewLine(); | 174 if (not_first || !element()->is_root()) NewLine(); |
| 171 if (!name.empty()) { | 175 bool empty_key_ok = GetAndResetEmptyKeyOk(); |
| 176 if (!name.empty() || empty_key_ok) { |
| 172 WriteChar('"'); | 177 WriteChar('"'); |
| 173 ArrayByteSource source(name); | 178 if (!name.empty()) { |
| 174 JsonEscaping::Escape(&source, &sink_); | 179 ArrayByteSource source(name); |
| 180 JsonEscaping::Escape(&source, &sink_); |
| 181 } |
| 175 stream_->WriteString("\":"); | 182 stream_->WriteString("\":"); |
| 176 if (!indent_string_.empty()) WriteChar(' '); | 183 if (!indent_string_.empty()) WriteChar(' '); |
| 177 } | 184 } |
| 178 } | 185 } |
| 179 | 186 |
| 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 |
| 180 } // namespace converter | 193 } // namespace converter |
| 181 } // namespace util | 194 } // namespace util |
| 182 } // namespace protobuf | 195 } // namespace protobuf |
| 183 } // namespace google | 196 } // namespace google |
| OLD | NEW |