#serde-json #protobuf #deserialize #prost-types

anystruct

AnyStruct is a Rust crate that provides traits for converting between JSON and Protocol Buffers (Proto) data structures

1 unstable release

0.1.0 Apr 30, 2023

#1227 in Data structures

Download history 5/week @ 2024-02-16 7/week @ 2024-02-23 2/week @ 2024-03-01 1/week @ 2024-03-08 7/week @ 2024-03-29 1/week @ 2024-04-05 38/week @ 2024-04-12 9/week @ 2024-04-19

55 downloads per month

MIT license

9KB
96 lines

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 a Value struct from the prost_types crate to a serde_json::Value struct.
  • IntoProto: Converts a serde_json::Value struct to a Value struct from the prost_types crate.
  • IntoJSONStruct: Converts a Struct struct from the prost_types crate to a serde_json::Map struct.
  • IntoProtoStruct: Converts a serde_json::Map struct to a Struct struct from the prost_types crate.

Usage

To use AnyStruct in your Rust project, add the following line to your Cargo.toml file:

[dependencies]
anystruct = "0.1.0"

Here is an example usage for converting a JSON string to a Proto struct:

use anystruct::{IntoProto, ProtoStruct};

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 = serde_json::from_str(json_str).unwrap();
let proto_value = json_value.into_proto();
let proto_struct = ProtoStruct { fields: [("my_data".to_string(), proto_value)].iter().cloned().collect() };

And here is an example usage for converting a Proto struct to a JSON string:

use anystruct::{IntoJSON, IntoJSONStruct};

let proto_value = prost_types::Value {
    kind: Some(prost_types::value::Kind::StructValue(prost_types::Struct {
        fields: [("name".to_string(), prost_types::Value {
            kind: Some(prost_types::value::Kind::StringValue("John Doe".to_string())),
        })].iter().cloned().collect(),
    })),
};
let json_value = proto_value.into_json();
let json_map = json_value.as_object().unwrap().clone();
let json_str = serde_json::to_string_pretty(&json_map).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();

Dependencies

~3MB
~65K SLoC