diff options
Diffstat (limited to 'lib/ipaddr.rb')
-rw-r--r-- | lib/ipaddr.rb | 16 |
1 files changed, 16 insertions, 0 deletions
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 |