2个版本
0.1.1 | 2019年6月23日 |
---|---|
0.1.0 | 2019年6月22日 |
#74 in #transformation
70KB
2K SLoC
Serde JSON
大黄蜂是一个具有简单内置规则的JSON转换器,即使是普通用户也能轻松实现。它设计用于可扩展性,使用简单,可序列化,以便于在服务和应用中进行存储和创建。
[dependencies]
bumblebee = "0.1"
示例用法
use bumblebee::prelude::*;
use bumblebee::errors::Result;
fn test_example() -> Result<()> {
let trans = TransformerBuilder::default()
.add_direct("user_id", "id")?
.add_direct("full-name", "name")?
.add_flatten(
"nicknames",
"",
FlattenOps {
recursive: true,
prefix: Some("nickname"),
separator: Some("_"),
manipulation: None,
},
)?
.add_direct("nested.inner.key", "prev_nested")?
.add_direct("nested.my_arr[1]", "prev_arr")?
.build()?;
let input = r#"
{
"user_id":"111",
"full-name":"Dean Karn",
"nicknames":["Deano","Joey Bloggs"],
"nested": {
"inner":{
"key":"value"
},
"my_arr":[null,"arr_value",null]
}
}"#;
let expected = r#"{"id":"111","name":"Dean Karn","nickname_1":"Deano","nickname_2":"Joey Bloggs","prev_arr":"arr_value","prev_nested":"value"}"#;
let res = trans.apply_from_str(input)?;
assert_eq!(expected, serde_json::to_string(&res)?);
Ok(())
}
或者当你想要进行结构到结构的转换时
use bumblebee::prelude::*;
use bumblebee::errors::Result;
use serde::{Serialize, Deserialize};
fn test_struct() -> Result<()> {
#[derive(Debug, Serialize)]
struct From {
existing: String,
}
#[derive(Debug, Deserialize, PartialEq)]
struct To {
new: String,
}
let trans = TransformerBuilder::default()
.add_direct("existing", "new")?
.build()?;
let from = From {
existing: String::from("existing_value"),
};
let expected = To {
new: String::from("existing_value"),
};
let res: To = trans.apply_to(from)?;
assert_eq!(expected, res);
Ok(())
}
许可证
根据您的选择,许可协议为Apache License, Version 2.0或MIT license。除非您明确声明,否则您提交给Serde的任何贡献,根据Apache-2.0许可证的定义,应按照上述方式双重许可,不附加任何额外条款或条件。
依赖项
~1.9–2.5MB
~57K SLoC