Skip to content

Commit

Permalink
feat: add jsonpb marshallers (#3165)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasefuchs authored Oct 12, 2024
1 parent 229b30d commit 6a242d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions common/net/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,9 @@ func (d *IPOrDomain) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []by
d.Address = result.Address
return nil
}

func (d *IPOrDomain) MarshalJSONPB(marshaler *jsonpb.Marshaler) ([]byte, error) {
ipod := d.AsAddress().String()

return json.Marshal(ipod)
}
10 changes: 10 additions & 0 deletions common/net/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ func (nl *NetworkList) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []
return newError("unknown format of a string list: " + string(bytes))
}

func (nl *NetworkList) MarshalJSONPB(marshaler *jsonpb.Marshaler) ([]byte, error) {
networkStrList := make([]string, len(nl.Network))

for idx, network := range nl.Network {
networkStrList[idx] = network.String()
}

return json.Marshal(networkStrList)
}

// HasNetwork returns true if the network list has a certain network.
func HasNetwork(list []Network, network Network) bool {
for _, value := range list {
Expand Down
6 changes: 6 additions & 0 deletions proxy/shadowsocks/simplified/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func (c *CipherTypeWrapper) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, byt
return nil
}

func (c *CipherTypeWrapper) MarshalJSONPB(marshaler *jsonpb.Marshaler) ([]byte, error) {
method := c.Value.String()

return json.Marshal(method)
}

func init() {
common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
simplifiedServer := config.(*ServerConfig)
Expand Down

0 comments on commit 6a242d1

Please sign in to comment.