| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // | 86 // |
| 87 // JsonObjectWriter is thread-unsafe. | 87 // JsonObjectWriter is thread-unsafe. |
| 88 class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { | 88 class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { |
| 89 public: | 89 public: |
| 90 JsonObjectWriter(StringPiece indent_string, | 90 JsonObjectWriter(StringPiece indent_string, |
| 91 google::protobuf::io::CodedOutputStream* out) | 91 google::protobuf::io::CodedOutputStream* out) |
| 92 : element_(new Element(NULL)), | 92 : element_(new Element(NULL)), |
| 93 stream_(out), | 93 stream_(out), |
| 94 sink_(out), | 94 sink_(out), |
| 95 indent_string_(indent_string.ToString()), | 95 indent_string_(indent_string.ToString()), |
| 96 use_websafe_base64_for_bytes_(false) {} | 96 use_websafe_base64_for_bytes_(false), |
| 97 empty_name_ok_for_next_key_(false) {} |
| 97 virtual ~JsonObjectWriter(); | 98 virtual ~JsonObjectWriter(); |
| 98 | 99 |
| 99 // ObjectWriter methods. | 100 // ObjectWriter methods. |
| 100 virtual JsonObjectWriter* StartObject(StringPiece name); | 101 virtual JsonObjectWriter* StartObject(StringPiece name); |
| 101 virtual JsonObjectWriter* EndObject(); | 102 virtual JsonObjectWriter* EndObject(); |
| 102 virtual JsonObjectWriter* StartList(StringPiece name); | 103 virtual JsonObjectWriter* StartList(StringPiece name); |
| 103 virtual JsonObjectWriter* EndList(); | 104 virtual JsonObjectWriter* EndList(); |
| 104 virtual JsonObjectWriter* RenderBool(StringPiece name, bool value); | 105 virtual JsonObjectWriter* RenderBool(StringPiece name, bool value); |
| 105 virtual JsonObjectWriter* RenderInt32(StringPiece name, int32 value); | 106 virtual JsonObjectWriter* RenderInt32(StringPiece name, int32 value); |
| 106 virtual JsonObjectWriter* RenderUint32(StringPiece name, uint32 value); | 107 virtual JsonObjectWriter* RenderUint32(StringPiece name, uint32 value); |
| 107 virtual JsonObjectWriter* RenderInt64(StringPiece name, int64 value); | 108 virtual JsonObjectWriter* RenderInt64(StringPiece name, int64 value); |
| 108 virtual JsonObjectWriter* RenderUint64(StringPiece name, uint64 value); | 109 virtual JsonObjectWriter* RenderUint64(StringPiece name, uint64 value); |
| 109 virtual JsonObjectWriter* RenderDouble(StringPiece name, double value); | 110 virtual JsonObjectWriter* RenderDouble(StringPiece name, double value); |
| 110 virtual JsonObjectWriter* RenderFloat(StringPiece name, float value); | 111 virtual JsonObjectWriter* RenderFloat(StringPiece name, float value); |
| 111 virtual JsonObjectWriter* RenderString(StringPiece name, StringPiece value); | 112 virtual JsonObjectWriter* RenderString(StringPiece name, StringPiece value); |
| 112 virtual JsonObjectWriter* RenderBytes(StringPiece name, StringPiece value); | 113 virtual JsonObjectWriter* RenderBytes(StringPiece name, StringPiece value); |
| 113 virtual JsonObjectWriter* RenderNull(StringPiece name); | 114 virtual JsonObjectWriter* RenderNull(StringPiece name); |
| 115 virtual JsonObjectWriter* RenderNullAsEmpty(StringPiece name); |
| 114 | 116 |
| 115 void set_use_websafe_base64_for_bytes(bool value) { | 117 void set_use_websafe_base64_for_bytes(bool value) { |
| 116 use_websafe_base64_for_bytes_ = value; | 118 use_websafe_base64_for_bytes_ = value; |
| 117 } | 119 } |
| 118 | 120 |
| 121 // Whether empty strings should be rendered for the next JSON key. This |
| 122 // setting is only valid until the next key is rendered, after which it gets |
| 123 // reset to false. |
| 124 virtual void empty_name_ok_for_next_key() { |
| 125 empty_name_ok_for_next_key_ = true; |
| 126 } |
| 127 |
| 119 protected: | 128 protected: |
| 120 class LIBPROTOBUF_EXPORT Element : public BaseElement { | 129 class LIBPROTOBUF_EXPORT Element : public BaseElement { |
| 121 public: | 130 public: |
| 122 explicit Element(Element* parent) : BaseElement(parent), is_first_(true) {} | 131 explicit Element(Element* parent) : BaseElement(parent), is_first_(true) {} |
| 123 | 132 |
| 124 // Called before each field of the Element is to be processed. | 133 // Called before each field of the Element is to be processed. |
| 125 // Returns true if this is the first call (processing the first field). | 134 // Returns true if this is the first call (processing the first field). |
| 126 bool is_first() { | 135 bool is_first() { |
| 127 if (is_first_) { | 136 if (is_first_) { |
| 128 is_first_ = false; | 137 is_first_ = false; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 197 } |
| 189 | 198 |
| 190 // Writes a prefix. This will write out any pretty printing and | 199 // Writes a prefix. This will write out any pretty printing and |
| 191 // commas that are required, followed by the name and a ':' if | 200 // commas that are required, followed by the name and a ':' if |
| 192 // the name is not null. | 201 // the name is not null. |
| 193 void WritePrefix(StringPiece name); | 202 void WritePrefix(StringPiece name); |
| 194 | 203 |
| 195 // Writes an individual character to the output. | 204 // Writes an individual character to the output. |
| 196 void WriteChar(const char c) { stream_->WriteRaw(&c, sizeof(c)); } | 205 void WriteChar(const char c) { stream_->WriteRaw(&c, sizeof(c)); } |
| 197 | 206 |
| 207 // Returns the current value of empty_name_ok_for_next_key_ and resets it to |
| 208 // false. |
| 209 bool GetAndResetEmptyKeyOk(); |
| 210 |
| 198 google::protobuf::scoped_ptr<Element> element_; | 211 google::protobuf::scoped_ptr<Element> element_; |
| 199 google::protobuf::io::CodedOutputStream* stream_; | 212 google::protobuf::io::CodedOutputStream* stream_; |
| 200 ByteSinkWrapper sink_; | 213 ByteSinkWrapper sink_; |
| 201 const string indent_string_; | 214 const string indent_string_; |
| 202 | 215 |
| 203 // Whether to use regular or websafe base64 encoding for byte fields. Defaults | 216 // Whether to use regular or websafe base64 encoding for byte fields. Defaults |
| 204 // to regular base64 encoding. | 217 // to regular base64 encoding. |
| 205 bool use_websafe_base64_for_bytes_; | 218 bool use_websafe_base64_for_bytes_; |
| 206 | 219 |
| 220 // Whether empty strings should be rendered for the next JSON key. This |
| 221 // setting is only valid until the next key is rendered, after which it gets |
| 222 // reset to false. |
| 223 bool empty_name_ok_for_next_key_; |
| 224 |
| 207 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(JsonObjectWriter); | 225 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(JsonObjectWriter); |
| 208 }; | 226 }; |
| 209 | 227 |
| 210 } // namespace converter | 228 } // namespace converter |
| 211 } // namespace util | 229 } // namespace util |
| 212 } // namespace protobuf | 230 } // namespace protobuf |
| 213 | 231 |
| 214 } // namespace google | 232 } // namespace google |
| 215 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_JSON_OBJECTWRITER_H__ | 233 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_JSON_OBJECTWRITER_H__ |
| OLD | NEW |