Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generator/sbpg/targets/resources/sbp2json-cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ features = ["sbp2json"]
[dev-dependencies]
sha2 = "0.8"
hex = "0.4"
serde_json = "1.0"

[profile.release]
lto = true
Expand Down
24 changes: 12 additions & 12 deletions rust/sbp/src/sbp2json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn add_common_fields<'a>(
/// etc). to the outputted JSON object.
fn write_sbp_json_value(
float_compat: bool,
rewrap_data: bool,
rewrap_data: Option<Value>,
base64_payload: &mut String,
common_sbp: &dyn SBPMessage,
slice: &[u8],
Expand All @@ -200,12 +200,12 @@ fn write_sbp_json_value(
) -> Result<()> {
let value = unpack(value);
let value = add_common_fields(common_sbp, slice, value.unwrap(), base64_payload);
let data_wrapped;
let value = if rewrap_data {
data_wrapped = json!({ "data": value });
&data_wrapped
let value = if let Some(mut rewrap_data) = rewrap_data {
let data_obj = rewrap_data.as_object_mut().unwrap();
data_obj.insert("data".into(), value.clone());
rewrap_data
} else {
value
value.clone()
};
if float_compat {
let io_ref = Rc::get_mut(stream).expect("could not get output stream");
Expand All @@ -223,18 +223,18 @@ fn write_sbp_json_value(

/// The Swift console stores SBP JSON in the "data" field of a JSON object, if this field is
/// present then we should unpack the SPB JSON from it.
fn unwrap_data_obj<'a>(value: &'a Value) -> (&'a Value, bool) {
fn unwrap_data_obj<'a>(value: &'a Value) -> (&'a Value, Option<Value>) {
if !value.is_object() {
return (value, false);
return (value, None);
}
let map = value.as_object().unwrap();
if map.contains_key("payload") {
return (value, false);
return (value, None);
}
if !map.contains_key("data") {
return (value, false);
return (value, None);
}
(map.get("data").unwrap(), true)
(map.get("data").unwrap(), Some(value.clone()))
}

/// Unwrap and return the "payload" key from a JSON object, if it exists.
Expand Down Expand Up @@ -454,7 +454,7 @@ pub fn sbp2json_read_loop(
let mut value = serde_json::to_value(&msg)?;
write_sbp_json_value(
float_compat,
false,
None,
&mut base64_payload,
common_sbp,
slice,
Expand Down
3 changes: 2 additions & 1 deletion rust/sbp2json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ features = ["sbp2json"]
[dev-dependencies]
sha2 = "0.8"
hex = "0.4"
serde_json = "1.0"

[profile.release]
lto = true
Expand All @@ -40,4 +41,4 @@ path = "src/json2sbp.rs"

[[bin]]
name = "json2json"
path = "src/json2json.rs"
path = "src/json2json.rs"
4 changes: 2 additions & 2 deletions rust/sbp2json/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sha2::{Digest, Sha256};

use sbp::sbp2json::Result;

fn find_project_root() -> Option<PathBuf> {
pub fn find_project_root() -> Option<PathBuf> {
let exe = env::current_exe();
assert!(exe.is_ok());
let mut path = exe.unwrap();
Expand All @@ -32,7 +32,7 @@ fn find_project_root() -> Option<PathBuf> {
return None;
}

struct DeleteTestOutput {
pub struct DeleteTestOutput {
files: Vec<PathBuf>,
}

Expand Down
48 changes: 47 additions & 1 deletion rust/sbp2json/tests/test_round_trips.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::boxed::Box;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::rc::Rc;

#[macro_use]
mod common;

use common::{test_round_trip, ThirdTransform};
use common::{find_project_root, test_round_trip, DeleteTestOutput, ThirdTransform};

use sbp::sbp2json::json2json_read_loop;
use sbp::sbp2json::json2sbp_read_loop;
Expand Down Expand Up @@ -80,3 +82,47 @@ fn test_json2json() -> Result<()> {
third_transform,
)
}

#[test]
fn test_json2json_maintain_fields() -> Result<()> {
let mut del_test_output = DeleteTestOutput::new();

let root = find_project_root().unwrap();
let root = root.as_path();

let output_path = root.join("test_data/serial-link.log.json.output");

{
let input_path = root.join("test_data/serial-link.log.json");
let mut input_file = File::open(input_path).expect("could not open input file");

del_test_output.add_test_output(&output_path);

let output_file = File::create(&output_path).expect("could not create output file");

let mut output_file: Rc<Box<dyn Write>> = Rc::new(Box::new(output_file));

assert!(json2json_read_loop(false, false, &mut input_file, &mut output_file).is_ok());
}

let mut message_count = 0;

{
let output_file = File::open(output_path).expect("could not open input file");
let output_file = BufReader::new(output_file);

for line in output_file.lines() {
let value: serde_json::Value = serde_json::from_str(&line.unwrap()).unwrap();
assert!(value.is_object());

let value = value.as_object().unwrap();
assert!(value.contains_key("time"));

message_count += 1;
}
}

assert_eq!(message_count, 10);

Ok(())
}
10 changes: 10 additions & 0 deletions test_data/serial-link.log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{"time":"2020-08-28T20:02:28.43443402Z","data":{"length":82,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIGNoYW5nZWQ6IGVuYWJsZV9icm9hZGNhc3QgPSAwLCBicm9hZGNhc3QgcG9ydCA9IDU2NjY2LCBvZmZzZXQgPSAtMzIuMTU5Nw==","crc":15453}}
{"time":"2020-08-28T20:02:28.454301488Z","data":{"length":82,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIGNoYW5nZWQ6IGVuYWJsZV9icm9hZGNhc3QgPSAwLCBicm9hZGNhc3QgcG9ydCA9IDU2NjY2LCBvZmZzZXQgPSAtMzIuMTU5Nw==","crc":15453}}
{"time":"2020-08-28T20:02:28.464622847Z","data":{"length":34,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIHdhdGNoZWQ6IG50cmlwX2VuYWJsZT0gMA==","crc":21196}}
{"time":"2020-08-28T20:02:28.474422726Z","data":{"length":82,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIGNoYW5nZWQ6IGVuYWJsZV9icm9hZGNhc3QgPSAwLCBicm9hZGNhc3QgcG9ydCA9IDU2NjY2LCBvZmZzZXQgPSAtMzIuMTU5Nw==","crc":15453}}
{"time":"2020-08-28T20:02:28.484696224Z","data":{"length":34,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIHdhdGNoZWQ6IG50cmlwX2VuYWJsZT0gMA==","crc":21196}}
{"time":"2020-08-28T20:02:28.494474444Z","data":{"length":82,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIGNoYW5nZWQ6IGVuYWJsZV9icm9hZGNhc3QgPSAwLCBicm9hZGNhc3QgcG9ydCA9IDU2NjY2LCBvZmZzZXQgPSAtMzIuMTU5Nw==","crc":15453}}
{"time":"2020-08-28T20:02:28.504458866Z","data":{"length":34,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIHdhdGNoZWQ6IG50cmlwX2VuYWJsZT0gMA==","crc":21196}}
{"time":"2020-08-28T20:02:28.515197572Z","data":{"length":82,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIGNoYW5nZWQ6IGVuYWJsZV9icm9hZGNhc3QgPSAwLCBicm9hZGNhc3QgcG9ydCA9IDU2NjY2LCBvZmZzZXQgPSAtMzIuMTU5Nw==","crc":15453}}
{"time":"2020-08-28T20:02:28.524860578Z","data":{"length":34,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIHdhdGNoZWQ6IG50cmlwX2VuYWJsZT0gMA==","crc":21196}}
{"time":"2020-08-28T20:02:28.534466535Z","data":{"length":82,"preamble":85,"sender":44432,"msg_type":1025,"payload":"B1NldHRpbmdzIGNoYW5nZWQ6IGVuYWJsZV9icm9hZGNhc3QgPSAwLCBicm9hZGNhc3QgcG9ydCA9IDU2NjY2LCBvZmZzZXQgPSAtMzIuMTU5Nw==","crc":15453}}