diff --git a/lib/protobuf/field/field_array.rb b/lib/protobuf/field/field_array.rb index e4f2eb1a..eb1f29d9 100644 --- a/lib/protobuf/field/field_array.rb +++ b/lib/protobuf/field/field_array.rb @@ -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 diff --git a/lib/protobuf/field/field_hash.rb b/lib/protobuf/field/field_hash.rb index 36b26447..94eedbb9 100644 --- a/lib/protobuf/field/field_hash.rb +++ b/lib/protobuf/field/field_hash.rb @@ -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 diff --git a/lib/protobuf/message.rb b/lib/protobuf/message.rb index a13c0d19..06b26b94 100644 --- a/lib/protobuf/message.rb +++ b/lib/protobuf/message.rb @@ -134,14 +134,16 @@ 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) @@ -149,14 +151,15 @@ def to_json_hash # 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 diff --git a/spec/lib/protobuf/message_spec.rb b/spec/lib/protobuf/message_spec.rb index 96668b67..13110bc9 100644 --- a/spec/lib/protobuf/message_spec.rb +++ b/spec/lib/protobuf/message_spec.rb @@ -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