aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Joshua Haberman <jhaberman@gmail.com>2018-07-02 14:00:05 -0700
committerGravatar GitHub <noreply@github.com>2018-07-02 14:00:05 -0700
commit59e04d80cca9b86a97190df0cbab2d93125537fb (patch)
treee162ddf59bb3316c537b938067b289b3f3eb4f15
parent0ea3d74c3d01736ed8e4ee05a235f024ad56cb42 (diff)
parent7b8f571756792ed2296a766c70af4ddf69082099 (diff)
Merge pull request #4321 from devwout/ruby_json_omit_repeated
Ruby JSON encoding omits zero-length repeated fields by default.
-rw-r--r--ruby/ext/google/protobuf_c/encode_decode.c4
-rw-r--r--ruby/tests/basic.rb4
2 files changed, 7 insertions, 1 deletions
diff --git a/ruby/ext/google/protobuf_c/encode_decode.c b/ruby/ext/google/protobuf_c/encode_decode.c
index 12080d03..8c6ded64 100644
--- a/ruby/ext/google/protobuf_c/encode_decode.c
+++ b/ruby/ext/google/protobuf_c/encode_decode.c
@@ -963,13 +963,15 @@ static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink,
if (ary == Qnil) return;
+ size = NUM2INT(RepeatedField_length(ary));
+ if (size == 0 && !emit_defaults) return;
+
upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
if (upb_fielddef_isprimitive(f)) {
sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
}
- size = NUM2INT(RepeatedField_length(ary));
for (int i = 0; i < size; i++) {
void* memory = RepeatedField_index_native(ary, i);
switch (type) {
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb
index 0a5c5fb5..9e9ff385 100644
--- a/ruby/tests/basic.rb
+++ b/ruby/tests/basic.rb
@@ -1273,6 +1273,10 @@ module BasicTest
Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2]))
end
+ def test_json_empty
+ assert TestMessage.encode_json(TestMessage.new) == '{}'
+ end
+
def test_json_emit_defaults
# TODO: Fix JSON in JRuby version.
return if RUBY_PLATFORM == "java"