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_INTERNAL_OBJECT_WRITER_H__
32#define GOOGLE_PROTOBUF_UTIL_INTERNAL_OBJECT_WRITER_H__
33
34#include <cstdint>
35
36#include <google/protobuf/stubs/common.h>
37#include <google/protobuf/stubs/strutil.h>
38
39// Must be included last.
40#include <google/protobuf/port_def.inc>
41
42namespace google {
43namespace protobuf {
44namespace util {
45namespace converter {
46
47
48class DataPiece;
49
50// An ObjectWriter is an interface for writing a stream of events
51// representing objects and collections. Implementation of this
52// interface can be used to write an object stream to an in-memory
53// structure, protobufs, JSON, XML, or any other output format
54// desired. The ObjectSource interface is typically used as the
55// source of an object stream.
56//
57// See JsonObjectWriter for a sample implementation of ObjectWriter
58// and its use.
59//
60// Derived classes could be thread-unsafe.
61//
62// TODO(xinb): seems like a prime candidate to apply the RAII paradigm
63// and get rid the need to call EndXXX().
64class PROTOBUF_EXPORT ObjectWriter {
65 public:
66 virtual ~ObjectWriter() {}
67
68 // Starts an object. If the name is empty, the object will not be named.
69 virtual ObjectWriter* StartObject(StringPiece name) = 0;
70
71 // Ends an object.
72 virtual ObjectWriter* EndObject() = 0;
73
74 // Starts a list. If the name is empty, the list will not be named.
75 virtual ObjectWriter* StartList(StringPiece name) = 0;
76
77 // Ends a list.
78 virtual ObjectWriter* EndList() = 0;
79
80 // Renders a boolean value.
81 virtual ObjectWriter* RenderBool(StringPiece name, bool value) = 0;
82
83 // Renders an 32-bit integer value.
84 virtual ObjectWriter* RenderInt32(StringPiece name, int32_t value) = 0;
85
86 // Renders an 32-bit unsigned integer value.
87 virtual ObjectWriter* RenderUint32(StringPiece name,
88 uint32_t value) = 0;
89
90 // Renders a 64-bit integer value.
91 virtual ObjectWriter* RenderInt64(StringPiece name, int64_t value) = 0;
92
93 // Renders an 64-bit unsigned integer value.
94 virtual ObjectWriter* RenderUint64(StringPiece name,
95 uint64_t value) = 0;
96
97
98 // Renders a double value.
99 virtual ObjectWriter* RenderDouble(StringPiece name, double value) = 0;
100 // Renders a float value.
101 virtual ObjectWriter* RenderFloat(StringPiece name, float value) = 0;
102
103 // Renders a StringPiece value. This is for rendering strings.
104 virtual ObjectWriter* RenderString(StringPiece name,
105 StringPiece value) = 0;
106
107 // Renders a bytes value.
108 virtual ObjectWriter* RenderBytes(StringPiece name, StringPiece value) = 0;
109
110 // Renders a Null value.
111 virtual ObjectWriter* RenderNull(StringPiece name) = 0;
112
113
114 // Renders a DataPiece object to a ObjectWriter.
115 static void RenderDataPieceTo(const DataPiece& data, StringPiece name,
116 ObjectWriter* ow);
117
118
119 // Indicates whether this ObjectWriter has completed writing the root message,
120 // usually this means writing of one complete object. Subclasses must override
121 // this behavior appropriately.
122 virtual bool done() { return false; }
123
124 void set_use_strict_base64_decoding(bool value) {
125 use_strict_base64_decoding_ = value;
126 }
127
128 bool use_strict_base64_decoding() const {
129 return use_strict_base64_decoding_;
130 }
131
132 protected:
133 ObjectWriter() : use_strict_base64_decoding_(true) {}
134
135 private:
136 // If set to true, we use the stricter version of base64 decoding for byte
137 // fields by making sure decoded version encodes back to the original string.
138 bool use_strict_base64_decoding_;
139
140 // Do not add any data members to this class.
141 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectWriter);
142};
143
144} // namespace converter
145} // namespace util
146} // namespace protobuf
147} // namespace google
148
149#include <google/protobuf/port_undef.inc>
150
151#endif // GOOGLE_PROTOBUF_UTIL_INTERNAL_OBJECT_WRITER_H__
152