Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support IPAddr class #504

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions json.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Gem::Specification.new do |s|
"lib/json/add/date.rb",
"lib/json/add/date_time.rb",
"lib/json/add/exception.rb",
"lib/json/add/ipaddr.rb",
"lib/json/add/ostruct.rb",
"lib/json/add/range.rb",
"lib/json/add/rational.rb",
Expand Down
1 change: 1 addition & 0 deletions json_pure.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |s|
"lib/json/add/date.rb".freeze,
"lib/json/add/date_time.rb".freeze,
"lib/json/add/exception.rb".freeze,
"lib/json/add/ipaddr.rb".freeze,
"lib/json/add/ostruct.rb".freeze,
"lib/json/add/range.rb".freeze,
"lib/json/add/rational.rb".freeze,
Expand Down
8 changes: 8 additions & 0 deletions lib/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@
# - Date: <tt>require 'json/add/date'</tt>
# - DateTime: <tt>require 'json/add/date_time'</tt>
# - Exception: <tt>require 'json/add/exception'</tt>
# - IPAddr: <tt>require 'json/add/ipaddr'</tt>
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
# - Range: <tt>require 'json/add/range'</tt>
# - Rational: <tt>require 'json/add/rational'</tt>
Expand Down Expand Up @@ -451,6 +452,13 @@
# ruby1 = JSON.parse(json, create_additions: true) # Another message
# ruby1.class # RuntimeError
#
# \IPAddr:
# require 'json/add/ipaddr'
# ruby0 = IPAddr.new('127.0.0.1') # #<IPAddr: IPv4:127.0.0.1/255.255.255.255>
# json = JSON.generate(ruby0) # {"json_class":"IPAddr","a":127.0.0.1,"p":32}
# ruby1 = JSON.parse(json, create_additions: true) # #<IPAddr: IPv4:127.0.0.1/255.255.255.255>
# ruby1.class # IPAddr
#
# \OpenStruct:
# require 'json/add/ostruct'
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
Expand Down
1 change: 1 addition & 0 deletions lib/json/add/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'json/add/date'
require 'json/add/date_time'
require 'json/add/exception'
require 'json/add/ipaddr'
require 'json/add/range'
require 'json/add/regexp'
require 'json/add/struct'
Expand Down
57 changes: 57 additions & 0 deletions lib/json/add/ipaddr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#frozen_string_literal: false
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json'
end
require 'ipaddr'

class IPAddr

# Deserializes JSON string by constructing new IPAddr object with address
# <tt>a</tt> and prefix <tt>p</tt> serialized with <tt>to_json</tt>
def self.json_create(object)
addr = IPAddr.new(object['a'])
addr.mask(object['p'])
addr
end

# Returns a hash, that will be turned into a JSON object and represent this
# object.
def as_json(*)
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.5.0")
prefix = prefixlen
else
prefix = self.prefix
end

{
JSON.create_id => self.class.name,
'a' => to_s,
'p' => prefix,
}
end

# Stores class name (IPAddr) with address <tt>a</tt> and prefix
# <tt>p</tt> as JSON string
def to_json(*args)
as_json.to_json(*args)
end

# for ruby version <= 2.4
def prefixlen
case @family
when Socket::AF_INET
n = IN4MASK ^ @mask_addr
i = 32
when Socket::AF_INET6
n = IN6MASK ^ @mask_addr
i = 128
else
raise AddressFamilyError, "unsupported address family"
end
while n.positive?
n >>= 1
i -= 1
end
i
end
end
7 changes: 7 additions & 0 deletions tests/json_addition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'json/add/ostruct'
require 'json/add/set'
require 'date'
require 'ipaddr'

class JSONAdditionTest < Test::Unit::TestCase
include JSON
Expand Down Expand Up @@ -196,4 +197,10 @@ def test_set
s = Set.new([:a, :b, :c, :a])
assert_equal s, JSON.parse(JSON(s), :create_additions => true)
end

def test_ipaddr
assert_equal IPAddr.new('127.0.0.1'), parse(JSON(IPAddr.new('127.0.0.1')), :create_additions => true)
assert_equal IPAddr.new('192.0.2.0/24'), parse(JSON(IPAddr.new('192.0.2.0/24')), :create_additions => true)
assert_equal IPAddr.new('2001:db8::/48'), parse(JSON(IPAddr.new('2001:db8::/48')), :create_additions => true)
end
end