Skip to content

Commit

Permalink
User MarshalJSON and UnmarshalJSON directly (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Jul 9, 2021
1 parent 8c2d44d commit 74872d2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
7 changes: 3 additions & 4 deletions audience_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package jwt

import (
"encoding/json"
"testing"
)

func TestAudienceMarshal(t *testing.T) {
f := func(got Audience, want string) {
t.Helper()

raw, err := json.Marshal(got)
raw, err := got.MarshalJSON()
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
Expand All @@ -30,7 +29,7 @@ func TestAudienceUnmarshal(t *testing.T) {
t.Helper()

var a Audience
err := json.Unmarshal([]byte(got), &a)
err := a.UnmarshalJSON([]byte(got))
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
Expand All @@ -56,7 +55,7 @@ func TestAudienceUnmarshalMalformed(t *testing.T) {
t.Helper()

var a Audience
err := json.Unmarshal([]byte(got), &a)
err := a.UnmarshalJSON([]byte(got))
if err == nil {
t.Error("want err")
}
Expand Down
2 changes: 1 addition & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func encodeHeader(header Header) []byte {
// another algorithm? encode below
}
// returned err is always nil, see *Header.MarshalJSON
buf, _ := json.Marshal(header)
buf, _ := header.MarshalJSON()

encoded := make([]byte, b64EncodedLen(len(buf)))
b64Encode(encoded, buf)
Expand Down
3 changes: 1 addition & 2 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jwt

import (
"encoding/base64"
"encoding/json"
"strings"
"testing"
)
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestMarshalHeader(t *testing.T) {
f := func(h *Header, want string) {
t.Helper()

raw, err := json.Marshal(h)
raw, err := h.MarshalJSON()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions numeric_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func NewNumericDate(t time.Time) *NumericDate {

// MarshalJSON implements the json.Marshaler interface.
func (t *NumericDate) MarshalJSON() ([]byte, error) {
if t == nil || t.IsZero() {
return []byte("null"), nil
}
return []byte(strconv.FormatInt(t.Unix(), 10)), nil
}

Expand Down
7 changes: 3 additions & 4 deletions numeric_date_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jwt

import (
"encoding/json"
"strconv"
"testing"
"time"
Expand All @@ -11,7 +10,7 @@ func TestNumericDateMarshal(t *testing.T) {
f := func(got *NumericDate, want string) {
t.Helper()

raw, err := json.Marshal(got)
raw, err := got.MarshalJSON()
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
Expand All @@ -33,7 +32,7 @@ func TestNumericDateUnmarshal(t *testing.T) {
t.Helper()

var got NumericDate
err := json.Unmarshal([]byte(s), &got)
err := got.UnmarshalJSON([]byte(s))
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
Expand All @@ -52,7 +51,7 @@ func TestNumericDateUnmarshalMalformed(t *testing.T) {
t.Helper()

var nd NumericDate
err := json.Unmarshal([]byte(got), &nd)
err := nd.UnmarshalJSON([]byte(got))
if err == nil {
t.Error("want err")
}
Expand Down

0 comments on commit 74872d2

Please sign in to comment.