Enhance JSON unmarshalling in CustomTime types to improve string parsing and add debug logging
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 25s
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 25s
This commit is contained in:
11
mysubaru.go
11
mysubaru.go
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -496,12 +497,13 @@ type CustomTime1 struct {
|
|||||||
func (ct *CustomTime1) UnmarshalJSON(b []byte) (err error) {
|
func (ct *CustomTime1) UnmarshalJSON(b []byte) (err error) {
|
||||||
// Use the correct layout string for the desired format
|
// Use the correct layout string for the desired format
|
||||||
const layout = "2006-01-02T15:04:05"
|
const layout = "2006-01-02T15:04:05"
|
||||||
// s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes
|
fmt.Printf("UnmarshalJSON called with: %s\n", string(b))
|
||||||
|
s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes
|
||||||
if string(b) == "null" {
|
if string(b) == "null" {
|
||||||
ct.Time = time.Time{}
|
ct.Time = time.Time{}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ct.Time, err = time.Parse(layout, string(b)) // Parse the string with your custom layout
|
ct.Time, err = time.Parse(layout, s) // Parse the string with your custom layout
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -514,11 +516,12 @@ type CustomTime2 struct {
|
|||||||
func (ct *CustomTime2) UnmarshalJSON(b []byte) (err error) {
|
func (ct *CustomTime2) UnmarshalJSON(b []byte) (err error) {
|
||||||
// Use the correct layout string for the desired format
|
// Use the correct layout string for the desired format
|
||||||
const layout = "2006-01-02T15:04:05-0700"
|
const layout = "2006-01-02T15:04:05-0700"
|
||||||
// s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes
|
fmt.Printf("UnmarshalJSON called with: %s\n", string(b))
|
||||||
|
s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes
|
||||||
if string(b) == "null" {
|
if string(b) == "null" {
|
||||||
ct.Time = time.Time{}
|
ct.Time = time.Time{}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ct.Time, err = time.Parse(layout, string(b)) // Parse the string with your custom layout
|
ct.Time, err = time.Parse(layout, s) // Parse the string with your custom layout
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user