Converting CBOR to JSON

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.

Conversion limitations

When converting from CBOR to JSON, there may be information loss. This section lists the possible scenarios.

enum ConversionStatusFlags
TypeWasNotNative = 0x100
TypeWasTagged = 0x200
NumberPrecisionWasLost = 0x400
NumberWasNaN = 0x800
NumberWasInfinite = 0x1000
NumberWasNegative = 0x2000
FinalTypeMask = 0xff
enum CborToJsonFlags
CborConvertAddMetadata = 1
CborConvertTagsToObjects = 2
CborConvertIgnoreTags = 0
CborConvertObeyByteStringTags = 0
CborConvertByteStringsToBase64Url = 4
CborConvertRequireMapStringKeys = 0
CborConvertStringifyMapKeys = 8
CborConvertDefaultFlags = 0
struct ConversionStatus ConversionStatus
FILE * open_memstream(char ** bufptr, size_t * sizeptr)
cbor.h::CborError value_to_json(FILE * out, CborValue * it, int flags, cbor.h::CborType type, ConversionStatus * status)
cbor.h::CborError dump_bytestring_base16(char ** result, CborValue * it)
cbor.h::CborError generic_dump_base64(char ** result, CborValue * it, const char alphabet)
cbor.h::CborError dump_bytestring_base64(char ** result, CborValue * it)
cbor.h::CborError dump_bytestring_base64url(char ** result, CborValue * it)
cbor.h::CborError add_value_metadata(FILE * out, cbor.h::CborType type, const ConversionStatus * status)
cbor.h::CborError find_tagged_type(CborValue * it, cbor.h::CborTag * tag, cbor.h::CborType * type)
cbor.h::CborError tagged_value_to_json(FILE * out, CborValue * it, int flags, ConversionStatus * status)
cbor.h::CborError stringify_map_key(char ** key, CborValue * it, int flags, cbor.h::CborType type)
cbor.h::CborError array_to_json(FILE * out, CborValue * it, int flags, ConversionStatus * status)
cbor.h::CborError map_to_json(FILE * out, CborValue * it, int flags, ConversionStatus * status)
cbor.h::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.

See also

cborjson.h::cbor_value_to_json(), cbor.h::cbor_value_to_pretty_advance()

cbor.h::CborError cbor_value_to_json(FILE * out, const 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.

See also

cbortojson.c::cbor_value_to_json_advance(), cbor.h::cbor_value_to_pretty()

struct ConversionStatus
cbor.h::CborTag lastTag
uint64_t originalNumber
int flags