Skip to content

Unable to unmarshal Date using json-iterator #209

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

Closed
aafreensheikh96 opened this issue Oct 15, 2018 · 0 comments
Closed

Unable to unmarshal Date using json-iterator #209

aafreensheikh96 opened this issue Oct 15, 2018 · 0 comments

Comments

@aafreensheikh96
Copy link

//This is an example for "encoding/json" which can parse a date field
package main

import (
"encoding/json"
"errors"
"fmt"
"time"

"IAS-01.redbus.in/modules/rbgo/jsonutil"

)

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 {

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.

import (
"errors"
"fmt"
"time"

"github.com/json-iterator/go"

)

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 {

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)

}

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