From 236e6166880b8313a7447cc0bc91dc6501a834db Mon Sep 17 00:00:00 2001 From: Ryunosuke Sato Date: Sat, 11 Jun 2016 00:57:29 +0900 Subject: 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(?:(?\\[(?:(?(?:\\h{1,4}:){6}(?\\h{1,4}:\\h{1,4}|(?(?[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]|\\d)\\.\\g\\.\\g\\.\\g))|::(?:\\h{1,4}:){5}\\g|\\h{,4}::(?:\\h{1,4}:){4}\\g|(?:(?:\\h{1,4}:)?\\h{1,4})?::(?:\\h{1,4}:){3}\\g|(?:(?:\\h{1,4}:){,2}\\h{1,4})?::(?:\\h{1,4}:){2}\\g|(?:(?:\\h{1,4}:){,3}\\h{1,4})?::\\h{1,4}:\\g|(?:(?:\\h{1,4}:){,4}\\h{1,4})?::\\g|(?:(?:\\h{1,4}:){,5}\\h{1,4})?::\\h{1,4}|(?:(?:\\h{1,4}:){,6}\\h{1,4})?::)|(?v\\h+\\.[!$&-.0-;=A-Z_a-z~]+))\\])|\\g|(?(?:%\\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)"}}} ``` --- activesupport/lib/active_support/core_ext/object/json.rb | 7 +++++++ activesupport/test/json/encoding_test_cases.rb | 2 ++ 2 files changed, 9 insertions(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/object/json.rb b/activesupport/lib/active_support/core_ext/object/json.rb index d49b2fbe54..32a41eafe4 100644 --- a/activesupport/lib/active_support/core_ext/object/json.rb +++ b/activesupport/lib/active_support/core_ext/object/json.rb @@ -1,6 +1,7 @@ # Hack to load json gem first so we can overwrite its to_json. require 'json' require 'bigdecimal' +require 'uri/generic' require 'active_support/core_ext/big_decimal/conversions' # for #to_s require 'active_support/core_ext/hash/except' require 'active_support/core_ext/hash/slice' @@ -192,6 +193,12 @@ class DateTime end end +class URI::Generic #:nodoc: + def as_json(options = nil) + to_s + end +end + class Process::Status #:nodoc: def as_json(options = nil) { :exitstatus => exitstatus, :pid => pid } diff --git a/activesupport/test/json/encoding_test_cases.rb b/activesupport/test/json/encoding_test_cases.rb index 0159ba8606..60d8685d32 100644 --- a/activesupport/test/json/encoding_test_cases.rb +++ b/activesupport/test/json/encoding_test_cases.rb @@ -76,6 +76,8 @@ module JSONTest RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']] + URITests = [[ URI.parse('http://example.com'), %("http://example.com") ]] + DateTests = [[ Date.new(2005,2,1), %("2005/02/01") ]] TimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] -- cgit v1.2.3