Skip to main content

json_to_cbor

Function json_to_cbor 

Source
pub fn json_to_cbor(json: &[u8]) -> Result<Vec<u8>>
Expand description

Converts a JSON document into its CBOR encoding.

§Arguments

  • json - the UTF-8 JSON document to convert.

§Returns

The CBOR encoding of the same document, which is typically a good deal smaller and is what a constrained device or metered link should carry.

§Errors

Returns [Error::Codec] if json is not a valid JSON document, or if the document cannot be written as CBOR.

§Examples

use pamoja_codec::json_to_cbor;

let cbor = json_to_cbor(br#"{"c":21.5}"#).unwrap();
assert!(cbor.len() < br#"{"c":21.5}"#.len());