Commit 761da5c
authored
Improve key formatting for nested hashes and disable by default (#497)
* Fix changing key format in child elements
Since #486, key format is applied deeply to hashes and arrays that are
passed to `set!`, `merge!` and `array!`:
json.key_format! :upcase
json.set!(:foo, {some: "value"})
# => {"FOO": {"SOME": "value"}}
json.key_format! :upcase
json.merge!({some: "value"})
# => {"SOME": "value"}
json.key_format! :upcase
json.array!([{some: "value"}])
# => [{"SOME": "value"}]
This also works for arrays and hashes extracted from objects:
comment = Struct.new(:author).new({ first_name: 'John', last_name: 'Doe' })
json.key_format! camlize: :lower
json.set!(:comment, comment, :author)
# => {"comment": {"author": {"firstName": "John", "lastName": "Doe"}}}
As a side effect of the change, key format is also applied to the
result of nested blocks, making it impossible to change key format in
the scope of a block:
json.key_format! camelize: :lower
json.level_one do
json.key_format! :upcase
json.value 'two'
end
# => jbuilder 2.10.0: {"levelOne": {"VALUE": "two"}}
# => jbuilder 2.11.0: {"levelOne": {"vALUE": "two"}}
The string "vALUE" results from calling
`"value".upcase.camelize(:lower)`. The same happens when trying to
change the key format inside of an `array!` block.
This happens since key transformation was added in the `_merge_values`
method, which is used both by `merge!` but also when `set!` is called
with a block.
To restore the previous behavior, we pull the `_transform_keys` call
up into `merge!` itself. To make sure extracted hashes and arrays keep
being transformed (see comment/author example above), we apply
`_transform_keys` in `_extract_hash_values` and
`_extract_method_values`.
This also aligns the behavior of `extract!` which was left out in #486:
comment = {author: { first_name: 'John', last_name: 'Doe' }}
result = jbuild do |json|
json.key_format! camelize: :lower
json.extract! comment, :author
end
# => jbuilder 2.10 and 2.11: {"author": { :first_name => "John", :last_name => "Doe" }}
# => now: {"author": { "firstName": "John", "lastName": "Doe" }}
Finally, to fix `array!`, we make it call `_merge_values` directly
instead of relying on `merge!`. `array!` then has to transform keys
itself when a collection is passed to preserve the new behavior
introduced by #486.
* Disable deeply formatting keys of nested hashes by default
Since #486, key format was also applied to nested hashes that are
passed as values:
json.key_format! camelize: :lower
json.settings({some_value: "abc"})
# => { "settings": { "someValue": "abc" }}
This breaks code that relied on the previous behavior. Add a
`deep_format_keys!` directive that can be used to opt into this new
behavior:
json.key_format! camelize: :lower
json.deep_format_keys!
json.settings({some_value: "abc"})
# => { "settings": { "someValue": "abc" }}1 parent 18c06fe commit 761da5c
3 files changed
+196
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
277 | 296 | | |
278 | 297 | | |
279 | 298 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
131 | 133 | | |
132 | 134 | | |
133 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
134 | 161 | | |
135 | 162 | | |
136 | 163 | | |
| |||
190 | 217 | | |
191 | 218 | | |
192 | 219 | | |
193 | | - | |
| 220 | + | |
194 | 221 | | |
195 | 222 | | |
196 | | - | |
| 223 | + | |
197 | 224 | | |
198 | 225 | | |
199 | 226 | | |
| |||
244 | 271 | | |
245 | 272 | | |
246 | 273 | | |
247 | | - | |
| 274 | + | |
248 | 275 | | |
249 | 276 | | |
250 | 277 | | |
| |||
255 | 282 | | |
256 | 283 | | |
257 | 284 | | |
258 | | - | |
| 285 | + | |
259 | 286 | | |
260 | 287 | | |
261 | 288 | | |
262 | | - | |
| 289 | + | |
263 | 290 | | |
264 | 291 | | |
265 | 292 | | |
| |||
273 | 300 | | |
274 | 301 | | |
275 | 302 | | |
276 | | - | |
| 303 | + | |
277 | 304 | | |
278 | | - | |
| 305 | + | |
279 | 306 | | |
280 | | - | |
| 307 | + | |
281 | 308 | | |
282 | 309 | | |
283 | 310 | | |
| |||
288 | 315 | | |
289 | 316 | | |
290 | 317 | | |
| 318 | + | |
| 319 | + | |
291 | 320 | | |
292 | 321 | | |
293 | 322 | | |
| |||
312 | 341 | | |
313 | 342 | | |
314 | 343 | | |
315 | | - | |
| 344 | + | |
316 | 345 | | |
317 | 346 | | |
318 | 347 | | |
319 | 348 | | |
320 | | - | |
| 349 | + | |
321 | 350 | | |
322 | 351 | | |
323 | 352 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
566 | 566 | | |
567 | 567 | | |
568 | 568 | | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
569 | 599 | | |
570 | 600 | | |
571 | 601 | | |
| |||
593 | 623 | | |
594 | 624 | | |
595 | 625 | | |
596 | | - | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
597 | 681 | | |
598 | 682 | | |
599 | 683 | | |
| 684 | + | |
600 | 685 | | |
601 | 686 | | |
602 | 687 | | |
603 | 688 | | |
604 | 689 | | |
605 | 690 | | |
606 | | - | |
| 691 | + | |
607 | 692 | | |
608 | 693 | | |
609 | 694 | | |
| 695 | + | |
610 | 696 | | |
611 | 697 | | |
612 | 698 | | |
613 | 699 | | |
614 | 700 | | |
615 | 701 | | |
616 | | - | |
| 702 | + | |
617 | 703 | | |
618 | 704 | | |
619 | 705 | | |
| 706 | + | |
620 | 707 | | |
621 | 708 | | |
622 | 709 | | |
623 | 710 | | |
624 | 711 | | |
625 | 712 | | |
626 | | - | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
627 | 725 | | |
628 | 726 | | |
629 | 727 | | |
| 728 | + | |
630 | 729 | | |
631 | 730 | | |
632 | 731 | | |
633 | 732 | | |
634 | 733 | | |
635 | 734 | | |
636 | | - | |
| 735 | + | |
637 | 736 | | |
638 | 737 | | |
639 | 738 | | |
640 | 739 | | |
| 740 | + | |
641 | 741 | | |
642 | 742 | | |
643 | 743 | | |
644 | 744 | | |
645 | 745 | | |
646 | 746 | | |
647 | 747 | | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
648 | 781 | | |
649 | 782 | | |
650 | 783 | | |
| |||
0 commit comments