aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Erik Benoist <erik@reverb.com>2018-05-22 10:14:04 -0500
committerGravatar Paul Yang <TeBoring@users.noreply.github.com>2018-05-22 08:14:04 -0700
commita8e2359329cf9ca1d00720ff77957f6dd0c678ba (patch)
tree41a3c2df105591c3dca1690c6ec5f0236cb6f516
parentdd2dc0f14f9e5dc4e8343cc5f78d5f0bddd8991c (diff)
Allows the json marshaller to be passed json marshal options (#4252)
-rw-r--r--ruby/lib/google/protobuf.rb4
-rw-r--r--ruby/lib/google/protobuf/message_exts.rb4
-rw-r--r--ruby/tests/encode_decode_test.rb24
3 files changed, 28 insertions, 4 deletions
diff --git a/ruby/lib/google/protobuf.rb b/ruby/lib/google/protobuf.rb
index 9b8d8231..4a805e88 100644
--- a/ruby/lib/google/protobuf.rb
+++ b/ruby/lib/google/protobuf.rb
@@ -60,8 +60,8 @@ module Google
msg.to_proto
end
- def self.encode_json(msg)
- msg.to_json
+ def self.encode_json(msg, options = {})
+ msg.to_json(options)
end
def self.decode(klass, proto)
diff --git a/ruby/lib/google/protobuf/message_exts.rb b/ruby/lib/google/protobuf/message_exts.rb
index e10266ba..f432f89f 100644
--- a/ruby/lib/google/protobuf/message_exts.rb
+++ b/ruby/lib/google/protobuf/message_exts.rb
@@ -40,8 +40,8 @@ module Google
module ClassMethods
end
- def to_json
- self.class.encode_json(self)
+ def to_json(options = {})
+ self.class.encode_json(self, options)
end
def to_proto
diff --git a/ruby/tests/encode_decode_test.rb b/ruby/tests/encode_decode_test.rb
index 09581ab0..2bd9b98b 100644
--- a/ruby/tests/encode_decode_test.rb
+++ b/ruby/tests/encode_decode_test.rb
@@ -60,4 +60,28 @@ class EncodeDecodeTest < Test::Unit::TestCase
to = A::B::C::TestMessage.encode(m.oneof_msg)
assert_equal '', to
end
+
+ def test_encode_json
+ msg = A::B::C::TestMessage.new({ optional_int32: 22 })
+ json = msg.to_json
+
+ to = A::B::C::TestMessage.decode_json(json)
+ assert_equal to.optional_int32, 22
+
+ msg = A::B::C::TestMessage.new({ optional_int32: 22 })
+ json = msg.to_json({ preserve_proto_fieldnames: true })
+
+ assert_match 'optional_int32', json
+
+ to = A::B::C::TestMessage.decode_json(json)
+ assert_equal 22, to.optional_int32
+
+ msg = A::B::C::TestMessage.new({ optional_int32: 22 })
+ json = A::B::C::TestMessage.encode_json(
+ msg,
+ { preserve_proto_fieldnames: true, emit_defaults: true }
+ )
+
+ assert_match 'optional_int32', json
+ end
end