28 lines
578 B
Go
28 lines
578 B
Go
package formatter
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
|
|
"royalroadupdates/internal/royalroad"
|
|
)
|
|
|
|
// jsonEntry is the JSON output shape.
|
|
type jsonEntry struct {
|
|
Name string `json:"name"`
|
|
Date string `json:"date"`
|
|
}
|
|
|
|
// PrintJSON writes stories as JSON to w.
|
|
func PrintJSON(w io.Writer, stories []royalroad.Story) error {
|
|
entries := make([]jsonEntry, 0, len(stories))
|
|
for _, s := range stories {
|
|
entries = append(entries, jsonEntry{
|
|
Name: s.Name,
|
|
Date: s.DateModified.UTC().Format("2006-01-02T15:04:05Z"),
|
|
})
|
|
}
|
|
enc := json.NewEncoder(w)
|
|
return enc.Encode(entries)
|
|
}
|