From fc0f5810f93b6406aa23780bd9357c8169070cb2 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Thu, 24 Nov 2016 21:46:30 -0500 Subject: json and xml marshalling almost working --- data.go | 16 ++++++++-------- marshall_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ modern.go | 6 +++--- 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 marshall_test.go diff --git a/data.go b/data.go index a844630..3327ca4 100644 --- a/data.go +++ b/data.go @@ -12,14 +12,14 @@ import ( type Scrobble struct { Artist Correctable `xml:"artist" json:"artist"` AlbumArtist Correctable `xml:"albumArtist" json:"albumArtist"` - TrackName Correctable `xml:"track" json:"albumArtist"` - Album Correctable `xml:"album" json:album"` - TrackNumber int - Duration int - Time int - Chosen bool - Mbid string - Session string + TrackName Correctable `xml:"track" json:"track"` + Album Correctable `xml:"album" json:"album"` + TrackNumber int `xml:"-" json:"-"` + Duration int `xml:"-" json:"-"` + Time int `xml:"timestamp" json:"timestamp,string"` + Chosen bool `xml:"-" json:"-"` + Mbid string `xml:"-" json:"-"` + Session string `xml:"-" json:"-"` } type Session struct { diff --git a/marshall_test.go b/marshall_test.go new file mode 100644 index 0000000..b5e9fae --- /dev/null +++ b/marshall_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "encoding/json" + "encoding/xml" + "fmt" + "os" + "testing" +) + +func TestMarshallXML(t *testing.T) { + s := []Scrobble{ + Scrobble{Artist: Correctable{Name: "Led Zeppelin"}, + TrackName: Correctable{Name: "Loser"}, + Time: 1479682785, + Album: Correctable{Name: "Mellow Gold"}}, + } + fmt.Printf("%v\n", s) + scrobbles := Scrobbles{Scrobbles: s, + Attrs: Attrs{1, 0}} + output, err := xml.MarshalIndent(scrobbles, " ", " ") + if err != nil { + fmt.Printf("error: %v\n", err) + } + os.Stdout.Write(output) +} + +func TestMarshallJSON(t *testing.T) { + s := []Scrobble{ + Scrobble{Artist: Correctable{Name: "Mumford & Sons"}, + Time: 1479682537, + Album: Correctable{Name: "Sign No More"}, + TrackName: Correctable{Name: "Little Lion Man"}}, + } + scrobbles := Scrobbles{Scrobbles: s, + Attrs: Attrs{1, 0}} + jsonscrobble := map[string]Scrobbles{"scrobbles": scrobbles} + output, err := json.MarshalIndent(jsonscrobble, " ", " ") + if err != nil { + fmt.Printf("error: %v\n", err) + } + os.Stdout.Write(output) +} diff --git a/modern.go b/modern.go index ee69dc4..fe66164 100644 --- a/modern.go +++ b/modern.go @@ -32,14 +32,14 @@ type Attrs struct { } type Scrobbles struct { - XMLName xml.Name `xml:"scrobbles" json:scrobbles"` - Scrobbles []Scrobble `xml:"scrobble" json:scrobble` + XMLName xml.Name `xml:"scrobbles" json:"-"` + Scrobbles []Scrobble `xml:"scrobble" json:"scrobble"` Attrs `json:"@attr"` } type Correctable struct { - Corrected int `xml:"corrected,attr" json:"corrected"` Name string `xml:",chardata" json:"#text"` + Corrected int `xml:"corrected,attr" json:"corrected"` } func NewCorrectable(name string) Correctable { -- cgit v1.2.3-70-g09d2