AnyStruct Crate
AnyStruct is a Rust crate that provides traits for converting between JSON and Protocol Buffers (Proto) data structures. Specifically, it offers the following traits:
IntoJSON
: Converts aValue
struct from theprost_types
crate to aserde_json::Value
struct.IntoProto
: Converts aserde_json::Value
struct to aValue
struct from theprost_types
crate.IntoJSONStruct
: Converts aStruct
struct from theprost_types
crate to aserde_json::Map
struct.IntoProtoStruct
: Converts aserde_json::Map
struct to aStruct
struct from theprost_types
crate.
Usage
To use AnyStruct in your Rust project, add the following line to your Cargo.toml
file:
[]
= "0.1.0"
Here is an example usage for converting a JSON string to a Proto struct:
use ;
let json_str = r#"{
"name": "John Doe",
"age": 42,
"is_student": true,
"grades": [80, 85, 90],
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
}
}"#;
let json_value = from_str.unwrap;
let proto_value = json_value.into_proto;
let proto_struct = ProtoStruct ;
And here is an example usage for converting a Proto struct to a JSON string:
use ;
let proto_value = Value ;
let json_value = proto_value.into_json;
let json_map = json_value.as_object.unwrap.clone;
let json_str = to_string_pretty.unwrap;
You can also use the IntoProtoStruct
and IntoJSONStruct
traits to convert between Struct
and serde_json::Map
structs:
let proto_struct = json_map.into_proto_struct;
let json_map2 = proto_struct.into_json_struct;