Skip to content

Commit a84b39f

Browse files
authored
Merge pull request #410 from cucumber/lower-camel-case-field-names-json
#to_json(:lower_camel_case) generates camel cased keys
2 parents 3137562 + 30ac31a commit a84b39f

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

lib/protobuf/field/field_array.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ def to_hash_value
4949
# Return a hash-representation of the given values for this field type
5050
# that is safe to convert to JSON.
5151
# The value in this case would be an array.
52-
def to_json_hash_value
52+
def to_json_hash_value(options = {})
5353
if field.respond_to?(:json_encode)
5454
map do |value|
5555
field.json_encode(value)
5656
end
5757
else
5858
map do |value|
59-
value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value : value
59+
value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value(options) : value
6060
end
6161
end
6262
end

lib/protobuf/field/field_hash.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ def to_hash_value
5858
# The value in this case would be the hash itself, right? Unfortunately
5959
# not because the values of the map could be messages themselves that we
6060
# need to transform.
61-
def to_json_hash_value
61+
def to_json_hash_value(options = {})
6262
if field.respond_to?(:json_encode)
6363
each_with_object({}) do |(key, value), hash|
6464
hash[key] = field.json_encode(value)
6565
end
6666
else
6767
each_with_object({}) do |(key, value), hash|
68-
hash[key] = value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value : value
68+
hash[key] = value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value(options) : value
6969
end
7070
end
7171
end

lib/protobuf/message.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,32 @@ def to_hash_with_string_keys
134134
end
135135

136136
def to_json(options = {})
137-
to_json_hash.to_json(options)
137+
to_json_hash(options).to_json(options)
138138
end
139139

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

145+
lower_camel_case = options[:lower_camel_case]
146+
145147
@values.each_key do |field_name|
146148
value = self[field_name]
147149
field = self.class.get_field(field_name, true)
148150

149151
# NB: to_json_hash_value should come before json_encode so as to handle
150152
# repeated fields without extra logic.
151153
hashed_value = if value.respond_to?(:to_json_hash_value)
152-
value.to_json_hash_value
154+
value.to_json_hash_value(options)
153155
elsif field.respond_to?(:json_encode)
154156
field.json_encode(value)
155157
else
156158
value
157159
end
158160

159-
result[field.name] = hashed_value
161+
key = lower_camel_case ? field.name.to_s.camelize(:lower).to_sym : field.name
162+
result[key] = hashed_value
160163
end
161164

162165
result

spec/lib/protobuf/message_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@
437437

438438
specify { expect(subject.to_json).to eq '{"widget_bytes":["Bo0xSFAXOmI="]}' }
439439
end
440+
441+
context 'using lower camel case field names' do
442+
let(:bytes) { "\x06\x8D1HP\x17:b" }
443+
444+
subject do
445+
::Test::ResourceFindRequest.new(:widget_bytes => [bytes])
446+
end
447+
448+
specify { expect(subject.to_json(:lower_camel_case => true)).to eq '{"widgetBytes":["Bo0xSFAXOmI="]}' }
449+
end
440450
end
441451

442452
describe '.to_json' do

0 commit comments

Comments
 (0)