config_json.go 793 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // +build json
  2. package transport
  3. import (
  4. "encoding/json"
  5. "strings"
  6. )
  7. func (this *Config) UnmarshalJSON(data []byte) error {
  8. type TypeConfig struct {
  9. StreamType string `json:"streamType"`
  10. Settings json.RawMessage `json:"settings"`
  11. }
  12. type JsonTCPConfig struct {
  13. ConnectionReuse bool `json:"connectionReuse"`
  14. }
  15. typeConfig := new(TypeConfig)
  16. if err := json.Unmarshal(data, typeConfig); err != nil {
  17. return err
  18. }
  19. this.StreamType = StreamTypeTCP
  20. streamType := strings.ToLower(typeConfig.StreamType)
  21. if streamType == "tcp" {
  22. jsonTCPConfig := new(JsonTCPConfig)
  23. if err := json.Unmarshal(typeConfig.Settings, jsonTCPConfig); err != nil {
  24. return err
  25. }
  26. this.TCPConfig = &TCPConfig{
  27. ConnectionReuse: jsonTCPConfig.ConnectionReuse,
  28. }
  29. }
  30. return nil
  31. }