1use prost_types::{Value, Struct};
2
3pub trait IntoJSON {
4 fn into_json(self) -> serde_json::Value;
5}
6
7impl IntoJSON for Value {
8 fn into_json(self) -> serde_json::Value {
9 match self.kind {
10 Some(kind) => match kind {
11 prost_types::value::Kind::NullValue(_) => serde_json::Value::Null,
12 prost_types::value::Kind::NumberValue(n) => serde_json::Value::Number(serde_json::Number::from_f64(n).unwrap()),
13 prost_types::value::Kind::StringValue(s) => serde_json::Value::String(s),
14 prost_types::value::Kind::BoolValue(b) => serde_json::Value::Bool(b),
15 prost_types::value::Kind::StructValue(struct_value) => {
16 serde_json::Value::Object(
17 struct_value.fields.into_iter()
18 .map(|(name, val)| (name, val.into_json()))
19 .collect()
20 )
21 },
22 prost_types::value::Kind::ListValue(list_value) => {
23 serde_json::Value::Array(
24 list_value.values.into_iter()
25 .map(|value| value.into_json())
26 .collect()
27 )
28 }
29 },
30 None => serde_json::Value::Null,
31 }
32 }
33}
34
35
36pub trait IntoJSONStruct {
37 fn into_json_struct(self) -> serde_json::Map<String, serde_json::Value>;
38}
39
40impl IntoJSONStruct for Struct {
41 fn into_json_struct(self) -> serde_json::Map<String, serde_json::Value> {
42 self.fields.into_iter()
43 .map(|(name, value)| (name, value.into_json()))
44 .collect()
45 }
46}