Skip to content

atomic.Time not being marshaled when calling json.Marshal #124

@BRUHItsABunny

Description

@BRUHItsABunny
func TestTimeMarshal(t *testing.T) {
	type Test struct {
		Field1 time.Time
		Field2 *atomic.Time
	}

	obj := &Test{Field1: time.Now()}
	obj.Field2 = atomic.NewTime(obj.Field1)
	jsonBytes, err := json.Marshal(obj)
	if err != nil {
		t.Error(err)
	}

	tmpMap := make(map[string]interface{})
	err = json.Unmarshal(jsonBytes, &tmpMap)
	if err != nil {
		t.Error(err)
	}

	fmt.Println(fmt.Sprintf("JSON:\n%s", string(jsonBytes)))
	if tmpMap["Field1"] != tmpMap["Field2"] { // Field2 is not even a string as of right now
		t.Error("Marshaling not equal")
	}
}

Output:

=== RUN   TestTimeMarshal
JSON:
{"Field1":"2022-11-25T21:28:20.1068541+01:00","Field2":{}}
    utils_test.go:143: Marshaling not equal
--- FAIL: TestTimeMarshal (0.00s)

FAIL

As demonstrated, Go's std time.Time is able to marshal to JSON but atomic.Time doesn't do the same.
Was there a reason behind this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions