···
3
+
An OCaml library for parsing and generating [JSON Feed](https://www.jsonfeed.org/) documents.
5
+
JSON Feed is a format similar to RSS and Atom but uses JSON instead of XML.
6
+
It is designed to be easier for developers to work with while providing all the
7
+
functionality needed for feed syndication.
11
+
Add to your `dune-project`:
28
+
(* Create an author *)
29
+
let author = Author.create
31
+
~url:"https://example.com/jane"
34
+
(* Create an item with HTML content *)
35
+
let item = Item.create
36
+
~id:"https://example.com/posts/1"
37
+
~url:"https://example.com/posts/1"
38
+
~title:"Hello, JSON Feed!"
39
+
~content:(`Html "<p>My first post using JSON Feed.</p>")
41
+
~tags:["introduction"; "jsonfeed"]
44
+
(* Create the feed *)
45
+
let feed = Jsonfeed.create
47
+
~home_page_url:"https://example.com"
48
+
~feed_url:"https://example.com/feed.json"
52
+
(* Serialize to JSON *)
53
+
let json = Jsonfeed.to_string feed
61
+
(* Parse from string *)
62
+
match Jsonfeed.of_string json_string with
64
+
Printf.printf "Feed: %s\n" (Jsonfeed.title feed);
65
+
List.iter (fun item ->
66
+
match Item.title item with
67
+
| Some title -> Printf.printf "- %s\n" title
69
+
) (Jsonfeed.items feed)
70
+
| Error (`Msg err) ->
71
+
Printf.eprintf "Parse error: %s\n" err
73
+
(* Parse from file *)
74
+
let content = In_channel.with_open_text "feed.json" In_channel.input_all in
75
+
match Jsonfeed.of_string content with
76
+
| Ok feed -> (* ... *)
77
+
| Error _ -> (* ... *)
82
+
Items can have HTML content, plain text content, or both:
86
+
let item1 = Item.create
88
+
~content:(`Html "<p>Rich <strong>HTML</strong> content</p>")
91
+
(* Plain text only *)
92
+
let item2 = Item.create
94
+
~content:(`Text "Plain text content")
97
+
(* Both HTML and text *)
98
+
let item3 = Item.create
100
+
~content:(`Both ("<p>HTML version</p>", "Text version"))
103
+
(* Access content *)
104
+
match Item.content_html item1 with
105
+
| Some html -> Printf.printf "HTML: %s\n" html
109
+
### Podcast Feed with Attachments
112
+
(* Create an audio attachment *)
113
+
let episode_audio = Attachment.create
114
+
~url:"https://podcast.example.com/ep1.mp3"
115
+
~mime_type:"audio/mpeg"
116
+
~size_in_bytes:15_728_640L
117
+
~duration_in_seconds:1800
120
+
(* Create a podcast episode *)
121
+
let episode = Item.create
122
+
~id:"https://podcast.example.com/episodes/1"
123
+
~title:"Episode 1: Introduction"
124
+
~content:(`Html "<p>Welcome to the show!</p>")
125
+
~attachments:[episode_audio]
131
+
The `example/` directory contains several complete examples:
133
+
- **feed_example.ml** - Creating and serializing feeds (blog and podcast)
134
+
- **feed_parser.ml** - Parsing and analyzing feeds from files
135
+
- **feed_validator.ml** - Validating feeds and demonstrating various feed types
140
+
opam exec -- dune exec -- ./example/feed_parser.exe
141
+
opam exec -- dune exec -- ./example/feed_example.exe
144
+
## API Documentation
146
+
Build the API documentation:
149
+
opam exec -- dune build @doc
152
+
Then open `_build/default/_doc/_html/index.html` in your browser.
156
+
This library implements [JSON Feed Version 1.1](https://www.jsonfeed.org/version/1.1/).
160
+
See LICENSE.md for details.