-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_test.go
36 lines (31 loc) · 1 KB
/
json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package orderedmap
import (
"encoding/json"
"testing"
"time"
"github.com/alecthomas/assert/v2"
)
func TestOrderedMap_MarshalJSON(t *testing.T) {
now := time.Now().UTC().Truncate(time.Second)
got, err := json.Marshal(OrderedMap[string, any]{
{"name", "John"},
{"age", 30},
{"active", true},
{"last_access_time", now},
})
assert.NoError(t, err)
assert.Equal(t, `{"name":"John","age":30,"active":true,"last_access_time":"`+now.Format(time.RFC3339Nano)+`"}`, string(got))
}
func TestOrderedMap_UnmarshalJSON(t *testing.T) {
now := time.Now().UTC().Truncate(time.Second)
nowStr := now.Format(time.RFC3339Nano)
var got OrderedMap[string, any]
err := json.Unmarshal([]byte(`{"name":"John","age":30,"active":true,"last_access_time":"`+nowStr+`"}`), &got)
assert.NoError(t, err)
assert.Equal(t, OrderedMap[string, any]{
{"name", "John"},
{"age", float64(30)}, // json always unmarshals numbers as float64
{"active", true},
{"last_access_time", nowStr}, // json doesn't detect time.Time
}, got)
}