ooni-probe-cli/internal/engine/sessionresolver/jsoncodec.go
Simone Basso 1153850aca
cleanup: doh.powerdns.org is not working anymore (#924)
While there, `.../internal/sessionresolver` => `.../sessionresolver`

See https://github.com/ooni/probe/issues/2255
2022-09-02 14:44:23 +02:00

38 lines
817 B
Go

package sessionresolver
//
// JSON codec
//
import "encoding/json"
// jsonCodec encodes to/decodes from JSON.
type jsonCodec interface {
// Encode encodes v as a JSON stream of bytes.
Encode(v interface{}) ([]byte, error)
// Decode decodes b from a JSON stream of bytes.
Decode(b []byte, v interface{}) error
}
// codec always returns a valid jsonCodec.
func (r *Resolver) codec() jsonCodec {
if r.jsonCodec != nil {
return r.jsonCodec
}
return &jsonCodecStdlib{}
}
// jsonCodecStdlib is the default codec.
type jsonCodecStdlib struct{}
// Decode implements jsonCodec.Decode.
func (*jsonCodecStdlib) Decode(b []byte, v interface{}) error {
return json.Unmarshal(b, v)
}
// Encode implements jsonCodec.Encode.
func (*jsonCodecStdlib) Encode(v interface{}) ([]byte, error) {
return json.Marshal(v)
}