|
1 |
| -use super::{escape_cbor_buf, unescape_cbor_buf, CborUtilError, CborUtilResult}; |
2 |
| - |
3 |
| -pub fn cbor_to_json(cbor: &[u8]) -> CborUtilResult<String> { |
4 |
| - let mut val = cbor4ii::serde::from_slice(cbor).map_err(|_| CborUtilError::InvalidEncoding)?; |
5 |
| - escape_cbor_buf(&mut val); |
6 |
| - serde_json::to_string_pretty(&val).map_err(|_| CborUtilError::InvalidEncoding) |
7 |
| -} |
8 |
| - |
9 |
| -pub fn json_to_cbor(json: &str) -> CborUtilResult<Vec<u8>> { |
10 |
| - let mut val = serde_json::from_str(json).map_err(|_| CborUtilError::InvalidEncoding)?; |
11 |
| - unescape_cbor_buf(&mut val)?; |
12 |
| - cbor4ii::serde::to_vec(vec![], &val).map_err(|_| CborUtilError::InvalidEncoding) |
13 |
| -} |
14 |
| - |
15 |
| -#[cfg(test)] |
16 |
| -mod tests { |
17 |
| - use super::*; |
18 |
| - |
19 |
| - #[test] |
20 |
| - fn test_cbor_to_json() { |
21 |
| - let cbor = b"\x42\x68\x68"; |
22 |
| - let json = cbor_to_json(cbor).unwrap(); |
23 |
| - assert_eq!( |
24 |
| - json, |
25 |
| - r#"{ |
26 |
| - "__byte_repr": "utf8", |
27 |
| - "data": "hh" |
28 |
| -}"# |
29 |
| - ); |
30 |
| - } |
31 |
| - #[test] |
32 |
| - fn test_cbor_to_json_invalid_cbor() { |
33 |
| - let cbor = b"\x42\x68"; |
34 |
| - let res = cbor_to_json(cbor); |
35 |
| - assert_eq!(res, Err(CborUtilError::InvalidEncoding)); |
36 |
| - } |
37 |
| - |
38 |
| - #[test] |
39 |
| - fn test_json_to_cbor() { |
40 |
| - let json = r#"{ "__byte_repr": "utf8", "data": "hh" }"#; |
41 |
| - let cbor = json_to_cbor(json).unwrap(); |
42 |
| - let expected_cbor = b"\x42\x68\x68"; |
43 |
| - assert_eq!(cbor, expected_cbor); |
44 |
| - } |
45 |
| - #[test] |
46 |
| - fn test_json_to_cbor_invalid_json() { |
47 |
| - let json = "{ "; |
48 |
| - let res = json_to_cbor(json); |
49 |
| - assert_eq!(res, Err(CborUtilError::InvalidEncoding)); |
50 |
| - } |
51 |
| -} |
| 1 | +use super::{escape_cbor_buf, unescape_cbor_buf, CborUtilError, CborUtilResult}; |
| 2 | + |
| 3 | +pub fn cbor_to_json(cbor: &[u8]) -> CborUtilResult<String> { |
| 4 | + let mut val = cbor4ii::serde::from_slice(cbor).map_err(|_| CborUtilError::InvalidEncoding)?; |
| 5 | + escape_cbor_buf(&mut val); |
| 6 | + serde_json::to_string_pretty(&val).map_err(|_| CborUtilError::InvalidEncoding) |
| 7 | +} |
| 8 | + |
| 9 | +pub fn json_to_cbor(json: &str) -> CborUtilResult<Vec<u8>> { |
| 10 | + let mut val = serde_json::from_str(json).map_err(|_| CborUtilError::InvalidEncoding)?; |
| 11 | + unescape_cbor_buf(&mut val)?; |
| 12 | + cbor4ii::serde::to_vec(vec![], &val).map_err(|_| CborUtilError::InvalidEncoding) |
| 13 | +} |
| 14 | + |
| 15 | +#[cfg(test)] |
| 16 | +mod tests { |
| 17 | + use super::*; |
| 18 | + |
| 19 | + #[test] |
| 20 | + fn test_cbor_to_json() { |
| 21 | + let cbor = b"\x42\x68\x68"; |
| 22 | + let json = cbor_to_json(cbor).unwrap(); |
| 23 | + assert_eq!( |
| 24 | + json, |
| 25 | + r#"{ |
| 26 | + "__byte_repr": "utf8", |
| 27 | + "data": "hh" |
| 28 | +}"# |
| 29 | + ); |
| 30 | + } |
| 31 | + #[test] |
| 32 | + fn test_cbor_to_json_invalid_cbor() { |
| 33 | + let cbor = b"\x42\x68"; |
| 34 | + let res = cbor_to_json(cbor); |
| 35 | + assert_eq!(res, Err(CborUtilError::InvalidEncoding)); |
| 36 | + } |
| 37 | + |
| 38 | + #[test] |
| 39 | + fn test_json_to_cbor() { |
| 40 | + let json = r#"{ "__byte_repr": "utf8", "data": "hh" }"#; |
| 41 | + let cbor = json_to_cbor(json).unwrap(); |
| 42 | + let expected_cbor = b"\x42\x68\x68"; |
| 43 | + assert_eq!(cbor, expected_cbor); |
| 44 | + } |
| 45 | + #[test] |
| 46 | + fn test_json_to_cbor_invalid_json() { |
| 47 | + let json = "{ "; |
| 48 | + let res = json_to_cbor(json); |
| 49 | + assert_eq!(res, Err(CborUtilError::InvalidEncoding)); |
| 50 | + } |
| 51 | +} |
0 commit comments