From eb8cf1d60e4436bfebf52fbf4342ee6e5e18ba05 Mon Sep 17 00:00:00 2001 From: Taketo Takashima Date: Thu, 16 Nov 2023 17:53:32 +0900 Subject: [ruby/ipaddr] Added to_json/as_json method Updated to use cidr method when return address with prefix in #as_json https://github.com/ruby/ipaddr/commit/cf8181d53e --- lib/ipaddr.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib') diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index dbb213c90a..b40d5c0359 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -227,6 +227,22 @@ class IPAddr return str end + # Returns a string containing the IP address representation with prefix. + def as_json(*) + if ipv4? && prefix == 32 + to_s + elsif ipv6? && prefix == 128 + to_s + else + cidr + end + end + + # Returns a json string containing the IP address representation. + def to_json(*) + format("\"%s\"", as_json) + end + # Returns a string containing the IP address representation in # cidr notation def cidr -- cgit v1.2.3