blob: 9f07363d89e52142d2afb5e1c1de1ec4a46a8e11 [file] [log] [blame]
Feng Xiaoe96ff302015-06-15 18:21:48 -07001// 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/stringpiece.h>
36
37namespace google {
38namespace protobuf {
39namespace util {
40namespace converter {
41
42class DataPiece;
43
44// An ObjectWriter is an interface for writing a stream of events
45// representing objects and collections. Implementation of this
46// interface can be used to write an object stream to an in-memory
47// structure, protobufs, JSON, XML, or any other output format
48// desired. The ObjectSource interface is typically used as the
49// source of an object stream.
50//
51// See JsonObjectWriter for a sample implementation of ObjectWriter
52// and its use.
53//
54// Derived classes could be thread-unsafe.
55//
56// TODO(xinb): seems like a prime candidate to apply the RAII paradigm
57// and get rid the need to call EndXXX().
58class LIBPROTOBUF_EXPORT ObjectWriter {
59 public:
60 virtual ~ObjectWriter() {}
61
62 // Starts an object. If the name is empty, the object will not be named.
63 virtual ObjectWriter* StartObject(StringPiece name) = 0;
64
65 // Ends an object.
66 virtual ObjectWriter* EndObject() = 0;
67
68 // Starts a list. If the name is empty, the list will not be named.
69 virtual ObjectWriter* StartList(StringPiece name) = 0;
70
71 // Ends a list.
72 virtual ObjectWriter* EndList() = 0;
73
74 // Renders a boolean value.
75 virtual ObjectWriter* RenderBool(StringPiece name, bool value) = 0;
76
77 // Renders an 32-bit integer value.
78 virtual ObjectWriter* RenderInt32(StringPiece name, int32 value) = 0;
79
80 // Renders an 32-bit unsigned integer value.
81 virtual ObjectWriter* RenderUint32(StringPiece name, uint32 value) = 0;
82
83 // Renders a 64-bit integer value.
84 virtual ObjectWriter* RenderInt64(StringPiece name, int64 value) = 0;
85
86 // Renders an 64-bit unsigned integer value.
87 virtual ObjectWriter* RenderUint64(StringPiece name, uint64 value) = 0;
88
89 // Renders a double value.
90 virtual ObjectWriter* RenderDouble(StringPiece name, double value) = 0;
91
92 // Renders a float value.
93 virtual ObjectWriter* RenderFloat(StringPiece name, float value) = 0;
94
95 // Renders a StringPiece value. This is for rendering strings.
96 virtual ObjectWriter* RenderString(StringPiece name, StringPiece value) = 0;
97
98 // Renders a bytes value.
99 virtual ObjectWriter* RenderBytes(StringPiece name, StringPiece value) = 0;
100
101 // Renders a Null value.
102 virtual ObjectWriter* RenderNull(StringPiece name) = 0;
103
Feng Xiaoe96ff302015-06-15 18:21:48 -0700104 // Renders a DataPiece object to a ObjectWriter.
105 static void RenderDataPieceTo(const DataPiece& data, StringPiece name,
106 ObjectWriter* ow);
107
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700108 // Indicates whether this ObjectWriter has completed writing the root message,
109 // usually this means writing of one complete object. Subclasses must override
110 // this behavior appropriately.
111 virtual bool done() { return false; }
112
113 void set_use_strict_base64_decoding(bool value) {
114 use_strict_base64_decoding_ = value;
115 }
116
117 bool use_strict_base64_decoding() const {
118 return use_strict_base64_decoding_;
119 }
120
Feng Xiaoe96ff302015-06-15 18:21:48 -0700121 protected:
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700122 ObjectWriter() : use_strict_base64_decoding_(true) {}
Feng Xiaoe96ff302015-06-15 18:21:48 -0700123
124 private:
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700125 // If set to true, we use the stricter version of base64 decoding for byte
126 // fields by making sure decoded version encodes back to the original string.
127 bool use_strict_base64_decoding_;
128
Feng Xiaoe96ff302015-06-15 18:21:48 -0700129 // Do not add any data members to this class.
130 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectWriter);
131};
132
133} // namespace converter
134} // namespace util
135} // namespace protobuf
136
137} // namespace google
138#endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_OBJECT_WRITER_H__