Commit Graph

159 Commits

Author SHA1 Message Date
Tyler Rick cb40ae38c1 Allow any module or class to be converted to JSON in a simple way 2021-06-18 14:06:50 +09:00
Rafael Mendonça França e7d207f02f
Change `IPAddr#to_json` to match the behavior of the json gem
Returning the string representation instead of the instance
variables of the object.

    Before:

    ```ruby
    IPAddr.new("127.0.0.1").to_json
    # => "{\"addr\":2130706433,\"family\":2,\"mask_addr\":4294967295}"
    ```

    After:

    ```ruby
    IPAddr.new("127.0.0.1").to_json
    # => "\"127.0.0.1\""
    ```

Fixes #40932.
2020-12-28 05:46:09 +00:00
Ryuta Kamizono c848baffd2 ✂️ [ci skip]
895134bcff (diff-7789e18ec0a61c6845755a257e6eb34fR3)
ee9e308f68 (diff-5e9551294914b338d923032fa904c6beR103)
5f63c771f7 (diff-600d5368b55e46ed961abb4295977ac3R506)
48b6bacbc5 (diff-279ac5c088a3ee7e9f954bbc10d1b773R246)
49adb7f4c6 (diff-0cbe7171fdd7821fd5836896849141c0R111)
9bfe89e68e (diff-9c1c95b001e3680ba8a473cf23508b68R8)
a0f18e6090 (diff-bef0ae2c16dacf77bfd2ba099af8a330R47)
8b4d344815 (diff-09660f59aee7ec893ad609315e8843c5R18)
49d1b5a98d (diff-531e71a9d5fdc8181f38ce4416553694R107)
b674f04756 (diff-7521c0bb452244663b689e77658e63e3R98)
a9012af688 (diff-60bdcf1e1954ec56d41fc4c7bd8d3e39R512)
6380aee182 (diff-5158c234d5e19bb1cffadcbc088400f1R262)
c7820d8124 (diff-f4614e7ba8081c0c9e20fe64b6374228R60)
8df7ed3b88 (diff-0495ed68f82d1bbde593ab5491eab24aR169)
6f94260ac6 (diff-7ecb6672f3fd37dfb79c4539395d3857R28)
03e44f9300 (diff-e9234c4c6668852448059c0a35840bcaR185)
2020-09-16 09:52:10 +09:00
Mads Ohm Larsen e2a2ee008b Decoding JSON dates should respect newlines 2020-05-21 13:04:01 +02:00
Michael Grosser 203998c916
allow running each test with pure ruby path/to/test.rb
also:
 - makes test dependencies obvious
 - makes tests runnable from within subfolders
2019-12-18 08:49:19 -06:00
Ryuta Kamizono c81af6ae72 Enable `Layout/EmptyLinesAroundAccessModifier` cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
Ryuta Kamizono 892e38c78e Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin block
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).

I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
2018-12-21 06:12:42 +09:00
Eileen Uchitelle 8df7ed3b88 Test that nested structs to_json works as expected
Check that options passed to the to_json are passed to all objects that
respond to as_json.
2018-10-11 15:28:52 -04:00
Ryuta Kamizono 14931e537f Remove unused `require "active_support/core_ext/regexp"`
Ruby 2.4 has native `Regexp#match?`.

https://ruby-doc.org/core-2.4.0/Regexp.html#method-i-match-3F

Related #32034.
2018-07-29 14:51:00 +09:00
Ryuta Kamizono 2b35826389 Enable `Layout/SpaceBeforeComma` rubocop rule, and fixed more
Follow up of #31390.
2017-12-12 20:00:50 +09:00
Ryuta Kamizono 7ce8a6af1b Enable `Style/DefWithParentheses` rubocop rule
The def with blank `()` was newly added in #31176, but we have not used
the blank `()` style in most part of our code base.
So I've enabled `Style/DefWithParentheses` to prevent to newly added the
code.
2017-11-27 15:16:12 +09:00
Paul Kuruvilla 813f8e333d Fix #to_json for unreadable IO objects, fixes #26132 2017-10-23 15:49:00 +05:30
Koichi ITO 7c260ae201 Fix RuboCop offenses
And enable `context_dependent` of Style/BracesAroundHashParameters cop.
2017-08-16 17:55:25 +09:00
Koichi ITO ac717d65a3 [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment` 2017-07-11 13:12:32 +09:00
Kir Shatrov 72950568dd Use frozen-string-literal in ActiveSupport 2017-07-09 15:08:29 +03:00
Matthew Draper 87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590, reversing
changes made to afb66a5a59.
2017-07-02 02:15:17 +09:30
Kir Shatrov cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Andrew White ac1862972c Add missing requires
Bundler 1.14.5 moved to lazily loading 'rubygems/spec_fetcher' which
revealed some missing requires from the JSON encoding test file.
2017-02-22 13:25:44 +00:00
Corey Ward 307065f959 Use appropriate assertion based on expectation
This resolves a stern Minitest “warning” about an upcoming
behavior change in MiniTest 6 that will result in the test failing.

https://github.com/seattlerb/minitest/issues/666
2017-01-17 14:25:41 -06:00
Akira Matsuda 9360b6be63 class Foo < Struct.new(:x) creates an extra unneeded anonymous class
because Struct.new returns a Class, we just can give it a name and use it directly without inheriting from it
2017-01-13 15:13:47 +09:00
Akira Matsuda e8ba0c0f21 "Use assert_nil if expecting nil. This will fail in minitest 6." 2016-12-25 02:29:52 +09:00
Akira Matsuda 3fe6a5d510 Privatize unneededly protected methods in Active Support tests 2016-12-24 00:22:29 +09:00
Andrew White 89071400b4 Fix typo in constant reference 2016-11-13 15:59:13 +00:00
Andrew White 60d82fa141 It's a NAN not Infinity 2016-11-13 15:57:42 +00:00
Andrew White d2890f76d4 Use literal values in assertions
Using the method you're testing to generate expected
values can lead to bugs being masked.
2016-11-13 15:31:05 +00:00
Prathamesh Sonpatki 8776f15b44
Fix an issue with JSON encoding of "Infinity" and "NaN" values
- When `as_json` returns `Infinity` or `NaN` as the value of any of the key,
  we don't used to call `as_json` on it as it was treated as primitive.
- This used to pass `Infinity` or `NaN` to `JSON.generate` and Ruby used
  to throw an error for `Infinity/NaN not allowed in JSON.`
- This patch changes the code to call `as_json` on these primitives so
  that they are converted to proper values before being passed to
  `JSON.generate`.
- Fixes #26877.
2016-10-30 20:08:55 +05:30
Rafael Mendonça França fe1f4b2ad5
Add more rubocop rules about whitespaces 2016-10-29 01:17:49 -02:00
Rafael Mendonça França 55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Ryuta Kamizono 762e3f05f3 Add `Style/EmptyLines` in `.rubocop.yml` and remove extra empty lines 2016-08-07 17:50:59 +09:00
Xavier Noria b326e82dc0 applies remaining conventions across the project 2016-08-06 20:20:22 +02:00
Xavier Noria 80e66cc4d9 normalizes indentation and whitespace across the project 2016-08-06 20:16:27 +02:00
Xavier Noria 411ccbdab2 remove redundant curlies from hash arguments 2016-08-06 19:44:11 +02:00
Xavier Noria 5c315a8fa6 modernizes hash syntax in activesupport 2016-08-06 19:38:33 +02:00
Xavier Noria a731125f12 applies new string literal convention in activesupport/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:10:53 +02:00
Xavier Noria cfc91c31aa systematic revision of =~ usage in AS
Where appropriate prefer the more concise Regexp#match?, String#include?,
String#start_with?, and String#end_with?
2016-07-22 23:13:49 +02:00
Ryunosuke Sato abbf00e583 Define `Pathname#as_json`
When the Pathname object is converted as JSON,
it should be a string that means itself.

Expected:
```
>> Pathname.new('/path/to/somewhere.txt').as_json
"/path/to/somewhere.txt"
```

Actual:
```
>> Pathname.new('/path/to/somewhere.txt').as_json
{"path"=>"/path/to/somewhere.txt"}
```
2016-06-25 21:50:16 +09:00
Ryunosuke Sato 236e616688 Define `URI::Generic#as_json`
When the URI object is converted as JSON,
it is expected that it is a string that means its URI.

Expected:
```
>> URI.parse('http://example.com').as_json
"http://example.com"
```

Actual:
```
>> URI.parse('http://example.com').as_json
{"scheme"=>"http",
 "user"=>nil,
 "password"=>nil,
 "host"=>"example.com",
 "port"=>80,
 "path"=>"",
 "query"=>nil,
 "opaque"=>nil,
 "fragment"=>nil,
 "parser"=>
  {"regexp"=>
    {"SCHEME"=>"(?-mix:\\A[A-Za-z][A-Za-z0-9+\\-.]*\\z)",
     "USERINFO"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=A-Z_a-z~])*\\z)",
     "HOST"=>
      "(?-mix:\\A(?:(?<IP-literal>\\[(?:(?<IPv6address>(?:\\h{1,4}:){6}(?<ls32>\\h{1,4}:\\h{1,4}|(?<IPv4address>(?<dec-octet>[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]|\\d)\\.\\g<dec-octet>\\.\\g<dec-octet>\\.\\g<dec-octet>))|::(?:\\h{1,4}:){5}\\g<ls32>|\\h{,4}::(?:\\h{1,4}:){4}\\g<ls32>|(?:(?:\\h{1,4}:)?\\h{1,4})?::(?:\\h{1,4}:){3}\\g<ls32>|(?:(?:\\h{1,4}:){,2}\\h{1,4})?::(?:\\h{1,4}:){2}\\g<ls32>|(?:(?:\\h{1,4}:){,3}\\h{1,4})?::\\h{1,4}:\\g<ls32>|(?:(?:\\h{1,4}:){,4}\\h{1,4})?::\\g<ls32>|(?:(?:\\h{1,4}:){,5}\\h{1,4})?::\\h{1,4}|(?:(?:\\h{1,4}:){,6}\\h{1,4})?::)|(?<IPvFuture>v\\h+\\.[!$&-.0-;=A-Z_a-z~]+))\\])|\\g<IPv4address>|(?<reg-name>(?:%\\h\\h|[!$&-.0-9;=A-Z_a-z~])*))\\z)",
     "ABS_PATH"=>
      "(?-mix:\\A\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*(?:\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*)*\\z)",
     "REL_PATH"=>
      "(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])+(?:\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*)*\\z)",
     "QUERY"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~\\/?])*\\z)",
     "FRAGMENT"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~\\/?])*\\z)",
     "OPAQUE"=>"(?-mix:\\A(?:[^\\/].*)?\\z)",
     "PORT"=>
      "(?-mix:\\A[\\x09\\x0a\\x0c\\x0d ]*\\d*[\\x09\\x0a\\x0c\\x0d ]*\\z)"}}}
```
2016-06-25 16:43:58 +09:00
Godfrey Chan 3a9428df91 Merge pull request #23011 from arnvald/bugfix/correctly_parse_date
Use correct timezone when parsing date in json
2016-06-25 12:02:07 +08:00
namusyaka 91386c1b35 Fix behavior of JSON encoding for Exception 2016-04-09 23:49:43 +09:00
Grzegorz Witek a3ddd5f157 Use correct timezone when parsing date in json
Fixes https://github.com/rails/rails/issues/22171

Time specified in ISO 8601 format without `Z` should be considered as local
time, yet until now it was treated as UTC.

This commit fixes problem by parsing time using timezone specified in
application config.

The downside of this solution is performance hit (`Time.zone.parse` is ~ 1.6x
slower than `Time.parse`), so maybe there's a better solution.
2016-01-22 09:25:28 +08:00
Godfrey Chan 696a19fb9b Expand the JSON test coverage for Struct and Hash (?!) 2015-07-11 12:48:16 -07:00
Godfrey Chan 2926d73e32 Add tests to ensure we don't interfere with json gem's output 2015-07-11 12:48:09 -07:00
Rafael Mendonça França 193b76cbf0 Escape HTML entities in JSON keys
Fixes CVE-2015-3226
2015-06-16 17:27:48 -03:00
Robin Dupret 562b0b2368 Skip the failing tests on Rubinius for now 2015-03-02 17:28:12 +01:00
Vipul A M 6eced6a1fe Removed magic comments # encoding: utf-8 , since its default from ruby 2.0 onwards. 2015-02-03 20:51:40 +05:30
Rafael Mendonça França d6e06ea827 Remove deprecated ActiveSupport::JSON::Encoding::CircularReferenceError. 2015-01-04 11:58:41 -03:00
Rafael Mendonça França c8019c0611 Remove deprecated ActiveSupport::JSON::Encoding.encode_big_decimal_as_string= 2015-01-04 11:58:40 -03:00
claudiob 64b09823e6 Remove redundant `to_s` in interpolation 2014-10-30 08:48:32 -07:00
Yves Senn e32dade866 Merge pull request #15856 from zuhao/refactor_activesupport_decoding_test
Use with_parse_json_times helper in tests.
2014-06-22 13:47:25 +02:00
Zuhao Wan 42b33590f3 Avoid hardcoded magic number in test teardown. 2014-06-22 18:36:07 +08:00