TinyCBOR 0.6.0 API
|
Group of functions used to convert CBOR to JSON. More...
Functions | |
CborError | cbor_value_to_json_advance (FILE *out, CborValue *value, int flags) |
Converts the current CBOR type pointed to by value to JSON and writes that to the out stream. | |
Group of functions used to convert CBOR to JSON.
This group contains two functions that can be used to convert a CborValue object to an equivalent JSON representation. This module attempts to follow the recommendations from RFC 7049 section 4.1 "Converting from CBOR to JSON", though it has a few differences. They are noted below.
These functions produce a "minified" JSON output, with no spacing, indentation or line breaks. If those are necessary, they need to be applied in a post-processing phase.
Note that JSON cannot support all CBOR types with fidelity, so the conversion is usually lossy. For that reason, TinyCBOR supports adding a set of metadata JSON values that can be used by a JSON-to-CBOR converter to restore the original data types.
The TinyCBOR library does not provide a way to convert from JSON representation back to encoded form. However, it provides a tool called json2cbor
which can be used for that purpose. That tool supports the metadata format that these functions may produce.
Either of the functions in this section will attempt to convert exactly one CborValue object to JSON. Those functions may return any error documented for the functions for CborParsing. In addition, if the C standard library stream functions return with error, the text conversion will return with error CborErrorIO.
These functions also perform UTF-8 validation in CBOR text strings. If they encounter a sequence of bytes that is not permitted in UTF-8, they will return CborErrorInvalidUtf8TextString. That includes encoding of surrogate points in UTF-8.
When converting from CBOR to JSON, there may be information loss. This section lists the possible scenarios.
nn
is the simple type's value, with the exception of CBOR undefined, which becomes "undefined", while CBOR byte strings are converted to an Base16, Base64, or Base64url encodingjson2cbor
tool is not able to parse this back to the original form.CborError cbor_value_to_json_advance | ( | FILE * | out, |
CborValue * | value, | ||
int | flags | ||
) |
Converts the current CBOR type pointed to by value to JSON and writes that to the out stream.
If an error occurs, this function returns an error code similar to CborParsing. The flags parameter indicates one or more of the flags from CborToJsonFlags that control the conversion.
If no error ocurred, this function advances value to the next element.