Skip to content

Commit

Permalink
Add Json marshaling and unmarshaling (#3)
Browse files Browse the repository at this point in the history
* Add Json marshaling and unmarshaling
* Fix MarshalJSON test failure
  • Loading branch information
arieroos authored Oct 12, 2023
1 parent 3054e00 commit 21da944
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
24 changes: 24 additions & 0 deletions duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package duration

import (
"errors"
"fmt"
"math"
"strconv"
"time"
Expand Down Expand Up @@ -275,3 +276,26 @@ func (duration *Duration) String() string {

return d
}

func (duration Duration) MarshalJSON() ([]byte, error) {
return []byte("\"" + duration.String() + "\""), nil
}

func (duration *Duration) UnmarshalJSON(source []byte) error {
strVal := string(source)
if len(strVal) < 2 {
return fmt.Errorf("invalid ISO 8601 duration: %s", strVal)
}
strVal = strVal[1 : len(strVal)-1]

if strVal == "null" {
return nil
}

parsed, err := Parse(strVal)
if err != nil {
return fmt.Errorf("invalid ISO 8601 duration: %s", strVal)
}
*duration = *parsed
return nil
}
62 changes: 62 additions & 0 deletions duration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duration

import (
"encoding/json"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -262,3 +263,64 @@ func TestDuration_String(t *testing.T) {
t.Errorf("expected: %s, got: %s", "-PT2H5M", negativeDuration.String())
}
}

func TestDuration_MarshalJSON(t *testing.T) {
td, err := Parse("P3Y6M4DT12H30M5.5S")
if err != nil {
t.Fatal(err)
}

jsonVal, err := json.Marshal(struct {
Dur *Duration `json:"d"`
}{Dur: td})
if err != nil {
t.Errorf("did not expect error: %s", err.Error())
}
if string(jsonVal) != `{"d":"P3Y6M4DT12H30M5.5S"}` {
t.Errorf("expected: %s, got: %s", `{"d":"P3Y6M4DT12H30M5.5S"}`, string(jsonVal))
}

jsonVal, err = json.Marshal(struct {
Dur Duration `json:"d"`
}{Dur: *td})
if err != nil {
t.Errorf("did not expect error: %s", err.Error())
}
if string(jsonVal) != `{"d":"P3Y6M4DT12H30M5.5S"}` {
t.Errorf("expected: %s, got: %s", `{"d":"P3Y6M4DT12H30M5.5S"}`, string(jsonVal))
}
}

func TestDuration_UnmarshalJSON(t *testing.T) {
jsonStr := `
{
"d": "P3Y6M4DT12H30M5.5S"
}
`
expected, err := Parse("P3Y6M4DT12H30M5.5S")
if err != nil {
t.Fatal(err)
}

var durStructPtr struct {
Dur *Duration `json:"d"`
}
err = json.Unmarshal([]byte(jsonStr), &durStructPtr)
if err != nil {
t.Errorf("did not expect error: %s", err.Error())
}
if !reflect.DeepEqual(durStructPtr.Dur, expected) {
t.Errorf("JSON Unmarshal ptr got = %s, want %s", durStructPtr.Dur, expected)
}

var durStruct struct {
Dur Duration `json:"d"`
}
err = json.Unmarshal([]byte(jsonStr), &durStruct)
if err != nil {
t.Errorf("did not expect error: %s", err.Error())
}
if !reflect.DeepEqual(durStruct.Dur, *expected) {
t.Errorf("JSON Unmarshal ptr got = %s, want %s", &(durStruct.Dur), expected)
}
}

0 comments on commit 21da944

Please sign in to comment.