]> git.lizzy.rs Git - go-anidb.git/commitdiff
misc: Implement json.Marshaler/Unmarshaler in EpisodeList
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Thu, 25 Jul 2013 22:21:50 +0000 (19:21 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Thu, 25 Jul 2013 22:21:50 +0000 (19:21 -0300)
Makes for nicer JSON dumps with anidb-like lists instead of unnecessarily
exposing all the internals.

misc/episodelist.go

index 9bc9036223adb17f4d820c335ac6f62367c8a702..a0fb2a0660efd018ee2166d3496fc00864dfe7a9 100644 (file)
@@ -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
+}