Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BRUHItsABunny opened this issue Nov 25, 2022 · 0 comments
Open

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

BRUHItsABunny opened this issue Nov 25, 2022 · 0 comments

Comments

@BRUHItsABunny
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant