-
Notifications
You must be signed in to change notification settings - Fork 526
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
Support custom JSON unmarshaling and json.Unmarshaler interface in decoding #42
Comments
Can you please let me know your scenario? Why do you need to do custom JSON unmarshalling? |
I'm requesting user's feed I couldn't think of any better solution other than break my struct in two: one that has not null fields, and second that embeds first struct plus field All this feels very clunky, I think it would be pretty convenient to implement |
I see. If this package provides an interface |
I guess that would resolve the problem. |
Fine. I'll implement it soon. |
@Perlence I implemented it in latest master code. Please take a look and have a try. |
Thank you! When I implemented type FeedResult struct {
Data []FacebookPost `json:"data"`
Paging Paging `json:"paging"`
}
type FacebookPost struct {
ID string `json:"id" db:"id"`
EntryID int64 `json:"entry_id" db:"entry_id"`
Message string `json:"message" db:"message"`
Story string `json:"story" db:"story"`
CreatedTime pq.NullTime `json:"created_time" db:"created_time"`
}
func (p *FacebookPost) UnmarshalJSON(data []byte) error {
var aux struct {
ID string `json:"id" db:"id"`
Message string `json:"message" db:"message"`
Story string `json:"story" db:"story"`
CreatedTime NullTime `json:"created_time" db:"created_time"`
}
...
}
type NullTime struct{ pq.NullTime }
func (nt *NullTime) UnmarshalJSON(data []byte) error {
t, err := time.Parse(dateFmt, string(data))
if err != nil {
return errors.Trace(err)
}
nt.Time = t
nt.Valid = !t.IsZero()
return nil
} Yeah, I know there's type FeedResult struct {
Data []FacebookPost `json:"data"`
Paging Paging `json:"paging"`
}
type FacebookPost struct {
ID string `json:"id" db:"id"`
EntryID int64 `json:"entry_id" db:"entry_id"`
Message string `json:"message" db:"message"`
Story string `json:"story" db:"story"`
CreatedTime NullTime `json:"created_time" db:"created_time"`
}
type NullTime struct{ pq.NullTime }
func (nt *NullTime) UnmarshalJSON(data []byte) error {
...
} I get error "field 'data.0.created_time' is not a json object in result." I guess, that's expected. |
It's a bug in my latest commit. I'll fix it soon. |
@Perlence Please take a look at latest master code again. Thanks. |
Yep, it works, thanks 😄 |
Glad to hear it. 😄 |
Can I have a
http.Response
as result of GET request? I want to do custom JSON unmarshalling.The text was updated successfully, but these errors were encountered: