Conversation
|
|
||
| # Returns a json string containing the IP address representation. | ||
| def to_json(*args) | ||
| format("\"%s\"", as_json) |
There was a problem hiding this comment.
Was your intent as_json(*args) because you gave it a name?
There was a problem hiding this comment.
@knu It is not necessary a name. I have removed a name of args.
It is from another patch which is ruby/json@f1ffc4a
There was a problem hiding this comment.
I prefer the implementation of to_json to be as_json.to_json(*args) so it is easier to change how IPAddrs are encoded simply by overriding as_json. With the current definition, when you want it to be def as_json(*) = to_f you'll need to rewrite to_json as well.
Updated to use cidr method when return address with prefix in #as_json
8473b87 to
cf8181d
Compare
knu
left a comment
There was a problem hiding this comment.
Forget about to_f, I think it mistook this for something else. In any case, I like how JSON::GenericObject defines to_json. I'll change it as appropriate later. Thanks for contributing!
|
The json library defines the generic to_json in Object, so we may not have to implement one in IPAddr. |
Because when use Expected behaviorrequire 'ipaddr'
require 'json'
IPAddr.new('172.16.0.0/24').to_json #=> "\"172.16.0.0/24\""Actual behaviorrequire 'ipaddr'
require 'json'
IPAddr.new('172.16.0.0/24').to_json #=> "\"172.16.0.0\"" |
Add
to_json/as_jsonmethods to get json format string.When use json gem,
IPAddr#to_jsonremove prefix address with CIDR.Although json gem have no paln to support for IPAddr class.
ruby/json#503
Actual behavior
This PR behavior
IPAddr#to_jsonmethod return json format address wit prefix without json gem.(When to require json gem, it have same behavior.)