json2pb/
lib.rs

1//! ## convert json object to protobuf message
2//!
3//! json2pb is a library for converting json object to protobuf message.
4//! It also provides a commandline program `j2pb`. For more information, run `j2pb -h`
5//!
6//! `j2pb -f test.json` this can convert test.json to pb message and print the result on the screen(stdout)
7//!
8//! `j2pb -f test.json -o test.proto` does the same thing but save the result in test.proto
9//!
10//! ## json2pb library
11//!
12//! And you can use json2pb in your project just by adding `json2pb="*"` in your cargo dependency.
13//!
14//! ### example
15//! ```rust
16//! use json2pb::parser;
17//! use json2pb::pbgen;
18//! use nom::error::VerboseError;
19//!
20//! let json_code = r#"
21//!     {
22//!         "name": "deen",
23//!         "score": 98.5,
24//!         "list": [
25//!              {
26//!                  "i_name": "deen"
27//!              },
28//!              {
29//!                  "i_name": "caibirdme",
30//!                  "i_age": 26
31//!              }
32//!          ],
33//!          "foo": []
34//!     }
35//! "#;
36//!
37//! let json_value = parser::parse_root(json_code).unwrap();
38//! let json_2_pb_ast = pbgen::visit_json_root(&json_value).unwrap();
39//! let generated_pb_message = pbgen::gen_pb_def(&json_2_pb_ast);
40//! assert_eq!(generated_pb_message, r#"message root_data {
41//!     string name = 1;
42//!     double score = 2;
43//!     repeated List list = 3;
44//!     repeated google.protobuf.Any foo = 4;
45//!
46//!     message List {
47//!         string i_name = 1;
48//!         int64 i_age = 2;
49//!     }
50//! }
51//! "#);
52//! ```
53
54
55pub mod parser;
56pub mod pbgen;
57
58/// Common Result
59pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + 'static>>;