Copyright © 2020-2025 the Contributors to the CBOR-LD 1.0 Specification, published by the JSON for Linked Data Community Group under the W3C Community Contributor License Agreement (CLA). A human-readable summary is available.
CBOR is a compact binary data serialization and messaging format. This specification defines CBOR-LD 1.0, a CBOR-based format to serialize Linked Data. The encoding is designed to leverage the existing JSON-LD ecosystem, which is deployed on hundreds of millions of systems today, to provide a compact serialization format for those seeking efficient encoding schemes for Linked Data. By utilizing semantic compression schemes, compression ratios in excess of 60% better than generalized compression schemes are possible. This format is primarily intended to be a way to use Linked Data in storage and bandwidth constrained programming environments, to build interoperable semantic wire-level protocols, and to efficiently store Linked Data in CBOR-based storage engines.
This specification was published by the JSON for Linked Data Community Group. It is not a W3C Standard nor is it on the W3C Standards Track. Please note that under the W3C Community Contributor License Agreement (CLA) there is a limited opt-out and other conditions apply. Learn more about W3C Community and Business Groups.
This document is experimental.
There is a reference implementation that is capable of demonstrating the features described in this document.
GitHub Issues are preferred for discussion of this specification.
This section is non-normative.
CBOR is a compact binary data serialization and messaging format. This specification defines CBOR-LD 1.0, a CBOR-based format to serialize Linked Data. The encoding is designed to leverage the existing JSON-LD ecosystem, which is deployed on hundreds of millions of systems today, to provide a compact serialization format for those seeking efficient encoding schemes for Linked Data. By utilizing semantic compression schemes, compression ratios in excess of 60% better than generalized compression schemes are possible. This format is primarily intended to be a way to use Linked Data in storage and bandwidth constrained programming environments, to build interoperable semantic wire-level protocols, and to efficiently store Linked Data in CBOR-based storage engines.
This section is non-normative.
This document is a detailed specification for a serialization of Linked Data in CBOR. The document is primarily intended for the following audiences:
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key word MUST in this document is to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, it appears in all capitals, as shown here.
This section is non-normative.
There are a number of ways that one may participate in the development of this specification:
This section is non-normative.
CBOR-LD satisfies the following design goals:
Similarly, the following are non-goals.
The following minefields have been identified while working on this specification:
This section is non-normative.
The general CBOR-LD encoding algorithm takes a JSON-LD Document and does the following:
"@type":
"@id"
, replace the value with the byte value associated with the term.
"@type": "@id"
, replace the value with the byte value associated with the
term.
The first step in decoding a CBOR-LD payload is to recreate the term codec map that was used to encode it by processing the contexts in the payload. However, the contexts needed to create the term codec map can have their URLs encoded as integers by CBOR-LD. If a CBOR-LD payload contains context URLs compressed in such a way, the consumer of the CBOR-LD needs to know what compression tables (maps from JSON-LD terms to integers) were used to compress the context URLs during creation to be able to reconstruct the term codec map. The following sections define the exact mechanism by which this can be accomplished, allowing an arbitrary CBOR-LD consumer to decompress any CBOR-LD payload that conforms to this specification.
To this end, we have registered the CBOR tag 0xCB1D
(tag value 51997) to be used for CBOR-LD.
The data that follows this tag value is used to look up what compression table(s) are needed
to decompress the CBOR-LD context URLs.
This exact tag value has not yet been officially registered with the IANA CBOR Tag Registry. The exact value is subject to change.
To enable unbounded extension on possible use cases for CBOR-LD that require different compression table material for consumption while working with a single CBOR tag value, we define the following.
CBOR-LD payloads MUST be structured such that the item tagged with tag 0xCB1D
is
a two-element array, and the first element MUST be a major type 0 integer. This
integer is a CBOR-LD Registry Entry ID.
The value of the CBOR-LD Registry Entry ID is then used to look up a CBOR-LD Registry Entry in the CBOR-LD Registry.
The CBOR-LD Registry is a global list that provides consumers of CBOR-LD payloads the information they need to reconstruct the term codec map required for decompression. A CBOR-LD Registry Entry contains the following:
typeTables
: an array containing what Type Tables
are to be used for this
type of payload.
processingModel
: what processing model is used for this registry entry. A processing model
specifies how auto-generated CBOR-LD values are created from JSON-LD contexts as well as what
type encoders are used alongside the Type Tables
(e.g. how to partially compress an
xsd:dateTime
value that does not appear in Type Table
). The default processing model,
which will be defined later in this specification, will be used unless otherwise specified
in the Registry Entry.
The typeTables
associated with a CBOR-LD Registry Entry MUST be an array of
or JSON objects. The only exception is the string "callerProvidedTable", which may appear in this array,
denoting that for this use case, a Type Table
is required which is not globally
defined.
Dereferencing one of these URLs MUST result in a JSON object with the following properties:
type
: a JSON-LD type.
table
: a JSON object that maps values of the above type to integers.
If a JSON object is present in the typeTables
array, it MUST be in the above format.
The following is the current CBOR-LD registry:
Registry Entry Id | Use Case | Processing Model | Provisional | typeTables |
---|---|---|---|---|
0 | Uncompressed CBORLD | DEFAULT | No | None |
1 | Compressed CBORLD, default use case. | DEFAULT | No | DEFAULT |
100 | Verifiable Credential Barcodes Specification Test Vectors | DEFAULT | Yes |
|
10001 | Provisional California DMV Credentials | DEFAULT | Yes |
|
10002 | Provisional First Responder Credentials | DEFAULT | Yes |
|
31000000 | California DL/ID Barcodes | DEFAULT | Yes |
|
This algorithm takes a map typeTable
, an integer registryEntryId
, and a JSON-LD
document jsonldDocument
as inputs, and returns a hexadecimal string cborldBytes
.
prefix
to the result of passing registryEntryId
to
4.6.1 Get Varint Structure Algorithm.
state
to an empty map.
state.strategy
to "compression".
state.typeTable
to typeTable
.
state.registryEntryId
to registryEntryId
.
state
to the result of passing state
to
4.2.1 Initialize Conversion Algorithm.
output
to the result of passing state
and jsonldDocument
as inputDocument
to 4.2.2 Convert Document Algorithm.
suffix
to the CBOR encoding of output
.
cborldBytes
to a hexidecimal encoding of prefix
prepended to suffix
.
cborldBytes
.
This algorithm takes a CBOR-LD payload cborldBytes
, and returns a JSON-LD document jsonldDocument
.
result
to the result of passing registryEntryId
and cborldBytes
to
4.6.1 Get Varint Structure Algorithm.
state
.registryEntryId
to result
.registryEntryId
and suffix
to result
.suffix
.
state
to an empty map.
state.strategy
to "decompression".
type
: map
in the typeTables
array in the CBOR-LD Varint Registry Entry
associated with registryEntryId
, add that entry to state
.typeTable
, and set the value of type
in state
.reverseTypeTable
to inverseMap
, where inverseMap
is map
with the mapping inverted.
state
to the result of passing state
to
4.2.1 Initialize Conversion Algorithm.
input
to the result of decoding suffix
from bytes to a map.
jsonldDocument
to the result of passing state
and input
as inputDocuments
to 4.2.2 Convert Document Algorithm.
jsonldDocument
.
The algorithms in this section describe the behavior of a "converter" for abstractly converting inputs between data forms. When used in conjunction with a "strategy", such as the "compression" and "decompression" strategies defined later in this section, these algorithms can be instantiated to convert between concrete data forms. The "compression" strategy converts from JSON-LD to CBOR-LD, while the "decompression" strategy converts from CBOR-LD to JSON-LD.
This algorithm takes and returns a map state
.
state
to the result of passing state
to
4.4.1 Initialize Context Loader Algorithm.
state.initialActiveContext
to the result of passing empty maps termMap
and
previousActiveContext
to 4.3.1 Initialize Active Context Algorithm.
state.typesEncodedAsBytes
to an empty set.
state.typesEncodedAsBytes
.
state
.
This algorithm takes a map state
and a map or array of
maps inputDocuments
, and returns a map containing a map state
and a map or
array of maps outputMaps
.
inputDocuments
is an array, set inputs
to inputDocuments
. Otherwise, set inputs
to [inputDocuments]
.
outputMaps
to an empty array.
input
in inputs
:
output
to an empty map.
result
to the result of passing state
, input
, output
, and
state.initialActiveContext
as activeContext
to
4.2.3 General Conversion Algorithm.
result.output
to outputMaps
.
state
to result.state
.
inputDocuments
is an array, return outputMaps
. Otherwise, return the
first element of outputMaps
.
This algorithm takes maps input
, output
, state
, and activeContext
as inputs, and returns a map containing maps state
and output
.
state.strategy
is set to "compression":
contextConversionResult
to the result of
4.2.5.1 Convert Contexts for Compression Algorithm, passing
state
, activeContext
, input
, and output
.
activeContext
to contextConversionResult.activeContext
, output
to contextConversionResult.output
, and state
to contextConversionResult.state
.
activeContext
to
result.activeContext
and state
to result.state
of result
resulting from
4.2.6.1 Convert Contexts for Decompression Algorithm, passing state
,
activeContext
, input
, and output
.
state.strategy
is set to "compression", set state
to result.state
and
objectTypes
to result.objectTypes
for result
resulting from
4.2.5.4 Get Object Types for Compression Algorithm, passing state
,
activeContext
, input
, and output
.
state
to result.state
and objectTypes
to result.objectTypes
for result
resulting from 4.2.6.4 Get Object Types for Decompression Algorithm,
passing state
, activeContext
, input
, and output
.
activeContext
to the result of passing activeContext
and objectTypes
to
4.3.4 Apply Type Scoped Contexts Algorithm.
state.strategy
is set to "compression", set state
to result.state
and
termEntries
to result.termEntries
for result
resulting from
4.2.5.3 Get Input Entries for Compression Algorithm, passing state
,
input
, and activeContext
.
state
to result.state
, output
to result.output
,
and termEntries
to result.termEntries
for result
resulting from
4.2.6.3 Get Input Entries for Decompression Algorithm,
passing state
, input
, output, and activeContext
.
[termInfo, value]
in termEntries
:
term
to termInfo.term
.
valueActiveContext
to the result of passing activeContext
and term
to
4.3.3 Apply Property Scoped Contexts Algorithm.
plural
to the value of termInfo.plural
and termType
to the value of @type
in termInfo.def
.
plural
is set to true
, set values
to the value of value
. Otherwise, set values
to
an array containing the value of value
as a single element.
outputs
to an empty array.
unconvertedValue
in values
:
result
to the result of 4.2.4 Convert Value Algorithm,
passing state
, termType
, unconvertedValue
as value
, and valueActiveContext
as activeContext
.
state
to result.state
and add result.output
to outputs
.
plural
is set to true
, set outputValues
to outputs
. Otherwise, set outputValues
to the first element of outputs
.
state.strategy
is set to "compression", set the value of termInfo.termId
to map to
outputValues
in output
. Otherwise, set the value of termInfo.term
to map to outputValues
in output
.
result
to be an empty map.
result.state
to state
and result.output
to output
.
result
.
This algorithm takes maps state
, activeContext
, termInfo
, and values value
and termType
.
It returns a result
object containing maps state
and output
.
value
is null
, return null
.
state
.strategy
is set to "compression", set output
to the result of passing state
, termType
,
termInfo
, and value
to 4.2.5.2 Convert Value for Compression Algorithm.
output
to the result of passing state
, termType
,
termInfo
, and value
to 4.2.6.2 Convert Value for Decompression Algorithm.
output
is defined, return result
, a map contatining state
and output
.
value
is an array:
outputs
to be an empty array.
element
of value
:
result
be the result of 4.2.4 Convert Value Algorithm, passing activeContext
, state
,
termInfo
, termType
, and element
as value
. Set state
to result
.state
and add result
.output
to outputs
.
result
to be an empty map. Set result
.state
to state
and result
.output
to outputs
.
result
.
output
to an empty map.
result
to the result of 4.2.3 General Conversion Algorithm, passing state
,
activeContext
, value
as input
, and output
.
result
.
The algorithms in this section define the "compression" strategy to be used with the "conversion" algorithms defined previously to convert JSON-LD to CBOR-LD.
This algorithm takes maps state
, activeContext
, input
, and output
,
and returns a map result
containing maps output
, state
, and activeContext
.
applyEmbeddedResult
to the result of 4.3.2 Apply Embedded Contexts Algorithm,
passing state
, activeContext
, and input
.
activeContext
to applyEmbeddedResult.activeContext
and state
to
applyEmbeddedResult.state
.
input
:
result
to an empty map.
result.state
to state
and result.activeContext
to activeContext
.
result
.
context
to the value of "@context" in input
.
encodedContexts
to an empty array.
context
is an array, set isArray
to true
and contexts
to context
.
Otherwise, set isArray
to false
and contexts
to [context]
.
contextValue
in contexts
:
encoderData
to the result of 4.5.1.1 Create Context Encoder, passing
state.typeTable
and contextValue
.
encoderData
is an empty map, add contextValue
to encodedContexts
.
encoderData
to encodedContexts
.
isArray
is true
, set id
to the value of "@context" in state.keywordsMap
plus 1
and set the value of id
in output
to encodedContexts
.
id
to the value of "@context" in state.keywordMap
and set the value of
id
in output
to the first element of encodedContexts
.
result.output
to output
, result.state
to state
, and result.activeContext
to activeContext
.
result
.
This algorithm takes maps state
and termInfo
, and values valueToEncode
and termType
,
and returns a map encoderData
.
valueToEncode
is an object, return.
result
to the result of 4.5.2.1 Create Value Encoder, passing
state
, termInfo
, valueToToEncode
, and termType
.
result
.
This algorithm takes maps state
, activeContext
, and input
, and returns a map state
and an array entries
.
entries
as an empty array.
keys
to the keys of input
, sorted lexicographically.
key
in keys
:
key
is "@context", continue.
value
to the value of key
in input
.
value
is an array, set plural
to true
. Otherwise, set plural
to false
.
key
does not have an entry in state
.termToId
, set termId
to key
.
plural
is true
, set termId
to the value of key
in state
.termToId
plus 1.
termId
to the value of key
in state
.termToId
.
activeContext
.termMap
has an entry for key
, set definition
to the value of key
in
activeContext
.termMap
. Otherwise, set definition
to an empty map.
entryTerm
to be a new map.
entryTerm
to be the value of key
. Add termId
, plural
, and definition
to entryTerm
.
entry
with two elements, entryTerm
and value
.
entry
to entries
.
result
containing entries
and state
.
This algorithm takes maps activeContext
and input
, and returns a set
objectTypes
.
objectTypes
to be an empty set.
term
in activeContext
.typeTerms
:
term
has an entry in input
:
types
to the value of term
in input
.
types
to objectTypes
.
objectTypes
.
The algorithms in this section define the "decompression" strategy to be used with the "conversion" algorithms defined previously to convert CBOR-LD to JSON-LD.
This algorithm takes maps state
, activeContext
, input
, and output
, and returns a map
result
containing maps output
, state
, and activeContext
.
decoderData
to the result of 4.5.1.3 Create Context Decoder, passing state
.reverseTypeTable
.
contextTermId
to the value of "@context" in state
.keywordsMap
.
contextTermId
has an entry in input
, set the value of
"@context" in output
to the result of 4.5.1.4 Decode Context, passing decoderData
and
the value of contextTermId
in input
as value
.
contextTermIdPlural
to the value of contextTermId
plus 1.
contextTermIdPlural
has an entry in input
:
contextTermId
also had an entry in input
during the previous check, throw an
ERR_INVALID_ENCODED_CONTEXT
error.
encodedContexts
to be the value of contextTermIdPlural
in input
. If encodedContexts
is
not an array, throw an ERR_INVALID_ENCODED_CONTEXT
error.
contexts
to be an empty array.
valueToDecode
in encodedContexts
, add the result of passing decoderData
and valueToDecode
as value
to 4.5.1.4 Decode Context to contexts
.
output
to contexts
.
embeddedContextResult
to the result of 4.3.2 Apply Embedded Contexts Algorithm, passing
activeContext
, output
as input
, and state
.
result
to an empty map.
result
.state
to embeddedContextResult
.state
and result.activeContext
to
embeddedContextResult
.activeContext
.
result
.
This algorithm takes maps state
and termInfo
, and values termType
and valueToDecode
, and returns a value
decodedValue
.
value
is a map, return.
decoderData
to the result of 4.5.2.3 Create Value Decoder, passing valueToDecode
,
state
, termInfo
, and termType
.
decodedValue
to the result of 4.5.2.4 Decode Value, passing decoderData
.
decodedValue
.
This algorithm takes maps state
, activeContext
, and input
, and returns a map state
and an array
entries
.
entries
to an empty array.
key
and value
in input
:
key
is the value of "@context" in state
.keywordsMap
or that value plus 1, continue.
key
is a string, set plural
to false and term
to key
.
key
is odd, set plural
to true. Otherwise, set plural
to false.
plural
is true
, set term
to the value of id
minus 1 in state
.idToTerm
.
If that value does not have an entry, throw an error ERR_UNKNOWN_CBORLD_TERM_ID.
term
to the value of id
in state
.idToTerm
. If that value does not have
an entry, throw an error ERR_UNKNOWN_CBORLD_TERM_ID.
definition
to the value of term
in activeContext
.termMap
.
entryTerm
to be a new map.
entryTerm
to be the value of key
. Add term
, plural
, and definition
to entryTerm
.
entry
with two elements, entryTerm
and value
.
entry
to entries
.
entries
by the value of term
in each element of entries
.
result
containing entries
and state
.
This algorithm takes maps state
, activeContext
, input
as inputs, and
returns a map state
and a set objectTypes
.
objectTypes
to be an empty set.
term
in activeContext
.typeTerms
:
term
does not have an entry in state
.termToId
, set termId
to term
.
termId
to the value of term
in state
.termToId
.
termId
nor termId
plus 1 are present in input
, continue.
termId
is present in input
, set value
to the value of termId
in input
.
value
to the value of termId
plus 1 in input
.
key
is a string, set plural
to false and term
to key
.
key
is odd, set plural
to true. Otherwise, set plural
to false.
plural
is true
, set term
to the value of id
minus 1 in state
.idToTerm
.
If that value does not have an entry, throw an error ERR_UNKNOWN_CBORLD_TERM_ID.
term
to the value of id
in state
.idToTerm
. If that value does not have
an entry, throw an error ERR_UNKNOWN_CBORLD_TERM_ID.
definition
to the value of term
in activeContext
.termMap
.
termInfo
to be a new map.
term
, termId
, plural
, and definition
to termInfo
.
value
is not an array, set values
to be an array containing as a single element value
.
Otherwise, set values
to the value of value
.
value
in values
:
decoderData
to the result of 4.5.2.3 Create Value Decoder, passing value
,
termInfo
, state
, and "@vocab" as termType
.
decoderData
exists, add the result of 4.5.2.4 Decode Value, passing decoderData
, to `objectTypes.
value
to objectTypes
.
objectTypes
.
The algorithms in this section describe how to determine what components of the context documents associated with a JSON-LD document are in use at any point during compression or decompression. These algorithms include how to apply embedded, type-scoped, and property-scoped contexts with CBOR-LD. This is in contrast to the Context Loading algorithms defined later in this specification, which describe how to construct the mappings from terms to integers that are the core CBOR-LD compression technique. Together, the Active Context Processing and Context Loading algorithms specify how JSON-LD context documents should be processed when converting to and from CBOR-LD.
This algorithm takes maps previousActiveContext
and termMap
,
and returns a map activeContext
. It updates the active context
in use and finds all aliases for '@type'
.
activeContext
to a new map.
activeContext.previousActiveContext
to previousActiveContext
.
activeContext.termMap
to termMap
.
activeContext.typeTerms
to the array ['@type']
.
[term, def]
in termMap
:
def
is "@type", add term
to activeContext.typeTerms
.
activeContext
.
This algorithm takes maps state
, activeContext
, and input
as inputs, and
returns a map result
containing maps state
and activeContext
.
termMapUpdateResult
to the result of passing state
, activeContext.termMap
as
activeTermMap
, and the value of '@context' in input
as contexts
to
4.3.5 Update Term Map Algorithm.
state
to termMapUpdateResult.state
.
termMap
to termMapUpdateResult.activeTermMap
.
newActiveContext
to the result of 4.3.1 Initialize Active Context Algorithm,
passing termMap
and activeContext
as previousActiveContext
.
result
to be a new map, and set result.activeContext
to newActiveContext
and
result.state
to state
.
result
.
This algorithm takes maps state
, activeContext
, and a string term
as inputs and
returns a map result
containing maps state
and activeContext
.
revertedTermMap
to the result of 4.3.6 Revert Term Map Algorithm,
passing activeContext
.
termDef
to the value of term
in activeContext.termMap
. Set contexts
to the value of
"@context" in termDef
.
termMapUpdateResult
to the result of passing state
, revertedTermMap
as
activeTermMap
, true
as propertyScope
, and contexts
to
4.3.5 Update Term Map Algorithm.
state
to termMapUpdateResult.state
.
termMap
to termMapUpdateResult.activeTermMap
.
newActiveContext
to the result of 4.3.1 Initialize Active Context Algorithm,
passing termMap
and activeContext
as previousActiveContext
.
result
to be a new map, and set result.activeContext
to newActiveContext
and
result.state
to state
.
result
.
This algorithm takes maps state
, activeContext
,and a set objectTypes
as inputs, and
returns a map result
containing maps state
and activeContext
.
objectTypesSorted
to an empty array.
objectTypes
and add the elements to
objectTypesSorted
in order.
newTermMap
to activeContext.termMap
.
type
in objectTypesSorted
:
typeDef
to the value of type
in newTermMap
. Set contexts
to the value of
"@context" in typeDef
.
termMapUpdateResult
to the result of passing state
, newTermMap
as
activeTermMap
, contexts
, and true
as typeScope
to
4.3.5 Update Term Map Algorithm.
state
to termMapUpdateResult.state
and newTermMap
to
termMapUpdateResult.activeTermMap
.
newActiveContext
to the result of 4.3.1 Initialize Active Context Algorithm,
passing newTermMap
as termMap
and activeContext
as previousActiveContext
.
result
to be a new map, and set result.activeContext
to newActiveContext
and
result.state
to state
.
result
.
This algorithm takes maps state
, activeTermMap
, and map or array contexts
as well as booleans typeScope
and propertyScope
, both of which default to false
if not provided, as inputs. It returns maps state
and activeTermMap
.
contexts
is not an array, set contexts
to be an array with the previous value of contexts
as its sole element.
allowProtectedOverride
to the value of propertyScope
.
propagateDefault
to the negation of the value of typeScope
.
contextIdentifier
in contexts
:
loadResult
to the result of 4.4.2 Load Context Algorithm, passing state
and
contextIdentifier
.
entry
to loadResult
.entry
, context
to entry
.context
, and state
to loadResult
.state
.
@propagate
appears in context
, set propagate
to the value of @propagate
in context
.
Otherwise, set propagate
to the value of propagateDefault
.
newTermMap
to be an empty map. For [key
, value
] in entry
.termMap
:
value
into a new map newValue
and add propagate
to newValue
.
key
in newTermMap
to newValue
.
term
, activeDef
] in activeTermMap
:
def
be the value of term
in newTermMap
.
def
is defined:
protected
in activeDef
is true
:
allowProtectedOverride
is set to false
and def
is not identical to activeDef
,
throw an error ERR_PROTECTED_TERM_REDEFINITION.
term
in newTermMap
to a map containing the values from activeDef
and propagate
set to the value of def
.propagate
.
term
appears in context
, set the value of term
in newTermMap
to
a map containing all values from activeDef
.
activeTermMap
to the value of newTermMap
.
result
to be an empty map.
result
.state
to state
and result
.activeTermMap
to activeTermMap
.
result
.
This algorithm takes as input a map activeContext
, and returns a map
newTermMap
.
newTermMap
to an empty map.
nonPropagatingTerms
to an empty array.
[term, def]
in activeContext
:
def.propagate
is set to false
, add term
to nonPropagatingTerms
and proceed to the next iteration of this loop.
term
in newTermMap
to def
.
term
in nonPropagatingTerms
:
currentContext
to activeContext.previousActiveContext
.
def
to the value of term
in currentContext.termMap
.
def
is not undefined and def.propagate
is set to false
:
currentContext
to activeContext.previousActiveContext
.
def
to the value of term
in currentContext.termMap
.
def
is not undefined, set the value of term
in newTermMap
to def
.
newTermMap
.
The algorithms in this section define how to construct the mappings between terms and integers that are used as the core CBOR-LD compression technique.
This algorithm takes and returns a map state
.
state.contextMap
to a new map.
state.nextTermId
to 100.
state.keywordsMap
to the following map of JSON-LD keywords to their associated
integer values:
{ '@context' => 0, '@type' => 2, '@id' => 4, '@value' => 6, '@direction' => 8, '@graph' => 10, '@included' => 12, '@index' => 14, '@json' => 16, '@language' => 18, '@list' => 20, '@nest' => 22, '@reverse' => 24, '@base' => 26, '@container' => 28, '@default' => 30, '@embed' => 32, '@explicit' => 34, '@none' => 36, '@omitDefault' => 38, '@prefix' => 40, '@preserve' => 42, '@protected' => 44, '@requireAll' => 46, '@set' => 48, '@version' => 50, '@vocab' => 52, '@propagate' => 54 }
state.keywordsMap
to state.termToId
.
state.strategy
is set to "decompression", set state.idToTerm
to the reverse map
of state.termToId
(i.e., a map from integers to JSON-LD keywords).
state
.
This algorithm takes a map state
and a context map or URL contextIdentifier
, and returns
result
, a map containing maps state
and entry
.
state.contextMap
has an entry for contextIdentifier
:
result
to an empty map.
result.state
to state
.
result.entry
to the value of contextIdentifier
in state.contextMap
.
result
.
context
is a string:
context
to the value
of "@context" in that object.
contextUrl
to the value of contextIdentifier
.
context
to contextIdentifier
.
result
to the result of 4.4.3 Add Context Algorithm, passing
state
, context
, and contextUrl
if set.
result
.
This algorithm takes a map state
, a context object context
, and a context URL contextUrl
,
and returns result
, a map containing maps state
and entry
.
context
has an entry "@import":
importUrl
to the value of "@import" in `context.
state.contextMap
does not have an entry for importUrl
:
importUrl
and set importContext
to the value of
"@context" in that object.
importedContextAdditionResult
to the result of 4.4.3 Add Context Algorithm,
passing state
, importContext
as context
, and importUrl
as contextUrl
.
state
to importedContextAdditionResult.state
and importEntry
to
importedContextAdditionResult.entry
.
importEntry
to the value of importUrl
in state.contextMap
.
context
to a map containing all entries from context
as well as importEntry.context
.
termMap
to an empty map.
entry
to be an object containing context
and termMap
.
sortedTerms
to the result of sorting the keys in context
in lexicographic order.
isProtected
to true
if "@protected" has an entry in context
and false
otherwise.
term
in sortedTerms
:
term
has an entry in state.keywordsMap
, proceed to the next iteration of this loop.
definition
to the value of term
in context
.
definition
is null
, proceed to the next iteration of this loop.
definition
is a string:
newDefinition
to an empty map.
newDefinition
to definition
.
definition
to newDefinition
.
protected
in definition
to isProtected
.
term
in termMap
to definition
.
term
does not have an entry in state.termToId
:
termId
to state.nextTermId
.
state.nextTermId
by 2.
term
in state.termToId
to termId
.
termId
in state.idToTerm
to term
.
contextUrl
is defined, set the value of contextUrl
in
state.contextMap
to entry
.
context
in state.contextMap
to entry
.
result
to be an empty map.
result.state
to state
and result.entry
to entry
.
result
.
The codecs in this section specify exactly how individual values in JSON-LD should be converted to CBOR and vice versa. They are used by the algorithms in the previous section, and allow CBOR-LD to efficiently encode both primitive and non-primitive types as CBOR.
This algorithm takes a map typeTable
and a value contextValue
and
returns a map encoderData
.
encoderData
to an empty map.
contextValue
is not a string, return.
contextTable
to the value of "context" in typeTable
.
encoderData.context
to contextValue
and encoderData.contextTable
to
contextTable
.
encoderData
.
This algorithm takes a map encoderData
, and returns CBOR binary data.
encoderData
.context
has an entry in encoderData
.contextTable
, return a CBOR encoding
of the value of encoderData
.context
in encoderData
as a Major Type 0 (unsigned integer) object.
encoderData
.context
as a Major Type 3
(text string) object.
This algorithm takes a map reverseTypeTable
, and
returns a map encoderData
.
reverseContextTable
to the value of "context" in reverseTypeTable
.
decoderData
to an empty map.
decoderData
.reverseContextTable
to the value of reverseContextTable
and
return decoderData
.
This algorithm takes a map decoderData
and a value value
, and returns a value.
value
is not a number, return value
.
decoderData
.reverseContextTable
has an entry for value
, return
the value of that entry.
This algorithm takes maps state
and termInfo
, and values
termType
and valueToEncode
, and returns a map encoderData
or valueToEncode
.
isUrl
to false
.
termInfo.term
is "@id" or "@type", set isUrl
to true
.
termInfo.def
is "@id" or "@type", set
isUrl
to true.
termType
is "@id" or "@vocab", set isUrl
to true
.
isUrl
is true
, set tableType
to "url".
termType
is defined, set tableType
to termType
.
tableType
to "none".
state.typeTable
has an entry for tableType
:
subTable
to the value of tableType
in state.typeTable
.
subTable
has an entry for valueToEncode
:
intValue
to the value of valueToEncode
in subTable
. Set includeSign
to false
.
state
.typesEncodedAsBytes
has an entry for tableType
, set convertToBytes
to true
.
Otherwise, set convertToBytes
to false
.
tableType
is not "none" and valueToEncode
is an integer:
intValue
to the value of valueToEncode
.
convertToBytes
and includeSign
to true
.
intValue
is defined:
encoderData
to an empty map.
encoderData
.intValue
to the value of intValue
, encoderData
.convertToBytes
to
the value of convertToBytes
, and encoderData
.includeSign
to the value of includeSign
.
encoderData
.
tableType
has an entry in state.processingModeTypeEncoders
, set encoderData
to the result of
calling the Create Encoder
algorithm associated with that entry's codec.
encoderData
is defined, return encoderData
.
valueToEncode
.
This algorithm takes a map encoderData
, and returns CBOR binary data.
encoderData
.convertToBytes
is true
:
bytes
to the result of converting intValue
to bytes, using the value of includeSign
to
determine whether the binary representation of the integer should be signed or unsigned.
bytes
as a Major Type 2 (byte string) object.
intValue
as a Major Type 0 (unsigned integer) object.
This algorithm takes maps state
and termInfo
, and values
termType
and valueToDecode
, and returns a map decoderData
.
isUrl
to false
.
termInfo.term
is "@id" or "@type", set isUrl
to true
.
termInfo.def
is "@id" or "@type", set
isUrl
to true
.
termType
is "@id" or "@vocab", set isUrl
to true
.
isUrl
is true
, set tableType
to "url".
termType
is defined, set tableType
to termType
.
tableType
to "none".
state.reverseTypeTable
has an entry for tableType
:
subTable
to the value of tableType
in state.reverseTypeTable
.
useTable
to false
.
valueToDecode
is a byte array and state
.typesEncodedAsBytes
has an entry
for tableType
:
useTable
to true
.
intValue
to the unsigned integer conversion of the valueToDecode
bytes.
valueToDecode
is an integer and state
.typesEncodedAsBytes
does not
have an entry for tableType
:
useTable
to true
.
intValue
to valueToDecode
.
useTable
is true
:
intValue
is not in subTable
, throw an error ERR_UNKNOWN_COMPRESSED_VALUE.
decoded
to the value of intValue
in subTable
.
valueToDecode
is a byte array and tableType
is not "none", set
decoded
to the integer conversion of valueToDecode
.
decoded
is defined, initialize decoderData
to an empty map, set decoderData
.decoded
to
the value of decoded
, and return decoderData
.
tableType
has an entry in state.processingModeTypeDecoders
, set DecoderData
to the result of
calling the Create Decoder
algorithm associated with that entry's codec.
decoderData
is defined, return decoderData
.
valueToDecode
is not an array, initialize decoderData
to an empty map, set
decoderData
.decoded
to valueToDecode
, and return decoderData
.
This algorithm takes a map decoderData
, and returns a value.
decoderData
.decoded
.
registryEntryId
, and returns a byte string prefix
.
registryEntryId
is less than 128:
varintEncoded
to the byte encoding of registryEntryId
.
prefix
to the result of appending varintEncoded
to the end of the bytes
0xD906
.
varintArray
to an array containing the varint representation of registryEntryId
.
varintTagValue
to varintArray[0]
appended to the end of the bytes 0xD906
.
varintBytesValue
to a CBOR byte string containing the rest of varintArray
appended
to the end of the bytes 0x82
.
prefix
to varintBytesValue
appended to the end of varintTagValue
.
prefix
.
This algorithm takes an encoded CBOR-LD payload cborldBytes
as input, and returns
suffix
, the main data to be decoded, as well as the registryEntryId
value that
should be used to decompress suffix
.
cborldBytes
is not in the range 0x0600
-0x06FF
, throw an
ERR_NON_CBOR_LD_TAG error.
cborldBytes
is in the range 0x0600
-0x067F
, set registryEntryId
to
the value of the last byte of the CBOR tag and set suffix
to the portion of cborldBytes
after the tag.
ERR_INVALID_VARINT_VALUE
error.
ERR_INVALID_VARINT_STRUCTURE
error.
registryEntryId
to the value of the varint for which the first byte is the last
byte of the CBOR tag and the rest of the varint is the first element in the two element array. Set suffix
to the
value of the second element in the array.
result
to be an empty map.
result
.suffix
to the value of suffix
and result
.registryEntryId
to the value of registryEntryId
.
result
.
This section is non-normative.
This specification registers a CBOR tag to allow consumers to identify CBOR-LD payloads. The following is provisional, and has not yet been ratified by IANA.
Tag: 51997
Registry: https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml
Data item: array
Semantics: a tag value of 51997 indicates that the payload is CBOR-LD.
Description of semantics: https://json-ld.github.io/cbor-ld-spec/#cbor-tags-for-cbor-ld
Point of contact: Wesley Smith (wsmith@digitalbazaar.com)