From a455733f8b4567c25690eaaffefbc894551490af Mon Sep 17 00:00:00 2001 From: Alex Savin Date: Tue, 8 Jul 2025 22:21:02 -0400 Subject: [PATCH] Enhance JSON unmarshalling for CustomTime types to handle null values and improve string parsing --- mysubaru.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mysubaru.go b/mysubaru.go index dc4311a..256e360 100644 --- a/mysubaru.go +++ b/mysubaru.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log/slog" + "strings" "time" ) @@ -496,6 +497,11 @@ type CustomTime1 struct { func (ct *CustomTime1) UnmarshalJSON(b []byte) (err error) { // Use the correct layout string for the desired format const layout = "2006-01-02T15:04:05" + s := strings.Trim(string(b), `"`) // Remove surrounding quotes + if s == "null" { + ct.Time = time.Time{} + return nil + } ct.Time, err = time.Parse(layout, string(b)) return } @@ -507,7 +513,13 @@ type CustomTime2 struct { // UnmarshalJSON implements the json.Unmarshaler interface func (ct *CustomTime2) UnmarshalJSON(b []byte) (err error) { + // Use the correct layout string for the desired format const layout = "2006-01-02T15:04:05-0700" - ct.Time, err = time.Parse(layout, string(b)) // Parse the string using the custom layout + s := strings.Trim(string(b), `"`) // Remove surrounding quotes + if s == "null" { + ct.Time = time.Time{} + return nil + } + ct.Time, err = time.Parse(layout, s) // Parse the string with your custom layout return }