json-iterator / java

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
http://jsoniter.com/
MIT License
1.5k stars 519 forks source link

Unable to unmarshal Date using json-iterator #209

Closed aafreensheikh96 closed 5 years ago

aafreensheikh96 commented 5 years ago

//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)

}