• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_OBJECT_WRITER_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_OBJECT_WRITER_H__
33 
34 #include <google/protobuf/stubs/common.h>
35 #include <google/protobuf/stubs/strutil.h>
36 
37 // Must be included last.
38 #include <google/protobuf/port_def.inc>
39 
40 namespace google {
41 namespace protobuf {
42 namespace util {
43 namespace converter {
44 
45 
46 class DataPiece;
47 
48 // An ObjectWriter is an interface for writing a stream of events
49 // representing objects and collections. Implementation of this
50 // interface can be used to write an object stream to an in-memory
51 // structure, protobufs, JSON, XML, or any other output format
52 // desired. The ObjectSource interface is typically used as the
53 // source of an object stream.
54 //
55 // See JsonObjectWriter for a sample implementation of ObjectWriter
56 // and its use.
57 //
58 // Derived classes could be thread-unsafe.
59 //
60 // TODO(xinb): seems like a prime candidate to apply the RAII paradigm
61 // and get rid the need to call EndXXX().
62 class PROTOBUF_EXPORT ObjectWriter {
63  public:
~ObjectWriter()64   virtual ~ObjectWriter() {}
65 
66   // Starts an object. If the name is empty, the object will not be named.
67   virtual ObjectWriter* StartObject(StringPiece name) = 0;
68 
69   // Ends an object.
70   virtual ObjectWriter* EndObject() = 0;
71 
72   // Starts a list. If the name is empty, the list will not be named.
73   virtual ObjectWriter* StartList(StringPiece name) = 0;
74 
75   // Ends a list.
76   virtual ObjectWriter* EndList() = 0;
77 
78   // Renders a boolean value.
79   virtual ObjectWriter* RenderBool(StringPiece name, bool value) = 0;
80 
81   // Renders an 32-bit integer value.
82   virtual ObjectWriter* RenderInt32(StringPiece name, int32 value) = 0;
83 
84   // Renders an 32-bit unsigned integer value.
85   virtual ObjectWriter* RenderUint32(StringPiece name, uint32 value) = 0;
86 
87   // Renders a 64-bit integer value.
88   virtual ObjectWriter* RenderInt64(StringPiece name, int64 value) = 0;
89 
90   // Renders an 64-bit unsigned integer value.
91   virtual ObjectWriter* RenderUint64(StringPiece name, uint64 value) = 0;
92 
93 
94   // Renders a double value.
95   virtual ObjectWriter* RenderDouble(StringPiece name, double value) = 0;
96   // Renders a float value.
97   virtual ObjectWriter* RenderFloat(StringPiece name, float value) = 0;
98 
99   // Renders a StringPiece value. This is for rendering strings.
100   virtual ObjectWriter* RenderString(StringPiece name,
101                                      StringPiece value) = 0;
102 
103   // Renders a bytes value.
104   virtual ObjectWriter* RenderBytes(StringPiece name, StringPiece value) = 0;
105 
106   // Renders a Null value.
107   virtual ObjectWriter* RenderNull(StringPiece name) = 0;
108 
109 
110   // Renders a DataPiece object to a ObjectWriter.
111   static void RenderDataPieceTo(const DataPiece& data, StringPiece name,
112                                 ObjectWriter* ow);
113 
114 
115   // Indicates whether this ObjectWriter has completed writing the root message,
116   // usually this means writing of one complete object. Subclasses must override
117   // this behavior appropriately.
done()118   virtual bool done() { return false; }
119 
set_use_strict_base64_decoding(bool value)120   void set_use_strict_base64_decoding(bool value) {
121     use_strict_base64_decoding_ = value;
122   }
123 
use_strict_base64_decoding()124   bool use_strict_base64_decoding() const {
125     return use_strict_base64_decoding_;
126   }
127 
128  protected:
ObjectWriter()129   ObjectWriter() : use_strict_base64_decoding_(true) {}
130 
131  private:
132   // If set to true, we use the stricter version of base64 decoding for byte
133   // fields by making sure decoded version encodes back to the original string.
134   bool use_strict_base64_decoding_;
135 
136   // Do not add any data members to this class.
137   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectWriter);
138 };
139 
140 }  // namespace converter
141 }  // namespace util
142 }  // namespace protobuf
143 }  // namespace google
144 
145 #include <google/protobuf/port_undef.inc>
146 
147 #endif  // GOOGLE_PROTOBUF_UTIL_CONVERTER_OBJECT_WRITER_H__
148