TinyCBOR 0.5.2 API
|
Group of functions used to convert CBOR to text form. More...
Enumerations | |
enum | CborPrettyFlags |
The CborPrettyFlags enum contains flags that control the conversion of CBOR to text format. More... | |
Functions | |
CborError | cbor_value_to_pretty_stream (CborStreamFunction streamFunction, void *token, CborValue *value, int flags) |
Converts the current CBOR type pointed by value to its textual representation and writes it to the stream by calling the streamFunction. More... | |
Group of functions used to convert CBOR to text form.
This group contains two functions that can be used to convert a CborValue object to a text representation. This module attempts to follow the recommendations from RFC 7049 section 6 "Diagnostic Notation", though it has a few differences. They are noted below.
TinyCBOR does not provide a way to convert from the text representation back to encoded form. To produce a text form meant to be parsed, CborToJson is recommended instead.
Either of the functions in this section will attempt to convert exactly one CborValue object to text. 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.
As described in RFC 7049 section 6 "Diagnostic Notation", the format is largely borrowed from JSON, but modified to suit CBOR's different data types. TinyCBOR makes further modifications to distinguish different, but similar values.
CBOR values are currently encoded as follows:
"h'"
followed by the Base16 (hex) representation of the binary data, followed by an ending quote (') "simple(nn)"
where nn
is the simple value null
undefined
true
or false
NaN
or infinite
. Otherwise, the decimal representation with as many digits as necessary to ensure no loss of information. By default, float values are suffixed by "f" and half-float values suffixed by "f16" (doubles have no suffix). If the CborPrettyNumericEncodingIndicators flag is active, the values instead are encoded following the Section 6 recommended encoding indicators: float values are suffixed with "_2" and half-float with "_1". A decimal point is always present. The CborPrettyFlags enumerator contains flags to control some aspects of the encoding:
enum CborPrettyFlags |
The CborPrettyFlags enum contains flags that control the conversion of CBOR to text format.
CborPrettyNumericEncodingIndicators
Use numeric encoding indicators instead of textual for float and half-float. CborPrettyTextualEncodingIndicators
Use textual encoding indicators for float ("f") and half-float ("f16"). CborPrettyIndicateIndeterminateLength
(default) Indicate when a map or array has indeterminate length. CborPrettyIndicateOverlongNumbers
Indicate when a number or length was encoded with more bytes than needed. CborPrettyShowStringFragments
If the byte or text string is transmitted in chunks, show each individually. CborPrettyMergeStringFragment
Merge all chunked byte or text strings and display them in a single entry. CborPrettyDefaultFlags
Default conversion flags. CborError cbor_value_to_pretty_stream | ( | CborStreamFunction | streamFunction, |
void * | token, | ||
CborValue * | value, | ||
int | flags | ||
) |
Converts the current CBOR type pointed by value to its textual representation and writes it to the stream by calling the streamFunction.
If an error occurs, this function returns an error code similar to Parsing CBOR streams.
The textual representation can be controlled by the flags parameter (see CborPrettyFlags for more information).
If no error ocurred, this function advances value to the next element. Often, concatenating the text representation of multiple elements can be done by appending a comma to the output stream in between calls to this function.
The streamFunction function will be called with the token value as the first parameter and a printf-style format string as the second, with a variable number of further parameters.
Referenced by cbor_value_to_pretty_advance(), and cbor_value_to_pretty_advance_flags().