1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use Error;
use Cursor;
use Value as CborValue;
use Value as JsonValue;
/// Converts CBOR data to JSON format.
///
/// This function accepts a byte slice (`&[u8]`) representing CBOR data,
/// and converts it into a `serde_json::Value` representing the equivalent JSON data.
/// If the conversion is successful, it returns `Ok(JsonValue)`. If it fails, it returns an
/// error of type `serde_json::Error`.
///
/// The conversion process is as follows:
/// 1. The CBOR data is deserialized into a `ciborium::value::Value` (aliased as `CborValue`).
/// 2. The `CborValue` is then converted into a `serde_json::Value` (aliased as `JsonValue`).
///
/// # Arguments
///
/// * `cbor_data` - A byte slice that encodes CBOR data.
///
/// # Returns
///
/// * `serde_json::Result<JsonValue>` - If successful, the function returns `Ok(JsonValue)`,
/// where `JsonValue` is the equivalent JSON data. If the conversion fails, it returns an
/// error of type `serde_json::Error`.
///
/// # Examples
///
/// ```
/// use serde_json::json;
/// use coapum::helper::convert_cbor_to_json;
///
/// let cbor_data: Vec<u8> = vec![0xA1, 0x63, 0x66, 0x6F, 0x6F, 0x63, 0x62, 0x61, 0x72]; // Equivalent to {"foo": "bar"}
/// let json_value = convert_cbor_to_json(&cbor_data).unwrap();
/// assert_eq!(json_value, json!({"foo": "bar"}));
/// ```
/// Converts a JSON string to CBOR format.
///
/// This function accepts a string slice (`&str`) representing JSON data,
/// and converts it into a byte vector (`Vec<u8>`) representing the equivalent CBOR data.
/// If the conversion is successful, it returns `Ok(Vec<u8>)`. If it fails, it returns an
/// error of type `Box<dyn std::error::Error>`.
///
/// The conversion process is as follows:
/// 1. The JSON string is parsed into a `serde_json::Value` (aliased as `JsonValue`).
/// 2. The `JsonValue` is then converted into a `ciborium::value::Value` (aliased as `CborValue`).
/// 3. The `CborValue` is serialized into CBOR format and written into a buffer.
///
/// # Arguments
///
/// * `json` - A string slice that encodes JSON data.
///
/// # Returns
///
/// * `Result<Vec<u8>, Box<dyn std::error::Error>>` - If successful, the function returns `Ok(Vec<u8>)`,
/// where `Vec<u8>` is the equivalent CBOR data. If the conversion fails, it returns an
/// error of type `Box<dyn std::error::Error>`.
///
/// # Examples
///
/// ```
/// use serde_json::json;
/// use coapum::helper::convert_json_to_cbor;
///
/// let json_string = json!({"foo": "bar"}).to_string();
/// let cbor_data = convert_json_to_cbor(&json_string).unwrap();
/// assert_eq!(cbor_data, vec![0xA1, 0x63, 0x66, 0x6F, 0x6F, 0x63, 0x62, 0x61, 0x72]); // Equivalent to {"foo": "bar"}
/// ```