3个版本

使用旧的Rust 2015

0.1.2 2018年10月22日
0.1.1 2018年10月21日
0.1.0 2018年10月20日

#467 in #structure


json_in_type中使用

自定义许可证

8KB
132 代码行

为json_in_type提供过程宏

此crate定义了一个derive模式宏,允许自动为自定义数据结构派生JSONValue trait。

示例

extern crate json_in_type;
#[macro_use]
extern crate json_in_type_derive;
use json_in_type::JSONValue;

#[derive(JSONValue)]
struct MyObject { // This will be encoded as a JSON object
    void: (),
    list: Vec<u8>,
    hello: String,
}

#[derive(JSONValue)]
struct WrapperStruct(u8, u8); // This will be encoded as a JSON list

lib.rs:

此模块提供了一个过程宏,用于为自定义结构体派生JSONValue。

示例

extern crate json_in_type;
#[macro_use] extern crate json_in_type_derive;
use json_in_type::JSONValue;

#[derive(JSONValue)]
struct MyObject {
    void: (),
    list: Vec<u8>,
    hello: String,
}

let obj = MyObject {
    void: (),
    list: vec![1, 2, 3],
    hello: String::from("world"),
};
assert_eq!(
    r#"{"void":null,"list":[1,2,3],"hello":"world"}"#,
    obj.to_json_string()
);

依赖项

~2MB
~47K SLoC