1
1
package openbd
2
2
3
3
import (
4
- "fmt "
4
+ "strconv "
5
5
"strings"
6
6
"time"
7
7
@@ -34,7 +34,10 @@ var timeTemplate3 = []string{
34
34
35
35
//UnmarshalJSON returns result of Unmarshal for json.Unmarshal()
36
36
func (t * Date ) UnmarshalJSON (b []byte ) error {
37
- s := strings .Trim (string (b ), "\" " )
37
+ s := string (b )
38
+ if ss , err := strconv .Unquote (s ); err == nil {
39
+ s = ss
40
+ }
38
41
if len (s ) == 0 || strings .ToLower (s ) == "null" {
39
42
* t = Date {time.Time {}}
40
43
return nil
@@ -44,44 +47,41 @@ func (t *Date) UnmarshalJSON(b []byte) error {
44
47
if strings .Contains (s , ":" ) {
45
48
for _ , tmplt := range timeTemplate1 {
46
49
if tm , err := time .Parse (tmplt , s ); err != nil {
47
- lastErr = err
50
+ lastErr = errs . Wrap ( err , "" , errs . WithParam ( "time_string" , s ), errs . WithParam ( "time_template" , tmplt ))
48
51
} else {
49
52
* t = Date {tm }
50
53
return nil
51
54
}
52
55
}
53
- return errs . Wrap ( lastErr , "error in Date.UnmarshalJSON() function" )
56
+ return lastErr
54
57
}
55
58
for _ , tmplt := range timeTemplate2 {
56
59
if tm , err := time .Parse (tmplt , s ); err != nil {
57
- lastErr = err
60
+ lastErr = errs . Wrap ( err , "" , errs . WithParam ( "time_string" , s ), errs . WithParam ( "time_template" , tmplt ))
58
61
} else {
59
62
* t = Date {tm }
60
63
return nil
61
64
}
62
65
}
63
- return errs . Wrap ( lastErr , "error in Date.UnmarshalJSON() function" )
66
+ return lastErr
64
67
}
65
68
for _ , tmplt := range timeTemplate3 {
66
69
if tm , err := time .Parse (tmplt , s ); err != nil {
67
- lastErr = err
70
+ lastErr = errs . Wrap ( err , "" , errs . WithParam ( "time_string" , s ), errs . WithParam ( "time_template" , tmplt ))
68
71
} else {
69
72
* t = Date {tm }
70
73
return nil
71
74
}
72
75
}
73
- return errs . Wrap ( lastErr , "error in Date.UnmarshalJSON() function" )
76
+ return lastErr
74
77
}
75
78
76
79
//MarshalJSON returns time string with RFC3339 format
77
80
func (t * Date ) MarshalJSON () ([]byte , error ) {
78
- if t == nil {
79
- return []byte ("\" \" " ), nil
80
- }
81
- if t .IsZero () {
81
+ if t == nil || t .IsZero () {
82
82
return []byte ("\" \" " ), nil
83
83
}
84
- return []byte (fmt . Sprintf ( " \" %v \" " , t )), nil
84
+ return []byte (strconv . Quote ( t . String () )), nil
85
85
}
86
86
87
87
func (t Date ) String () string {
0 commit comments