OCaml library for JSONfeed parsing and creation

readme

Changed files
+178
+18
LICENSE.md
···
+
(*
+
* ISC License
+
*
+
* Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>
+
*
+
* Permission to use, copy, modify, and distribute this software for any
+
* purpose with or without fee is hereby granted, provided that the above
+
* copyright notice and this permission notice appear in all copies.
+
*
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
*
+
*)
+160
README.md
···
+
# OCaml-JSONFeed
+
+
An OCaml library for parsing and generating [JSON Feed](https://www.jsonfeed.org/) documents.
+
+
JSON Feed is a format similar to RSS and Atom but uses JSON instead of XML.
+
It is designed to be easier for developers to work with while providing all the
+
functionality needed for feed syndication.
+
+
## Installation
+
+
Add to your `dune-project`:
+
+
```lisp
+
(package
+
(name your-package)
+
(depends
+
jsonfeed
+
...))
+
```
+
+
## Quick Start
+
+
### Creating a Feed
+
+
```ocaml
+
open Jsonfeed
+
+
(* Create an author *)
+
let author = Author.create
+
~name:"Jane Doe"
+
~url:"https://example.com/jane"
+
()
+
+
(* Create an item with HTML content *)
+
let item = Item.create
+
~id:"https://example.com/posts/1"
+
~url:"https://example.com/posts/1"
+
~title:"Hello, JSON Feed!"
+
~content:(`Html "<p>My first post using JSON Feed.</p>")
+
~authors:[author]
+
~tags:["introduction"; "jsonfeed"]
+
()
+
+
(* Create the feed *)
+
let feed = Jsonfeed.create
+
~title:"My Blog"
+
~home_page_url:"https://example.com"
+
~feed_url:"https://example.com/feed.json"
+
~items:[item]
+
()
+
+
(* Serialize to JSON *)
+
let json = Jsonfeed.to_string feed
+
```
+
+
### Parsing a Feed
+
+
```ocaml
+
open Jsonfeed
+
+
(* Parse from string *)
+
match Jsonfeed.of_string json_string with
+
| Ok feed ->
+
Printf.printf "Feed: %s\n" (Jsonfeed.title feed);
+
List.iter (fun item ->
+
match Item.title item with
+
| Some title -> Printf.printf "- %s\n" title
+
| None -> ()
+
) (Jsonfeed.items feed)
+
| Error (`Msg err) ->
+
Printf.eprintf "Parse error: %s\n" err
+
+
(* Parse from file *)
+
let content = In_channel.with_open_text "feed.json" In_channel.input_all in
+
match Jsonfeed.of_string content with
+
| Ok feed -> (* ... *)
+
| Error _ -> (* ... *)
+
```
+
+
### Content Types
+
+
Items can have HTML content, plain text content, or both:
+
+
```ocaml
+
(* HTML only *)
+
let item1 = Item.create
+
~id:"1"
+
~content:(`Html "<p>Rich <strong>HTML</strong> content</p>")
+
()
+
+
(* Plain text only *)
+
let item2 = Item.create
+
~id:"2"
+
~content:(`Text "Plain text content")
+
()
+
+
(* Both HTML and text *)
+
let item3 = Item.create
+
~id:"3"
+
~content:(`Both ("<p>HTML version</p>", "Text version"))
+
()
+
+
(* Access content *)
+
match Item.content_html item1 with
+
| Some html -> Printf.printf "HTML: %s\n" html
+
| None -> ()
+
```
+
+
### Podcast Feed with Attachments
+
+
```ocaml
+
(* Create an audio attachment *)
+
let episode_audio = Attachment.create
+
~url:"https://podcast.example.com/ep1.mp3"
+
~mime_type:"audio/mpeg"
+
~size_in_bytes:15_728_640L
+
~duration_in_seconds:1800
+
()
+
+
(* Create a podcast episode *)
+
let episode = Item.create
+
~id:"https://podcast.example.com/episodes/1"
+
~title:"Episode 1: Introduction"
+
~content:(`Html "<p>Welcome to the show!</p>")
+
~attachments:[episode_audio]
+
()
+
```
+
+
## Examples
+
+
The `example/` directory contains several complete examples:
+
+
- **feed_example.ml** - Creating and serializing feeds (blog and podcast)
+
- **feed_parser.ml** - Parsing and analyzing feeds from files
+
- **feed_validator.ml** - Validating feeds and demonstrating various feed types
+
+
Run examples:
+
+
```bash
+
opam exec -- dune exec -- ./example/feed_parser.exe
+
opam exec -- dune exec -- ./example/feed_example.exe
+
```
+
+
## API Documentation
+
+
Build the API documentation:
+
+
```bash
+
opam exec -- dune build @doc
+
```
+
+
Then open `_build/default/_doc/_html/index.html` in your browser.
+
+
## Specification
+
+
This library implements [JSON Feed Version 1.1](https://www.jsonfeed.org/version/1.1/).
+
+
## License
+
+
See LICENSE.md for details.