Support JSON serialization options for model objects (#811)

This commit is contained in:
Daniel Azuma 2019-07-29 10:40:39 -07:00 committed by GitHub
parent 05a1367af1
commit 75606c6ca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -145,7 +145,7 @@ module Google
def to_json(*a) def to_json(*a)
representation = self.class.const_get(:Representation) representation = self.class.const_get(:Representation)
representation.new(self).to_json(user_options: { skip_undefined: true }) representation.new(self).to_hash(user_options: { skip_undefined: true }).to_json(*a)
end end
end end
end end

View File

@ -23,6 +23,7 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
end end
let(:model_class) do let(:model_class) do
rep_class = representer_class
Class.new do Class.new do
attr_accessor :unset_value attr_accessor :unset_value
attr_accessor :nil_value attr_accessor :nil_value
@ -39,6 +40,8 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
attr_accessor :items attr_accessor :items
attr_accessor :child attr_accessor :child
attr_accessor :children attr_accessor :children
const_set(:Representation, rep_class)
include Google::Apis::Core::JsonObjectSupport
end end
end end
@ -150,6 +153,15 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
include_examples 'it serializes' include_examples 'it serializes'
it 'supports standard serialization from JSON gem' do
str = JSON.generate(model)
expect(str).to start_with("{\"")
end
it 'supports pretty serialization from JSON gem' do
str = JSON.pretty_generate(model)
expect(str).to start_with("{\n ")
end
end end
context 'with hash' do context 'with hash' do