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