From 36513d4077c4ebe1648a87e87045d88aa25fe787 Mon Sep 17 00:00:00 2001 From: David Petrovics Date: Thu, 3 Nov 2016 21:01:16 -0700 Subject: [PATCH] Custom deserializer for Application --- application.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/application.go b/application.go index f3e2be305a..6a381630a3 100644 --- a/application.go +++ b/application.go @@ -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 +}