From 008dc92c9d32abcf608883ae44d5a78f59a4e3c2 Mon Sep 17 00:00:00 2001 From: Ewout Date: Thu, 9 Mar 2017 20:47:56 +0100 Subject: Ruby version optionally emits default values in JSON encoding. Usage: Message.encode_json(m, emit_defaults: true) Message fields that are nil will still not appear in the encoded JSON. --- ruby/tests/basic.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'ruby/tests') diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index ca81e3a5..367be167 100644 --- a/ruby/tests/basic.rb +++ b/ruby/tests/basic.rb @@ -1174,6 +1174,36 @@ module BasicTest Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2])) end + def test_json_emit_defaults + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = TestMessage.new + + expected = '{"optionalInt32":0,"optionalInt64":0,"optionalUint32":0,"optionalUint64":0,"optionalBool":false,"optionalFloat":0,"optionalDouble":0,"optionalString":"","optionalBytes":"","optionalEnum":"Default","repeatedInt32":[],"repeatedInt64":[],"repeatedUint32":[],"repeatedUint64":[],"repeatedBool":[],"repeatedFloat":[],"repeatedDouble":[],"repeatedString":[],"repeatedBytes":[],"repeatedMsg":[],"repeatedEnum":[]}' + + assert TestMessage.encode_json(m, :emit_defaults => true) == expected + end + + def test_json_emit_defaults_submsg + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = TestMessage.new(optional_msg: TestMessage2.new) + + expected = '{"optionalInt32":0,"optionalInt64":0,"optionalUint32":0,"optionalUint64":0,"optionalBool":false,"optionalFloat":0,"optionalDouble":0,"optionalString":"","optionalBytes":"","optionalMsg":{"foo":0},"optionalEnum":"Default","repeatedInt32":[],"repeatedInt64":[],"repeatedUint32":[],"repeatedUint64":[],"repeatedBool":[],"repeatedFloat":[],"repeatedDouble":[],"repeatedString":[],"repeatedBytes":[],"repeatedMsg":[],"repeatedEnum":[]}' + + assert TestMessage.encode_json(m, :emit_defaults => true) == expected + end + + def test_json_emit_defaults_repeated_submsg + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = TestMessage.new(repeated_msg: [TestMessage2.new]) + + expected = '{"optionalInt32":0,"optionalInt64":0,"optionalUint32":0,"optionalUint64":0,"optionalBool":false,"optionalFloat":0,"optionalDouble":0,"optionalString":"","optionalBytes":"","optionalEnum":"Default","repeatedInt32":[],"repeatedInt64":[],"repeatedUint32":[],"repeatedUint64":[],"repeatedBool":[],"repeatedFloat":[],"repeatedDouble":[],"repeatedString":[],"repeatedBytes":[],"repeatedMsg":[{"foo":0}],"repeatedEnum":[]}' + + assert TestMessage.encode_json(m, :emit_defaults => true) == expected + end + def test_json_maps # TODO: Fix JSON in JRuby version. return if RUBY_PLATFORM == "java" @@ -1189,6 +1219,14 @@ module BasicTest assert m == m2 end + def test_json_maps_emit_defaults_submsg + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new}) + expected = '{"mapStringInt32":{},"mapStringMsg":{"a":{"foo":0}}}' + assert MapMessage.encode_json(m, :emit_defaults => true) == expected + end + def test_comparison_with_arbitrary_object assert MapMessage.new != nil end -- cgit v1.2.3