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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 public: | 76 public: |
77 // Options that control ProtoStreamObjectWriter class's behavior. | 77 // Options that control ProtoStreamObjectWriter class's behavior. |
78 struct Options { | 78 struct Options { |
79 // Treats integer inputs in google.protobuf.Struct as strings. Normally, | 79 // Treats integer inputs in google.protobuf.Struct as strings. Normally, |
80 // integer values are returned in double field "number_value" of | 80 // integer values are returned in double field "number_value" of |
81 // google.protobuf.Struct. However, this can cause precision loss for | 81 // google.protobuf.Struct. However, this can cause precision loss for |
82 // int64/uint64 inputs. This option is provided for cases that want to | 82 // int64/uint64 inputs. This option is provided for cases that want to |
83 // preserve integer precision. | 83 // preserve integer precision. |
84 bool struct_integers_as_strings; | 84 bool struct_integers_as_strings; |
85 | 85 |
86 // Not treat unknown fields as an error. If there is an unknown fields, | 86 Options() : struct_integers_as_strings(false) {} |
87 // just ignore it and continue to process the rest. | |
88 bool ignore_unknown_fields; | |
89 | |
90 // If true, check if enum name in camel case or without underscore matches | |
91 // the field name. | |
92 bool use_lower_camel_for_enums; | |
93 | |
94 Options() | |
95 : struct_integers_as_strings(false), | |
96 ignore_unknown_fields(false), | |
97 use_lower_camel_for_enums(false) {} | |
98 | 87 |
99 // Default instance of Options with all options set to defaults. | 88 // Default instance of Options with all options set to defaults. |
100 static const Options& Defaults() { | 89 static const Options& Defaults() { |
101 static Options defaults; | 90 static Options defaults; |
102 return defaults; | 91 return defaults; |
103 } | 92 } |
104 }; | 93 }; |
105 | 94 |
106 // Constructor. Does not take ownership of any parameter passed in. | 95 // Constructor. Does not take ownership of any parameter passed in. |
107 ProtoStreamObjectWriter(TypeResolver* type_resolver, | 96 ProtoStreamObjectWriter(TypeResolver* type_resolver, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // Passes a StartList call through to the Any writer. | 133 // Passes a StartList call through to the Any writer. |
145 void StartList(StringPiece name); | 134 void StartList(StringPiece name); |
146 | 135 |
147 // Passes an EndList call through to the Any writer. | 136 // Passes an EndList call through to the Any writer. |
148 void EndList(); | 137 void EndList(); |
149 | 138 |
150 // Renders a data piece on the any. | 139 // Renders a data piece on the any. |
151 void RenderDataPiece(StringPiece name, const DataPiece& value); | 140 void RenderDataPiece(StringPiece name, const DataPiece& value); |
152 | 141 |
153 private: | 142 private: |
154 // Before the "@type" field is encountered, we store all incoming data | |
155 // into this Event struct and replay them after we get the "@type" field. | |
156 class LIBPROTOBUF_EXPORT Event { | |
157 public: | |
158 enum Type { | |
159 START_OBJECT = 0, | |
160 END_OBJECT = 1, | |
161 START_LIST = 2, | |
162 END_LIST = 3, | |
163 RENDER_DATA_PIECE = 4, | |
164 }; | |
165 | |
166 // Constructor for END_OBJECT and END_LIST events. | |
167 explicit Event(Type type) : type_(type), value_(DataPiece::NullData()) {} | |
168 | |
169 // Constructor for START_OBJECT and START_LIST events. | |
170 explicit Event(Type type, StringPiece name) | |
171 : type_(type), | |
172 name_(name.ToString()), | |
173 value_(DataPiece::NullData()) {} | |
174 | |
175 // Constructor for RENDER_DATA_PIECE events. | |
176 explicit Event(StringPiece name, const DataPiece& value) | |
177 : type_(RENDER_DATA_PIECE), name_(name.ToString()), value_(value) { | |
178 DeepCopy(); | |
179 } | |
180 | |
181 Event(const Event& other) | |
182 : type_(other.type_), name_(other.name_), value_(other.value_) { | |
183 DeepCopy(); | |
184 } | |
185 | |
186 Event& operator=(const Event& other) { | |
187 type_ = other.type_; | |
188 name_ = other.name_; | |
189 value_ = other.value_; | |
190 DeepCopy(); | |
191 return *this; | |
192 } | |
193 | |
194 void Replay(AnyWriter* writer) const; | |
195 | |
196 private: | |
197 void DeepCopy(); | |
198 | |
199 Type type_; | |
200 string name_; | |
201 DataPiece value_; | |
202 string value_storage_; | |
203 }; | |
204 | |
205 // Handles starting up the any once we have a type. | 143 // Handles starting up the any once we have a type. |
206 void StartAny(const DataPiece& value); | 144 void StartAny(const DataPiece& value); |
207 | 145 |
208 // Writes the Any out to the parent writer in its serialized form. | 146 // Writes the Any out to the parent writer in its serialized form. |
209 void WriteAny(); | 147 void WriteAny(); |
210 | 148 |
211 // The parent of this writer, needed for various bits such as type info and | 149 // The parent of this writer, needed for various bits such as type info and |
212 // the listeners. | 150 // the listeners. |
213 ProtoStreamObjectWriter* parent_; | 151 ProtoStreamObjectWriter* parent_; |
214 | 152 |
(...skipping 15 matching lines...) Expand all Loading... |
230 int depth_; | 168 int depth_; |
231 | 169 |
232 // True if the type is a well-known type. Well-known types in Any | 170 // True if the type is a well-known type. Well-known types in Any |
233 // has a special formating: | 171 // has a special formating: |
234 // { | 172 // { |
235 // "@type": "type.googleapis.com/google.protobuf.XXX", | 173 // "@type": "type.googleapis.com/google.protobuf.XXX", |
236 // "value": <JSON representation of the type>, | 174 // "value": <JSON representation of the type>, |
237 // } | 175 // } |
238 bool is_well_known_type_; | 176 bool is_well_known_type_; |
239 TypeRenderer* well_known_type_render_; | 177 TypeRenderer* well_known_type_render_; |
240 | |
241 // Store data before the "@type" field. | |
242 std::vector<Event> uninterpreted_events_; | |
243 }; | 178 }; |
244 | 179 |
245 // Represents an item in a stack of items used to keep state between | 180 // Represents an item in a stack of items used to keep state between |
246 // ObjectWrier events. | 181 // ObjectWrier events. |
247 class LIBPROTOBUF_EXPORT Item : public BaseElement { | 182 class LIBPROTOBUF_EXPORT Item : public BaseElement { |
248 public: | 183 public: |
249 // Indicates the type of item. | 184 // Indicates the type of item. |
250 enum ItemType { | 185 enum ItemType { |
251 MESSAGE, // Simple message | 186 MESSAGE, // Simple message |
252 MAP, // Proto3 map type | 187 MAP, // Proto3 map type |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 ProtoStreamObjectWriter* ow_; | 224 ProtoStreamObjectWriter* ow_; |
290 | 225 |
291 // A writer for Any objects, handles all Any-related nonsense. | 226 // A writer for Any objects, handles all Any-related nonsense. |
292 google::protobuf::scoped_ptr<AnyWriter> any_; | 227 google::protobuf::scoped_ptr<AnyWriter> any_; |
293 | 228 |
294 // The type of this element, see enum for permissible types. | 229 // The type of this element, see enum for permissible types. |
295 ItemType item_type_; | 230 ItemType item_type_; |
296 | 231 |
297 // Set of map keys already seen for the type_. Used to validate incoming | 232 // Set of map keys already seen for the type_. Used to validate incoming |
298 // messages so no map key appears more than once. | 233 // messages so no map key appears more than once. |
299 google::protobuf::scoped_ptr<hash_set<string> > map_keys_; | 234 hash_set<string> map_keys_; |
300 | 235 |
301 // Conveys whether this Item is a placeholder or not. Placeholder items are | 236 // Conveys whether this Item is a placeholder or not. Placeholder items are |
302 // pushed to stack to account for special types. | 237 // pushed to stack to account for special types. |
303 bool is_placeholder_; | 238 bool is_placeholder_; |
304 | 239 |
305 // Conveys whether this Item is a list or not. This is used to send | 240 // Conveys whether this Item is a list or not. This is used to send |
306 // StartList or EndList calls to underlying ObjectWriter. | 241 // StartList or EndList calls to underlying ObjectWriter. |
307 bool is_list_; | 242 bool is_list_; |
308 | 243 |
309 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(Item); | 244 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(Item); |
310 }; | 245 }; |
311 | 246 |
312 ProtoStreamObjectWriter(const TypeInfo* typeinfo, | 247 ProtoStreamObjectWriter(const TypeInfo* typeinfo, |
313 const google::protobuf::Type& type, | 248 const google::protobuf::Type& type, |
314 strings::ByteSink* output, ErrorListener* listener); | 249 strings::ByteSink* output, ErrorListener* listener); |
315 | 250 |
316 // Returns true if the field is a map. | 251 // Returns true if the field is a map. |
317 inline bool IsMap(const google::protobuf::Field& field); | 252 bool IsMap(const google::protobuf::Field& field); |
318 | 253 |
319 // Returns true if the field is an any. | 254 // Returns true if the field is an any. |
320 inline bool IsAny(const google::protobuf::Field& field); | 255 bool IsAny(const google::protobuf::Field& field); |
321 | 256 |
322 // Returns true if the field is google.protobuf.Struct. | 257 // Returns true if the field is google.protobuf.Struct. |
323 inline bool IsStruct(const google::protobuf::Field& field); | 258 bool IsStruct(const google::protobuf::Field& field); |
324 | 259 |
325 // Returns true if the field is google.protobuf.Value. | 260 // Returns true if the field is google.protobuf.Value. |
326 inline bool IsStructValue(const google::protobuf::Field& field); | 261 bool IsStructValue(const google::protobuf::Field& field); |
327 | 262 |
328 // Returns true if the field is google.protobuf.ListValue. | 263 // Returns true if the field is google.protobuf.ListValue. |
329 inline bool IsStructListValue(const google::protobuf::Field& field); | 264 bool IsStructListValue(const google::protobuf::Field& field); |
330 | 265 |
331 // Renders google.protobuf.Value in struct.proto. It picks the right oneof | 266 // Renders google.protobuf.Value in struct.proto. It picks the right oneof |
332 // type based on value's type. | 267 // type based on value's type. |
333 static util::Status RenderStructValue(ProtoStreamObjectWriter* ow, | 268 static util::Status RenderStructValue(ProtoStreamObjectWriter* ow, |
334 const DataPiece& value); | 269 const DataPiece& value); |
335 | 270 |
336 // Renders google.protobuf.Timestamp value. | 271 // Renders google.protobuf.Timestamp value. |
337 static util::Status RenderTimestamp(ProtoStreamObjectWriter* ow, | 272 static util::Status RenderTimestamp(ProtoStreamObjectWriter* ow, |
338 const DataPiece& value); | 273 const DataPiece& value); |
339 | 274 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 | 331 |
397 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectWriter); | 332 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectWriter); |
398 }; | 333 }; |
399 | 334 |
400 } // namespace converter | 335 } // namespace converter |
401 } // namespace util | 336 } // namespace util |
402 } // namespace protobuf | 337 } // namespace protobuf |
403 | 338 |
404 } // namespace google | 339 } // namespace google |
405 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTWRITER_H__ | 340 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTWRITER_H__ |
OLD | NEW |