diff options
Diffstat (limited to 'src/google/protobuf/util/internal')
7 files changed, 33 insertions, 50 deletions
diff --git a/src/google/protobuf/util/internal/json_escaping.h b/src/google/protobuf/util/internal/json_escaping.h index 9b8b2afd..5495c57f 100644 --- a/src/google/protobuf/util/internal/json_escaping.h +++ b/src/google/protobuf/util/internal/json_escaping.h @@ -28,8 +28,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef NET_PROTO2_UTIL_CONVERTER_STRINGS_JSON_ESCAPING_H_ -#define NET_PROTO2_UTIL_CONVERTER_STRINGS_JSON_ESCAPING_H_ +#ifndef GOOGLE_PROTOBUF_UTIL_INTERNAL__JSON_ESCAPING_H__ +#define GOOGLE_PROTOBUF_UTIL_INTERNAL__JSON_ESCAPING_H__ #include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/bytestream.h> @@ -86,6 +86,6 @@ class JsonEscaping { } // namespace converter } // namespace util } // namespace protobuf -} // namespace google -#endif // NET_PROTO2_UTIL_CONVERTER_STRINGS_JSON_ESCAPING_H_ +} // namespace google +#endif // GOOGLE_PROTOBUF_UTIL_INTERNAL__JSON_ESCAPING_H__ diff --git a/src/google/protobuf/util/internal/json_objectwriter.cc b/src/google/protobuf/util/internal/json_objectwriter.cc index 6e4edd88..a431177a 100644 --- a/src/google/protobuf/util/internal/json_objectwriter.cc +++ b/src/google/protobuf/util/internal/json_objectwriter.cc @@ -57,7 +57,7 @@ JsonObjectWriter::~JsonObjectWriter() { JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) { WritePrefix(name); WriteChar('{'); - Push(); + PushObject(); return this; } @@ -71,7 +71,7 @@ JsonObjectWriter* JsonObjectWriter::EndObject() { JsonObjectWriter* JsonObjectWriter::StartList(StringPiece name) { WritePrefix(name); WriteChar('['); - Push(); + PushArray(); return this; } @@ -172,8 +172,7 @@ void JsonObjectWriter::WritePrefix(StringPiece name) { bool not_first = !element()->is_first(); if (not_first) WriteChar(','); if (not_first || !element()->is_root()) NewLine(); - bool empty_key_ok = GetAndResetEmptyKeyOk(); - if (!name.empty() || empty_key_ok) { + if (!name.empty() || element()->is_json_object()) { WriteChar('"'); if (!name.empty()) { ArrayByteSource source(name); @@ -184,12 +183,6 @@ void JsonObjectWriter::WritePrefix(StringPiece name) { } } -bool JsonObjectWriter::GetAndResetEmptyKeyOk() { - bool retval = empty_name_ok_for_next_key_; - empty_name_ok_for_next_key_ = false; - return retval; -} - } // namespace converter } // namespace util } // namespace protobuf diff --git a/src/google/protobuf/util/internal/json_objectwriter.h b/src/google/protobuf/util/internal/json_objectwriter.h index 31edc292..81da0500 100644 --- a/src/google/protobuf/util/internal/json_objectwriter.h +++ b/src/google/protobuf/util/internal/json_objectwriter.h @@ -89,12 +89,11 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { public: JsonObjectWriter(StringPiece indent_string, google::protobuf::io::CodedOutputStream* out) - : element_(new Element(NULL)), + : element_(new Element(/*parent=*/NULL, /*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. @@ -118,17 +117,13 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { 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,8 +135,13 @@ 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); }; @@ -175,8 +175,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,10 +211,6 @@ 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_; google::protobuf::io::CodedOutputStream* stream_; ByteSinkWrapper sink_; @@ -217,11 +220,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); }; diff --git a/src/google/protobuf/util/internal/json_objectwriter_test.cc b/src/google/protobuf/util/internal/json_objectwriter_test.cc index bbd9d78a..8cc588a6 100644 --- a/src/google/protobuf/util/internal/json_objectwriter_test.cc +++ b/src/google/protobuf/util/internal/json_objectwriter_test.cc @@ -95,6 +95,12 @@ TEST_F(JsonObjectWriterTest, EmptyList) { output_.substr(0, out_stream_->ByteCount())); } +TEST_F(JsonObjectWriterTest, EmptyObjectKey) { + ow_ = new JsonObjectWriter("", out_stream_); + ow_->StartObject("")->RenderString("", "value")->EndObject(); + EXPECT_EQ("{\"\":\"value\"}", output_.substr(0, out_stream_->ByteCount())); +} + TEST_F(JsonObjectWriterTest, ObjectInObject) { ow_ = new JsonObjectWriter("", out_stream_); ow_->StartObject("") diff --git a/src/google/protobuf/util/internal/object_writer.h b/src/google/protobuf/util/internal/object_writer.h index b6fbd19b..5781aa1e 100644 --- a/src/google/protobuf/util/internal/object_writer.h +++ b/src/google/protobuf/util/internal/object_writer.h @@ -119,13 +119,6 @@ class LIBPROTOBUF_EXPORT ObjectWriter { return use_strict_base64_decoding_; } - // Whether empty strings should be rendered for the next name for Start/Render - // calls. This setting is only valid until the next key is rendered, after - // which it gets reset. - // It is up to the derived classes to interpret this and render accordingly. - // Default implementation ignores this setting. - virtual void empty_name_ok_for_next_key() {} - protected: ObjectWriter() : use_strict_base64_decoding_(true) {} diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc index 02360a1a..72ad5a7a 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource.cc @@ -300,8 +300,6 @@ StatusOr<uint32> ProtoStreamObjectSource::RenderMap( return Status(util::error::INTERNAL, "Invalid map entry."); } ASSIGN_OR_RETURN(map_key, MapKeyDefaultValueAsString(*key_field)); - // Key is empty, force it to render as empty (for string values). - ow->empty_name_ok_for_next_key(); } RETURN_IF_ERROR(RenderField(field, map_key, ow)); } else { diff --git a/src/google/protobuf/util/internal/utility.cc b/src/google/protobuf/util/internal/utility.cc index 11780ee8..b1007765 100644 --- a/src/google/protobuf/util/internal/utility.cc +++ b/src/google/protobuf/util/internal/utility.cc @@ -353,8 +353,6 @@ bool IsMap(const google::protobuf::Field& field, google::protobuf::Field_Cardinality_CARDINALITY_REPEATED && (GetBoolOptionOrDefault(type.options(), "map_entry", false) || GetBoolOptionOrDefault(type.options(), - "google.protobuf.MessageOptions.map_entry", false) || - GetBoolOptionOrDefault(type.options(), "google.protobuf.MessageOptions.map_entry", false)); } @@ -362,9 +360,6 @@ bool IsMap(const google::protobuf::Field& field, bool IsMessageSetWireFormat(const google::protobuf::Type& type) { return GetBoolOptionOrDefault(type.options(), "message_set_wire_format", false) || - GetBoolOptionOrDefault(type.options(), - "google.protobuf.MessageOptions.message_set_wire_format", - false) || GetBoolOptionOrDefault( type.options(), "google.protobuf.MessageOptions.message_set_wire_format", false); |