config_json.go 669 B

123456789101112131415161718192021222324252627
  1. // +build json
  2. package freedom
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. )
  8. func (this *Config) UnmarshalJSON(data []byte) error {
  9. type JsonConfig struct {
  10. DomainStrategy string `json:"domainStrategy"`
  11. Timeout uint32 `json:"timeout"`
  12. }
  13. jsonConfig := new(JsonConfig)
  14. if err := json.Unmarshal(data, jsonConfig); err != nil {
  15. return errors.New("Freedom: Failed to parse config: " + err.Error())
  16. }
  17. this.DomainStrategy = Config_AS_IS
  18. domainStrategy := strings.ToLower(jsonConfig.DomainStrategy)
  19. if domainStrategy == "useip" || domainStrategy == "use_ip" {
  20. this.DomainStrategy = Config_USE_IP
  21. }
  22. this.Timeout = jsonConfig.Timeout
  23. return nil
  24. }