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