Skip to content

Commit

Permalink
Merge pull request #410 from cucumber/lower-camel-case-field-names-json
Browse files Browse the repository at this point in the history
#to_json(:lower_camel_case) generates camel cased keys
  • Loading branch information
film42 authored Jan 7, 2020
2 parents 3137562 + 30ac31a commit a84b39f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/protobuf/field/field_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ def to_hash_value
# Return a hash-representation of the given values for this field type
# that is safe to convert to JSON.
# The value in this case would be an array.
def to_json_hash_value
def to_json_hash_value(options = {})
if field.respond_to?(:json_encode)
map do |value|
field.json_encode(value)
end
else
map do |value|
value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value : value
value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value(options) : value
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/protobuf/field/field_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def to_hash_value
# The value in this case would be the hash itself, right? Unfortunately
# not because the values of the map could be messages themselves that we
# need to transform.
def to_json_hash_value
def to_json_hash_value(options = {})
if field.respond_to?(:json_encode)
each_with_object({}) do |(key, value), hash|
hash[key] = field.json_encode(value)
end
else
each_with_object({}) do |(key, value), hash|
hash[key] = value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value : value
hash[key] = value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value(options) : value
end
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/protobuf/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,32 @@ def to_hash_with_string_keys
end

def to_json(options = {})
to_json_hash.to_json(options)
to_json_hash(options).to_json(options)
end

# Return a hash-representation of the given fields for this message type that
# is safe to convert to JSON.
def to_json_hash
def to_json_hash(options = {})
result = {}

lower_camel_case = options[:lower_camel_case]

@values.each_key do |field_name|
value = self[field_name]
field = self.class.get_field(field_name, true)

# NB: to_json_hash_value should come before json_encode so as to handle
# repeated fields without extra logic.
hashed_value = if value.respond_to?(:to_json_hash_value)
value.to_json_hash_value
value.to_json_hash_value(options)
elsif field.respond_to?(:json_encode)
field.json_encode(value)
else
value
end

result[field.name] = hashed_value
key = lower_camel_case ? field.name.to_s.camelize(:lower).to_sym : field.name
result[key] = hashed_value
end

result
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/protobuf/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,16 @@

specify { expect(subject.to_json).to eq '{"widget_bytes":["Bo0xSFAXOmI="]}' }
end

context 'using lower camel case field names' do
let(:bytes) { "\x06\x8D1HP\x17:b" }

subject do
::Test::ResourceFindRequest.new(:widget_bytes => [bytes])
end

specify { expect(subject.to_json(:lower_camel_case => true)).to eq '{"widgetBytes":["Bo0xSFAXOmI="]}' }
end
end

describe '.to_json' do
Expand Down

0 comments on commit a84b39f

Please sign in to comment.