Created
January 17, 2019 06:20
-
-
Save wking/5db259cf807c1445eee5eff0a1a36e86 to your computer and use it in GitHub Desktop.
IPNet (de)serialization comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go run test.go | |
serialize JSON | |
their JSON: {"IP":"192.168.0.10","Mask":"////AA=="} | |
our JSON: "192.168.0.10/24" | |
serialize String | |
their String: 192.168.0.10/24 | |
our String: 192.168.0.10/24 | |
deserialize JSON | |
from their JSON: 192.168.0.10/24 | |
from our JSON: 192.168.0.10/24 | |
deserialize ParseCIDR | |
their ParseCIDR: 192.168.0.0/24 | |
our ParseCIDR: 192.168.0.0/24 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go run test.go | |
serialize JSON | |
their JSON: {"IP":"192.168.0.10","Mask":"////AA=="} | |
our JSON: "192.168.0.10/24" | |
serialize String | |
their String: 192.168.0.10/24 | |
our String: 192.168.0.10/24 | |
deserialize JSON | |
from their JSON: 192.168.0.10/24 | |
from our JSON: 192.168.0.0/24 | |
deserialize ParseCIDR | |
their ParseCIDR: 192.168.0.0/24 | |
our ParseCIDR: 192.168.0.0/24 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net" | |
"github.com/openshift/installer/pkg/ipnet" | |
) | |
func main() { | |
theirIPNet := &net.IPNet{ | |
IP: net.IP{192, 168, 0, 10}, | |
Mask: net.IPv4Mask(255, 255, 255, 0), | |
} | |
cidr := theirIPNet.String() | |
ourIPNet := &ipnet.IPNet{IPNet: *theirIPNet} | |
fmt.Printf("serialize JSON\n") | |
theirJSON, _ := json.Marshal(theirIPNet) | |
ourJSON, _ := json.Marshal(ourIPNet) | |
fmt.Printf("their JSON: %s\n", string(theirJSON)) | |
fmt.Printf("our JSON: %s\n", string(ourJSON)) | |
fmt.Printf("\nserialize String\n") | |
fmt.Printf("their String: %s\n", theirIPNet) | |
fmt.Printf("our String: %s\n", ourIPNet) | |
json.Unmarshal(theirJSON, &theirIPNet) | |
json.Unmarshal(ourJSON, &ourIPNet) | |
fmt.Printf("\ndeserialize JSON\n") | |
fmt.Printf("from their JSON: %s\n", theirIPNet) | |
fmt.Printf("from our JSON: %s\n", ourIPNet) | |
_, theirIPNet, _ = net.ParseCIDR(cidr) | |
ourIPNet, _ = ipnet.ParseCIDR(cidr) | |
fmt.Printf("\ndeserialize ParseCIDR\n") | |
fmt.Printf("their ParseCIDR: %s\n", theirIPNet) | |
fmt.Printf("our ParseCIDR: %s\n", ourIPNet) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment