Skip to content

Commit

Permalink
Custom deserializer for Application
Browse files Browse the repository at this point in the history
  • Loading branch information
dpetrovics-stripe committed Nov 4, 2016
1 parent 57f5c33 commit 36513d4
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions application.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package stripe

import "encoding/json"

type Application struct {
ID string `json:"id"`
Name string `json:"name"`
}

// UnmarshalJSON handles deserialization of an Application.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (a *Application) UnmarshalJSON(data []byte) error {
type application Application
var aa application
err := json.Unmarshal(data, &aa)
if err == nil {
*a = Application(aa)
} else {
// the id is surrounded by "\" characters, so strip them
a.ID = string(data[1 : len(data)-1])
}

return nil
}

0 comments on commit 36513d4

Please sign in to comment.