File tree Expand file tree Collapse file tree 4 files changed +21
-8
lines changed Expand file tree Collapse file tree 4 files changed +21
-8
lines changed Original file line number Diff line number Diff line change @@ -49,14 +49,14 @@ def to_hash_value
49
49
# Return a hash-representation of the given values for this field type
50
50
# that is safe to convert to JSON.
51
51
# The value in this case would be an array.
52
- def to_json_hash_value
52
+ def to_json_hash_value ( options = { } )
53
53
if field . respond_to? ( :json_encode )
54
54
map do |value |
55
55
field . json_encode ( value )
56
56
end
57
57
else
58
58
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
60
60
end
61
61
end
62
62
end
Original file line number Diff line number Diff line change @@ -58,14 +58,14 @@ def to_hash_value
58
58
# The value in this case would be the hash itself, right? Unfortunately
59
59
# not because the values of the map could be messages themselves that we
60
60
# need to transform.
61
- def to_json_hash_value
61
+ def to_json_hash_value ( options = { } )
62
62
if field . respond_to? ( :json_encode )
63
63
each_with_object ( { } ) do |( key , value ) , hash |
64
64
hash [ key ] = field . json_encode ( value )
65
65
end
66
66
else
67
67
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
69
69
end
70
70
end
71
71
end
Original file line number Diff line number Diff line change @@ -134,29 +134,32 @@ def to_hash_with_string_keys
134
134
end
135
135
136
136
def to_json ( options = { } )
137
- to_json_hash . to_json ( options )
137
+ to_json_hash ( options ) . to_json ( options )
138
138
end
139
139
140
140
# Return a hash-representation of the given fields for this message type that
141
141
# is safe to convert to JSON.
142
- def to_json_hash
142
+ def to_json_hash ( options = { } )
143
143
result = { }
144
144
145
+ lower_camel_case = options [ :lower_camel_case ]
146
+
145
147
@values . each_key do |field_name |
146
148
value = self [ field_name ]
147
149
field = self . class . get_field ( field_name , true )
148
150
149
151
# NB: to_json_hash_value should come before json_encode so as to handle
150
152
# repeated fields without extra logic.
151
153
hashed_value = if value . respond_to? ( :to_json_hash_value )
152
- value . to_json_hash_value
154
+ value . to_json_hash_value ( options )
153
155
elsif field . respond_to? ( :json_encode )
154
156
field . json_encode ( value )
155
157
else
156
158
value
157
159
end
158
160
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
160
163
end
161
164
162
165
result
Original file line number Diff line number Diff line change 437
437
438
438
specify { expect ( subject . to_json ) . to eq '{"widget_bytes":["Bo0xSFAXOmI="]}' }
439
439
end
440
+
441
+ context 'using lower camel case field names' do
442
+ let ( :bytes ) { "\x06 \x8D 1HP\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
440
450
end
441
451
442
452
describe '.to_json' do
You can’t perform that action at this time.
0 commit comments