address_json.go 401 B

1234567891011121314151617181920212223242526
  1. // +build json
  2. package net
  3. import (
  4. "encoding/json"
  5. "net"
  6. )
  7. type AddressJson struct {
  8. Address Address
  9. }
  10. func (this *AddressJson) UnmarshalJSON(data []byte) error {
  11. var rawStr string
  12. if err := json.Unmarshal(data, &rawStr); err != nil {
  13. return err
  14. }
  15. ip := net.ParseIP(rawStr)
  16. if ip != nil {
  17. this.Address = IPAddress(ip)
  18. } else {
  19. this.Address = DomainAddress(rawStr)
  20. }
  21. return nil
  22. }