object_writer.h 5.1 KB

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