aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/util/internal/json_objectwriter.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/util/internal/json_objectwriter.h')
-rw-r--r--src/google/protobuf/util/internal/json_objectwriter.h81
1 files changed, 38 insertions, 43 deletions
diff --git a/src/google/protobuf/util/internal/json_objectwriter.h b/src/google/protobuf/util/internal/json_objectwriter.h
index 31edc292..4c25b465 100644
--- a/src/google/protobuf/util/internal/json_objectwriter.h
+++ b/src/google/protobuf/util/internal/json_objectwriter.h
@@ -32,9 +32,6 @@
#define GOOGLE_PROTOBUF_UTIL_CONVERTER_JSON_OBJECTWRITER_H__
#include <memory>
-#ifndef _SHARED_PTR_H
-#include <google/protobuf/stubs/shared_ptr.h>
-#endif
#include <string>
#include <google/protobuf/io/coded_stream.h>
@@ -89,46 +86,41 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
public:
JsonObjectWriter(StringPiece indent_string,
google::protobuf::io::CodedOutputStream* out)
- : element_(new Element(NULL)),
+ : element_(new Element(/*parent=*/nullptr, /*is_json_object=*/false)),
stream_(out),
sink_(out),
indent_string_(indent_string.ToString()),
- use_websafe_base64_for_bytes_(false),
- empty_name_ok_for_next_key_(false) {}
+ use_websafe_base64_for_bytes_(false) {}
virtual ~JsonObjectWriter();
// ObjectWriter methods.
- virtual JsonObjectWriter* StartObject(StringPiece name);
- virtual JsonObjectWriter* EndObject();
- virtual JsonObjectWriter* StartList(StringPiece name);
- virtual JsonObjectWriter* EndList();
- virtual JsonObjectWriter* RenderBool(StringPiece name, bool value);
- virtual JsonObjectWriter* RenderInt32(StringPiece name, int32 value);
- virtual JsonObjectWriter* RenderUint32(StringPiece name, uint32 value);
- virtual JsonObjectWriter* RenderInt64(StringPiece name, int64 value);
- virtual JsonObjectWriter* RenderUint64(StringPiece name, uint64 value);
- virtual JsonObjectWriter* RenderDouble(StringPiece name, double value);
- virtual JsonObjectWriter* RenderFloat(StringPiece name, float value);
- virtual JsonObjectWriter* RenderString(StringPiece name, StringPiece value);
- virtual JsonObjectWriter* RenderBytes(StringPiece name, StringPiece value);
- virtual JsonObjectWriter* RenderNull(StringPiece name);
+ virtual JsonObjectWriter* StartObject(StringPiece name) override;
+ virtual JsonObjectWriter* EndObject() override;
+ virtual JsonObjectWriter* StartList(StringPiece name) override;
+ virtual JsonObjectWriter* EndList() override;
+ virtual JsonObjectWriter* RenderBool(StringPiece name, bool value) override;
+ virtual JsonObjectWriter* RenderInt32(StringPiece name, int32 value) override;
+ virtual JsonObjectWriter* RenderUint32(StringPiece name, uint32 value) override;
+ virtual JsonObjectWriter* RenderInt64(StringPiece name, int64 value) override;
+ virtual JsonObjectWriter* RenderUint64(StringPiece name, uint64 value) override;
+ virtual JsonObjectWriter* RenderDouble(StringPiece name, double value) override;
+ virtual JsonObjectWriter* RenderFloat(StringPiece name, float value) override;
+ virtual JsonObjectWriter* RenderString(StringPiece name, StringPiece value) override;
+ virtual JsonObjectWriter* RenderBytes(StringPiece name, StringPiece value) override;
+ virtual JsonObjectWriter* RenderNull(StringPiece name) override;
virtual JsonObjectWriter* RenderNullAsEmpty(StringPiece name);
void set_use_websafe_base64_for_bytes(bool value) {
use_websafe_base64_for_bytes_ = value;
}
- // Whether empty strings should be rendered for the next JSON key. This
- // setting is only valid until the next key is rendered, after which it gets
- // reset to false.
- virtual void empty_name_ok_for_next_key() {
- empty_name_ok_for_next_key_ = true;
- }
-
protected:
class LIBPROTOBUF_EXPORT Element : public BaseElement {
public:
- explicit Element(Element* parent) : BaseElement(parent), is_first_(true) {}
+ Element(Element* parent, bool is_json_object)
+ : BaseElement(parent),
+ is_first_(true),
+ is_json_object_(is_json_object) {}
// Called before each field of the Element is to be processed.
// Returns true if this is the first call (processing the first field).
@@ -140,23 +132,28 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
return false;
}
+ // Whether we are currently renderring inside a JSON object (i.e., between
+ // StartObject() and EndObject()).
+ bool is_json_object() const { return is_json_object_; }
+
private:
bool is_first_;
+ bool is_json_object_;
GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(Element);
};
- virtual Element* element() { return element_.get(); }
+ Element* element() override { return element_.get(); }
private:
class LIBPROTOBUF_EXPORT ByteSinkWrapper : public strings::ByteSink {
public:
explicit ByteSinkWrapper(google::protobuf::io::CodedOutputStream* stream)
: stream_(stream) {}
- virtual ~ByteSinkWrapper() {}
+ ~ByteSinkWrapper() override {}
// ByteSink methods.
- virtual void Append(const char* bytes, size_t n) {
+ void Append(const char* bytes, size_t n) override {
stream_->WriteRaw(bytes, n);
}
@@ -175,8 +172,15 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
return this;
}
- // Pushes a new element to the stack.
- void Push() { element_.reset(new Element(element_.release())); }
+ // Pushes a new JSON array element to the stack.
+ void PushArray() {
+ element_.reset(new Element(element_.release(), /*is_json_object=*/false));
+ }
+
+ // Pushes a new JSON object element to the stack.
+ void PushObject() {
+ element_.reset(new Element(element_.release(), /*is_json_object=*/true));
+ }
// Pops an element off of the stack and deletes the popped element.
void Pop() {
@@ -204,11 +208,7 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
// Writes an individual character to the output.
void WriteChar(const char c) { stream_->WriteRaw(&c, sizeof(c)); }
- // Returns the current value of empty_name_ok_for_next_key_ and resets it to
- // false.
- bool GetAndResetEmptyKeyOk();
-
- google::protobuf::scoped_ptr<Element> element_;
+ std::unique_ptr<Element> element_;
google::protobuf::io::CodedOutputStream* stream_;
ByteSinkWrapper sink_;
const string indent_string_;
@@ -217,11 +217,6 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
// to regular base64 encoding.
bool use_websafe_base64_for_bytes_;
- // Whether empty strings should be rendered for the next JSON key. This
- // setting is only valid until the next key is rendered, after which it gets
- // reset to false.
- bool empty_name_ok_for_next_key_;
-
GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(JsonObjectWriter);
};