JSON Feed Model provides types which can be used to manipulate JSON Feed data.
The crate is basically a newtype wrapper around Serde JSON's Map
type and provides methods
to JSON Feed properties.
By default, features which depend on the Rust std
library are included.
toml
[dependencies]
json-feed-model = "0.1.1"
If the host environment has an allocator but does not have access to the Rust std
library:
toml
[dependencies]
json-feed-model = { version = "0.1.1", default-features = false, features = ["alloc"]}
The following example shows how to read properties.
```rust use jsonfeedmodel::{Feed, ItemRef, Version};
let json = serdejson::json!({ "version": "https://jsonfeed.org/version/1.1", "title": "Lorem ipsum dolor sit amet.", "homepageurl": "https://example.org/", "feedurl": "https://example.org/feed.json", "items": [ { "id": "cd7f0673-8e81-4e13-b273-4bd1b83967d0", "contenttext": "Aenean tristique dictum mauris, et.", "url": "https://example.org/aenean-tristique" }, { "id": "2bcb497d-c40b-4493-b5ae-bc63c74b48fa", "contenthtml": "Vestibulum non magna vitae tortor.", "url": "https://example.org/vestibulum-non" } ] });
let feed = jsonfeedmodel::from_value(json)?;
assert!(feed.isvalid(&Version::Version11));
asserteq!(feed.version()?, Some(jsonfeedmodel::VERSION11)); asserteq!(feed.title()?, Some("Lorem ipsum dolor sit amet.")); asserteq!(feed.homepageurl()?, Some("https://example.org/")); asserteq!(feed.feed_url()?, Some("https://example.org/feed.json"));
let items: Option
asserteq!(items[0].id()?, Some("cd7f0673-8e81-4e13-b273-4bd1b83967d0")); asserteq!( items[0].contenttext()?, Some("Aenean tristique dictum mauris, et.") ); asserteq!( items[0].url()?, Some("https://example.org/aenean-tristique") );
asserteq!(items[1].id()?, Some("2bcb497d-c40b-4493-b5ae-bc63c74b48fa")); asserteq!( items[1].contenthtml()?, Some("Vestibulum non magna vitae tortor.") ); asserteq!(items[1].url()?, Some("https://example.org/vestibulum-non")); ```
The following example uses a custom trait to write and then read a custom extension.
It also shows a simple way to use serde_json
to write the JSON Feed. See
serde_json
for other serialization methods.
```rust use jsonfeedmodel::{Feed, Item, Version}; use serde_json::Value;
trait ExampleExtension { fn example(&self) -> Result
fn set_example<T>(&mut self, value: T) -> Option<Value>
where
T: ToString;
}
impl ExampleExtension for Feed { fn example(&self) -> Result
fn set_example<T>(&mut self, value: T) -> Option<Value>
where
T: ToString,
{
self.as_map_mut()
.insert(String::from("_example"), Value::String(value.to_string()))
}
}
let mut feed = Feed::new(); feed.setversion(Version::Version11); feed.set_title("Lorem ipsum dolor sit amet.");
feed.set_example("123456");
let mut item = Item::new(); item.setid("2bcb497d-c40b-4493-b5ae-bc63c74b48fa"); item.setcontenttext("Vestibulum non magna vitae tortor."); item.seturl("https://example.org/vestibulum-non");
feed.set_items(vec![item]);
assert!(feed.isvalid(&Version::Version11));
let expectedjson = serdejson::json!({ "version": "https://jsonfeed.org/version/1.1", "title": "Lorem ipsum dolor sit amet.", "example": "123456", "items": [ { "id": "2bcb497d-c40b-4493-b5ae-bc63c74b48fa", "contenttext": "Vestibulum non magna vitae tortor.", "url": "https://example.org/vestibulum-non", } ] }); asserteq!(feed, jsonfeedmodel::fromvalue(expected_json)?);
assert_eq!(feed.example()?, Some("123456"));
let output = serdejson::tostring(&feed); assert!(output.is_ok()); ```
Licensed under either of Apache License, Version 2.0 or MIT License at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.