Skip to content

Commit

Permalink
feat: support JSON Feed 1.1 closes gorilla#91 (gorilla#104)
Browse files Browse the repository at this point in the history
## What type of PR is this? (check all applicable)

- [x] Feature

## Description

## Related Tickets & Documents
closes gorilla#91

## Added/updated tests?

- [x] Yes
  • Loading branch information
meblum authored and Bios-Marcel committed Apr 27, 2024
1 parent 7822231 commit ba7d771
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ applications.

[atom]: https://tools.ietf.org/html/rfc4287
[rss]: http://www.rssboard.org/rss-specification
[jsonfeed]: https://jsonfeed.org/version/1
[jsonfeed]: https://jsonfeed.org/version/1.1

### Usage

Expand Down Expand Up @@ -145,13 +145,18 @@ Outputs:
</rss>

{
"version": "https://jsonfeed.org/version/1",
"version": "https://jsonfeed.org/version/1.1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"authors": [
{
"name": "Jason Moiron"
}
],
"items": [
{
"id": "",
Expand All @@ -161,7 +166,12 @@ Outputs:
"date_published": "2013-01-16T03:22:24.530817846-05:00",
"author": {
"name": "Jason Moiron"
}
},
"authors": [
{
"name": "Jason Moiron"
}
]
},
{
"id": "",
Expand Down
23 changes: 19 additions & 4 deletions feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:
</rss>`

var jsonOutput = `{
"version": "https://jsonfeed.org/version/1",
"version": "https://jsonfeed.org/version/1.1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"authors": [
{
"name": "Jason Moiron"
}
],
"items": [
{
"id": "",
Expand All @@ -138,7 +143,12 @@ var jsonOutput = `{
"date_published": "2013-01-16T21:52:35-05:00",
"author": {
"name": "Jason Moiron"
}
},
"authors": [
{
"name": "Jason Moiron"
}
]
},
{
"id": "",
Expand Down Expand Up @@ -369,13 +379,18 @@ var rssOutputSorted = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
</rss>`

var jsonOutputSorted = `{
"version": "https://jsonfeed.org/version/1",
"version": "https://jsonfeed.org/version/1.1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"authors": [
{
"name": "Jason Moiron"
}
],
"items": [
{
"id": "",
Expand Down Expand Up @@ -572,7 +587,7 @@ func TestJSONHub(t *testing.T) {
Version: "https://jsonfeed.org/version/1",
Title: "feed title",
Hubs: []*JSONHub{
&JSONHub{
{
Type: "WebSub",
Url: "https://websub-hub.example",
},
Expand Down
41 changes: 24 additions & 17 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

const jsonFeedVersion = "https://jsonfeed.org/version/1"
const jsonFeedVersion = "https://jsonfeed.org/version/1.1"

// JSONAuthor represents the author of the feed or of an individual item
// in the feed
Expand Down Expand Up @@ -77,7 +77,8 @@ type JSONItem struct {
BannerImage string `json:"banner_,omitempty"`
PublishedDate *time.Time `json:"date_published,omitempty"`
ModifiedDate *time.Time `json:"date_modified,omitempty"`
Author *JSONAuthor `json:"author,omitempty"`
Author *JSONAuthor `json:"author,omitempty"` // deprecated in JSON Feed v1.1, keeping for backwards compatibility
Authors []*JSONAuthor `json:"authors,omitempty"`
Tags []string `json:"tags,omitempty"`
Attachments []JSONAttachment `json:"attachments,omitempty"`
}
Expand All @@ -92,19 +93,21 @@ type JSONHub struct {
// JSONFeed represents a syndication feed in the JSON Feed Version 1 format.
// Matching the specification found here: https://jsonfeed.org/version/1.
type JSONFeed struct {
Version string `json:"version"`
Title string `json:"title"`
HomePageUrl string `json:"home_page_url,omitempty"`
FeedUrl string `json:"feed_url,omitempty"`
Description string `json:"description,omitempty"`
UserComment string `json:"user_comment,omitempty"`
NextUrl string `json:"next_url,omitempty"`
Icon string `json:"icon,omitempty"`
Favicon string `json:"favicon,omitempty"`
Author *JSONAuthor `json:"author,omitempty"`
Expired *bool `json:"expired,omitempty"`
Hubs []*JSONHub `json:"hubs,omitempty"`
Items []*JSONItem `json:"items,omitempty"`
Version string `json:"version"`
Title string `json:"title"`
Language string `json:"language,omitempty"`
HomePageUrl string `json:"home_page_url,omitempty"`
FeedUrl string `json:"feed_url,omitempty"`
Description string `json:"description,omitempty"`
UserComment string `json:"user_comment,omitempty"`
NextUrl string `json:"next_url,omitempty"`
Icon string `json:"icon,omitempty"`
Favicon string `json:"favicon,omitempty"`
Author *JSONAuthor `json:"author,omitempty"` // deprecated in JSON Feed v1.1, keeping for backwards compatibility
Authors []*JSONAuthor `json:"authors,omitempty"`
Expired *bool `json:"expired,omitempty"`
Hubs []*JSONHub `json:"hubs,omitempty"`
Items []*JSONItem `json:"items,omitempty"`
}

// JSON is used to convert a generic Feed to a JSONFeed.
Expand Down Expand Up @@ -139,9 +142,11 @@ func (f *JSON) JSONFeed() *JSONFeed {
feed.HomePageUrl = f.Link.Href
}
if f.Author != nil {
feed.Author = &JSONAuthor{
author := &JSONAuthor{
Name: f.Author.Name,
}
feed.Author = author
feed.Authors = []*JSONAuthor{author}
}
for _, e := range f.Items {
feed.Items = append(feed.Items, newJSONItem(e))
Expand All @@ -165,9 +170,11 @@ func newJSONItem(i *Item) *JSONItem {
item.ExternalUrl = i.Source.Href
}
if i.Author != nil {
item.Author = &JSONAuthor{
author := &JSONAuthor{
Name: i.Author.Name,
}
item.Author = author
item.Authors = []*JSONAuthor{author}
}
if !i.Created.IsZero() {
item.PublishedDate = &i.Created
Expand Down

0 comments on commit ba7d771

Please sign in to comment.