Allows the json marshaller to be passed json marshal options (#4252)

pull/4663/head
Erik Benoist 7 years ago committed by Paul Yang
parent dd2dc0f14f
commit a8e2359329
  1. 4
      ruby/lib/google/protobuf.rb
  2. 4
      ruby/lib/google/protobuf/message_exts.rb
  3. 24
      ruby/tests/encode_decode_test.rb

@ -60,8 +60,8 @@ module Google
msg.to_proto msg.to_proto
end end
def self.encode_json(msg) def self.encode_json(msg, options = {})
msg.to_json msg.to_json(options)
end end
def self.decode(klass, proto) def self.decode(klass, proto)

@ -40,8 +40,8 @@ module Google
module ClassMethods module ClassMethods
end end
def to_json def to_json(options = {})
self.class.encode_json(self) self.class.encode_json(self, options)
end end
def to_proto def to_proto

@ -60,4 +60,28 @@ class EncodeDecodeTest < Test::Unit::TestCase
to = A::B::C::TestMessage.encode(m.oneof_msg) to = A::B::C::TestMessage.encode(m.oneof_msg)
assert_equal '', to assert_equal '', to
end 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 end

Loading…
Cancel
Save