5个版本 (3个破坏性更新)

使用旧的Rust 2015

0.4.1 2017年11月22日
0.4.0 2017年11月22日
0.3.0 2017年11月19日
0.2.0 2017年11月5日
0.1.0 2017年10月29日

#1103 in HTTP服务器

MIT/Apache

195KB
4K SLoC

json-api

CircleCI branch AppVeyor branch Codecov branch Crates.io

构建健壮的JSON API的惯用类型。

功能

序列化DSL

您可以使用友好、声明式的dsl定义一个Resource

简洁

#[macro_use]
extern crate json_api;

struct Post {
    id: u64,
    body: String,
    title: String,
    author: Option<User>,
    comments: Vec<Comment>,
}

resource!(Post, |&self| {
    // Define the id.
    id self.id;

    // Define the resource "type"
    kind "posts";

    // Define attributes with a comma seperated list of field names.
    attrs body, title;

    // Define relationships with a comma seperated list of field names.
    has_one author;
    has_many comments;
});

灵活

#[macro_use]
extern crate json_api;

struct Post {
    id: u64,
    body: String,
    title: String,
    author: Option<User>,
    comments: Vec<Comment>,
}

resource!(Post, |&self| {
    kind "articles";
    id self.id;

    attrs body, title;

    // Define a virtual attribute with an expression
    attr "preview", {
        self.body
            .chars()
            .take(140)
            .collect::<String>()
    }

    // Define a relationship with granular detail
    has_one "author", {
        // Data for has one should be Option<&T> where T: Resource
        data self.author.as_ref();

        // Define relationship links
        link "self", format!("/articles/{}/relationships/author", self.id);
        link "related", format!("/articles/{}/author", self.id);

        // Define arbitrary meta members with a block expression
        meta "read-only", true
    }

    // Define a relationship with granular detail
    has_many "comments", {
        // Data for has one should be an Iterator<Item = &T> where T: Resource
        data self.comments.iter();

        // Define relationship links
        link "self", format!("/articles/{}/relationships/comments", self.id);
        link "related", format!("/articles/{}/comments", self.id);

        // Define arbitrary meta members with a block expression
        meta "total", {
            self.comments.len()
        }
    }

    // You can also define links with granular details as well
    link "self", {
        href format!("/articles/{}", self.id);
    }

    // Define arbitrary meta members an expression
    meta "copyright", self.author.as_ref().map(|user| {
        format!("© 2017 {}", user.full_name())
    });
});

Rocket支持

json-api-rocket crate提供响应器以及一个公平的公平,用于捕获错误并返回JSON API错误文档。

#![feature(plugin)]
#![plugin(rocket_codegen)]

#[macro_use]
extern crate json_api;
extern crate json_api_rocket;
extern crate rocket;

mod models;

use json_api_rocket::JsonApiFairing;
use json_api_rocket::response::{Collection, Member};

use models::Article;

#[get("/")]
fn collection() -> Collection<Article> {
    (1..25).map(Article::new).collect()
}

#[get("/<id>")]
fn member(id: u64) -> Member<Article> {
    Member(Article::new(id))
}

fn main() {
    rocket::ignite()
        .attach(JsonApiFairing)
        .mount("/articles", routes![collection, member])
        .launch();
}

许可证

许可为以下之一

任选其一。

贡献

除非您明确声明,否则任何有意提交给作品并由您包含的贡献,根据Apache-2.0许可证的定义,应按上述方式双重许可,不附加任何额外条款或条件。

依赖关系

~15–26MB
~490K SLoC