From 6accd34da4a57a8f61a4abf009eed8540044e3ac Mon Sep 17 00:00:00 2001 From: "Diogo Franco (Kovensky)" Date: Thu, 25 Jul 2013 19:21:50 -0300 Subject: [PATCH] misc: Implement json.Marshaler/Unmarshaler in EpisodeList Makes for nicer JSON dumps with anidb-like lists instead of unnecessarily exposing all the internals. --- misc/episodelist.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/misc/episodelist.go b/misc/episodelist.go index 9bc9036..a0fb2a0 100644 --- a/misc/episodelist.go +++ b/misc/episodelist.go @@ -1,6 +1,7 @@ package misc import ( + "encoding/json" "fmt" "sort" "strings" @@ -280,3 +281,24 @@ func (el *EpisodeList) Sub(ec EpisodeContainer) { } *el = append(*el, el2.Simplify()...) } + +// Equivalent to marshaling el.String() +func (el EpisodeList) MarshalJSON() ([]byte, error) { + return json.Marshal(el.String()) +} + +// NOTE: Since the String() representation doesn't include them, +// it's not exactly reversible if the user has set .Parts in any +// of the contained episodes. +func (el EpisodeList) UnmarshalJSON(b []byte) error { + var v string + if err := json.Unmarshal(b, &v); err != nil { + return err + } + + l := ParseEpisodeList(v) + for k := range l { + el[k] = l[k] + } + return nil +} -- 2.44.0