Couchbase Lite C
Couchbase Lite C API
Loading...
Searching...
No Matches
JSON Interoperability

Converting to JSON

These are convenience functions that directly return a JSON representation of a value.

For more control over the encoding, use an Fleece Encoders with format kFLEncodeJSON.

FLEECE_PUBLIC FLStringResult FLValue_ToJSON (FLValue FL_NULLABLE)
 Encodes a Fleece value as JSON (or a JSON fragment.)
FLEECE_PUBLIC FLStringResult FLValue_ToJSON5 (FLValue FL_NULLABLE)
 Encodes a Fleece value as JSON5, a more lenient variant of JSON that allows dictionary keys to be unquoted if they're alphanumeric.
FLEECE_PUBLIC FLStringResult FLValue_ToJSONX (FLValue FL_NULLABLE v, bool json5, bool canonicalForm)
 Most general Fleece to JSON converter.

Parsing JSON to Fleece Values

NODISCARD FLEECE_PUBLIC FLDoc FLDoc_FromJSON (FLSlice json, FLError *FL_NULLABLE outError)
 Creates an FLDoc from JSON-encoded data.
NODISCARD FLEECE_PUBLIC FLMutableArray FL_NULLABLE FLMutableArray_NewFromJSON (FLString json, FLError *FL_NULLABLE outError)
 Creates a new mutable Array from JSON.
NODISCARD FLEECE_PUBLIC FLMutableDict FL_NULLABLE FLMutableDict_NewFromJSON (FLString json, FLError *FL_NULLABLE outError)
 Creates a new mutable Dict from json.
FLEECE_PUBLIC bool FLEncoder_ConvertJSON (FLEncoder, FLSlice json)
 Parses JSON data and writes the value(s) to the encoder as their Fleece equivalents.

Detailed Description

Function Documentation

◆ FLDoc_FromJSON()

NODISCARD FLEECE_PUBLIC FLDoc FLDoc_FromJSON ( FLSlice json,
FLError *FL_NULLABLE outError )

Creates an FLDoc from JSON-encoded data.

The data is first encoded into Fleece, and the Fleece data is kept by the doc; the input JSON data is no longer needed after this function returns.

◆ FLEncoder_ConvertJSON()

FLEECE_PUBLIC bool FLEncoder_ConvertJSON ( FLEncoder ,
FLSlice json )

Parses JSON data and writes the value(s) to the encoder as their Fleece equivalents.

(This acts as a single write, like WriteInt; it's just that the value written is likely to be an entire dictionary or array.)

◆ FLMutableArray_NewFromJSON()

NODISCARD FLEECE_PUBLIC FLMutableArray FL_NULLABLE FLMutableArray_NewFromJSON ( FLString json,
FLError *FL_NULLABLE outError )

Creates a new mutable Array from JSON.

It is an error if the JSON is not an array. Its initial ref-count is 1, so a call to FLMutableArray_Release will free it.

◆ FLMutableDict_NewFromJSON()

NODISCARD FLEECE_PUBLIC FLMutableDict FL_NULLABLE FLMutableDict_NewFromJSON ( FLString json,
FLError *FL_NULLABLE outError )

Creates a new mutable Dict from json.

It is an error if the JSON is not a dictionary/object. Its initial ref-count is 1, so a call to FLMutableDict_Release will free it.

◆ FLValue_ToJSON()

FLEECE_PUBLIC FLStringResult FLValue_ToJSON ( FLValue FL_NULLABLE)

Encodes a Fleece value as JSON (or a JSON fragment.)

Note
Any Data values will be encoded as base64-encoded strings.

◆ FLValue_ToJSON5()

FLEECE_PUBLIC FLStringResult FLValue_ToJSON5 ( FLValue FL_NULLABLE)

Encodes a Fleece value as JSON5, a more lenient variant of JSON that allows dictionary keys to be unquoted if they're alphanumeric.

This tends to be more readable.

Note
Any Data values will be encoded as base64-encoded strings.

◆ FLValue_ToJSONX()

FLEECE_PUBLIC FLStringResult FLValue_ToJSONX ( FLValue FL_NULLABLE v,
bool json5,
bool canonicalForm )

Most general Fleece to JSON converter.

Parameters
vThe Fleece value to encode
json5If true, outputs JSON5, like FLValue_ToJSON5
canonicalFormIf true, outputs the JSON in a consistent "canonical" form. All equivalent values should produce byte-for-byte identical canonical JSON. This is useful for creating digital signatures, for example.