Skip to content

Commit

Permalink
Fix IntNullable and BoolNullable
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Kadolph committed Oct 24, 2012
1 parent 69c2eb7 commit e4d5066
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
29 changes: 17 additions & 12 deletions jsonencode.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,15 @@ func (i *Int64Nullable) UnmarshalJSON(data []byte) error {

type IntNullable int

func (i IntNullable) UnmarshalJSON(data []byte) error {
if len(data) > 0 && string(data) != "null" {
if in, err := strconv.ParseInt(string(data), 10, 32); err == nil {
i = IntNullable(in)
}
func (i *IntNullable) UnmarshalJSON(data []byte) error {
if len(data) == 0 || string(data) == "null" {
return nil
}

if in, err := strconv.ParseInt(string(data), 10, 32); err != nil {
return err
} else {
*i = IntNullable(in)
}
return nil
}
Expand All @@ -197,13 +200,15 @@ func (s *StringNullable) UnmarshalJSON(data []byte) error {

type BoolNullable bool

func (b BoolNullable) UnmarshalJSON(data []byte) error {
ds := string(data)
if len(data) > 0 {
//ParseBool(str string) (value bool, err error)
if bo, err := strconv.ParseBool(ds); err == nil {
b = BoolNullable(bo)
}
func (b *BoolNullable) UnmarshalJSON(data []byte) error {
if len(data) == 0 || string(data) == "null" {
return nil
}

if bo, err := strconv.ParseBool(string(data)); err != nil {
return err
} else {
*b = BoolNullable(bo)
}
return nil
}
12 changes: 6 additions & 6 deletions twittertypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type User struct {
Description StringNullable `json:"description"`
FavouritesCount int `json:"favourites_count"`
Followerscount int `json:"followers_count"`
Following BoolNullable // "following":null,
Following *BoolNullable // "following":null,
Friendscount int `json:"friends_count"`
Geo_enabled bool
Lang string
Expand All @@ -34,9 +34,9 @@ type User struct {
Statuses_Count int `json:"statuses_count"`
Time_zone StringNullable
Url StringNullable // "url":null
Utc_offset IntNullable // "utc_offset":null,
Utc_offset *IntNullable // "utc_offset":null,
Verified bool
ShowAllInlineMedia BoolNullable `json:"show_all_inline_media"`
ShowAllInlineMedia *BoolNullable `json:"show_all_inline_media"`
//"default_profile":false,
//"follow_request_sent":null,
//"is_translator":false,
Expand All @@ -58,11 +58,11 @@ type Tweet struct {
Id_str string
Created_at string
Retweet_Count int32
Retweeted BoolNullable
Possibly_Sensitive BoolNullable
Retweeted *BoolNullable
Possibly_Sensitive *BoolNullable
User *User
RawBytes []byte
Truncated BoolNullable
Truncated *BoolNullable
//Geo string // deprecated
//Place // "place":null,
//RetweetedStatus Tweet `json:"retweeted_status"`
Expand Down

0 comments on commit e4d5066

Please sign in to comment.