Skip to content

Commit 1de030f

Browse files
committed
jsonbytes: add UnpaddedURLBytes
1 parent 42bc272 commit 1de030f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

jsonbytes/unpadded.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,21 @@ func (b *UnpaddedBytes) UnmarshalJSON(data []byte) error {
2828
*b, err = base64.RawStdEncoding.DecodeString(b64str)
2929
return err
3030
}
31+
32+
// UnpaddedURLBytes is a byte slice that is encoded and decoded using
33+
// [base64.RawURLEncoding] instead of the default padded base64.
34+
type UnpaddedURLBytes []byte
35+
36+
func (b UnpaddedURLBytes) MarshalJSON() ([]byte, error) {
37+
return json.Marshal(base64.RawURLEncoding.EncodeToString(b))
38+
}
39+
40+
func (b *UnpaddedURLBytes) UnmarshalJSON(data []byte) error {
41+
var b64str string
42+
err := json.Unmarshal(data, &b64str)
43+
if err != nil {
44+
return err
45+
}
46+
*b, err = base64.RawURLEncoding.DecodeString(b64str)
47+
return err
48+
}

0 commit comments

Comments
 (0)