Skip to content

Commit

Permalink
Add sbp::json::to_value
Browse files Browse the repository at this point in the history
  • Loading branch information
silverjam committed Jul 12, 2023
1 parent a94f120 commit 13f8cc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rust/sbp/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use serde_json::ser::CompactFormatter;
pub use de::stream_messages;
pub use de::{iter_json2json_messages, iter_messages, iter_messages_from_fields};

pub use ser::{to_vec, to_writer, Json2JsonEncoder, JsonEncoder};
pub use ser::{to_vec, to_value, to_writer, Json2JsonEncoder, JsonEncoder};

#[derive(Debug, Deserialize)]
#[serde(untagged)]
Expand Down
16 changes: 16 additions & 0 deletions rust/sbp/src/json/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ where
Ok(())
}

/// Serialize the given message as a JSON byte vector.
pub fn to_value<M>(msg: &M) -> Result<serde_json::Value, JsonError>
where
M: SbpMessage + Serialize + WireFormat + Clone,
{
let mut frame = BytesMut::with_capacity(BUFLEN);
let mut payload = String::with_capacity(BUFLEN);

let output = JsonOutput {
common: get_common_fields(&mut payload, &mut frame, msg)?,
msg,
};

serde_json::to_value(output).map_err(|e| e.into())
}

/// Writes [Sbp] messages as JSON into a writer.
#[derive(Debug)]
pub struct JsonEncoder<W, F>(FramedWrite<W, JsonEncoderInner<F>>);
Expand Down

0 comments on commit 13f8cc3

Please sign in to comment.