You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// IST is the Indian Standard Time *time.Location.
var IST, _ = time.LoadLocation("Asia/Kolkata")
var (
// dateFormats are used to parse date from JSON. When
// UnmarshalJSON is called, string([]byte) returns date surrounded
// by quotes. So even our format has it.
dateFormats = []string{DateFmt, DateTimeFmt, DateTimeNanoFmt}
// hackDateFormats are used to parse and map loose date formats to their
// correct equivalent one. It was added for backward compatibility.
hackDateFormats = map[string]string{"2006-1-2": DateFmt}
)
type Date struct {
time.Time
fmt string
}
func (d *Date) UnmarshalJSON(b []byte) error {
dateStr := string(b) // something like `"2017-08-20"`
// Ignore null, like in the main JSON package.
if dateStr == "null" {
return nil
}
for _, format := range dateFormats {
if t, err := time.ParseInLocation(format, dateStr, IST); err == nil {
d.Time, d.fmt = t, format
return nil
}
}
for hackFmt, format := range hackDateFormats {
if t, err := time.ParseInLocation(hackFmt, dateStr, IST); err == nil {
d.Time, d.fmt = t, format
return nil
}
}
return errors.New("cant parse date")
}
func main() {
data := []byte(`{
"2018-12-12": true,
"2018-12-13": true,
"2018-12-14": true
}`)
v := map[jsonutil.Date]bool{}
if err := json.Unmarshal(data, &v); err != nil {
panic(err)
}
fmt.Printf("%#v\n", v)
}
//This is an example for "github.com/json-iterator/go" which is unable to parse date.
// IST is the Indian Standard Time *time.Location.
var IST, _ = time.LoadLocation("Asia/Kolkata")
var (
// dateFormats are used to parse date from JSON. When
// UnmarshalJSON is called, string([]byte) returns date surrounded
// by quotes. So even our format has it.
dateFormats = []string{DateFmt, DateTimeFmt, DateTimeNanoFmt}
// hackDateFormats are used to parse and map loose date formats to their
// correct equivalent one. It was added for backward compatibility.
hackDateFormats = map[string]string{"2006-1-2": DateFmt}
)
type Date struct {
time.Time
fmt string
}
func (d *Date) UnmarshalJSON(b []byte) error {
dateStr := string(b) // something like `"2017-08-20"`
// Ignore null, like in the main JSON package.
if dateStr == "null" {
return nil
}
for _, format := range dateFormats {
if t, err := time.ParseInLocation(format, dateStr, IST); err == nil {
d.Time, d.fmt = t, format
return nil
}
}
for hackFmt, format := range hackDateFormats {
if t, err := time.ParseInLocation(hackFmt, dateStr, IST); err == nil {
d.Time, d.fmt = t, format
return nil
}
}
return errors.New("cant parse date")
}
func main() {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
data := []byte(`{
"2018-12-12": true,
"2018-12-13": true,
"2018-12-14": true
}`)
v := map[Date]bool{}
if err := json.Unmarshal(data, &v); err != nil {
panic(err)
}
fmt.Printf("%#v\n", v)
}
The text was updated successfully, but these errors were encountered:
//This is an example for "encoding/json" which can parse a date field
package main
import (
"encoding/json"
"errors"
"fmt"
"time"
)
const (
DateFmt =
"2006-01-02"
DateTimeFmt =
"2006-01-02T15:04"
DateTimeNanoFmt =
"2006-01-02T15:04:05"
)
// IST is the Indian Standard Time
*time.Location
.var IST, _ = time.LoadLocation("Asia/Kolkata")
var (
// dateFormats are used to parse date from JSON. When
// UnmarshalJSON is called,
string([]byte)
returns date surrounded// by quotes. So even our format has it.
dateFormats = []string{DateFmt, DateTimeFmt, DateTimeNanoFmt}
// hackDateFormats are used to parse and map loose date formats to their
// correct equivalent one. It was added for backward compatibility.
hackDateFormats = map[string]string{
"2006-1-2"
: DateFmt})
type Date struct {
time.Time
fmt string
}
func (d *Date) UnmarshalJSON(b []byte) error {
}
func main() {
}
//This is an example for "github.com/json-iterator/go" which is unable to parse date.
import (
"errors"
"fmt"
"time"
)
const (
DateFmt =
"2006-01-02"
DateTimeFmt =
"2006-01-02T15:04"
DateTimeNanoFmt =
"2006-01-02T15:04:05"
)
// IST is the Indian Standard Time
*time.Location
.var IST, _ = time.LoadLocation("Asia/Kolkata")
var (
// dateFormats are used to parse date from JSON. When
// UnmarshalJSON is called,
string([]byte)
returns date surrounded// by quotes. So even our format has it.
dateFormats = []string{DateFmt, DateTimeFmt, DateTimeNanoFmt}
// hackDateFormats are used to parse and map loose date formats to their
// correct equivalent one. It was added for backward compatibility.
hackDateFormats = map[string]string{
"2006-1-2"
: DateFmt})
type Date struct {
time.Time
fmt string
}
func (d *Date) UnmarshalJSON(b []byte) error {
}
func main() {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
}
The text was updated successfully, but these errors were encountered: