mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-05-01 01:44:15 +00:00
Config: Implement missing MarshalJSON for structs having custom UnmarshalJSON (#4585)
* conf: implement MarshalJSON for FakeDNSConfig * conf: Rewrite MarshalJSON for PortList decouple PortRange from PortList. * conf: implement MarshalJSON for HostAddress * conf: Add MarshalJSON comments and use pointers.
This commit is contained in:
parent
0dbab7bcd7
commit
2d3126b752
4 changed files with 63 additions and 11 deletions
|
@ -25,6 +25,7 @@ type NameServerConfig struct {
|
|||
TimeoutMs uint64 `json:"timeoutMs"`
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
||||
func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
|
||||
var address Address
|
||||
if err := json.Unmarshal(data, &address); err == nil {
|
||||
|
@ -163,6 +164,18 @@ type HostAddress struct {
|
|||
addrs []*Address
|
||||
}
|
||||
|
||||
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
|
||||
func (h *HostAddress) MarshalJSON() ([]byte, error) {
|
||||
if (h.addr != nil) != (h.addrs != nil) {
|
||||
if h.addr != nil {
|
||||
return json.Marshal(h.addr)
|
||||
} else if h.addrs != nil {
|
||||
return json.Marshal(h.addrs)
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unexpected config state")
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
||||
func (h *HostAddress) UnmarshalJSON(data []byte) error {
|
||||
addr := new(Address)
|
||||
|
@ -208,6 +221,11 @@ func getHostMapping(ha *HostAddress) *dns.Config_HostMapping {
|
|||
}
|
||||
}
|
||||
|
||||
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
|
||||
func (m *HostsWrapper) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m.Hosts)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
||||
func (m *HostsWrapper) UnmarshalJSON(data []byte) error {
|
||||
hosts := make(map[string]*HostAddress)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue