Skip to content

Serialization

Martin Ledvinka edited this page Apr 6, 2023 · 3 revisions

Currently, the library outputs JSON-LD corresponding the the compacted format. Since version 0.10.0, it is possible to select whether the output will contain @context or whether the serializer will use full IRIs as attributes of the JSON object. The two corresponding classes are:

  • cz.cvut.kbss.jsonld.serialization.CompactedJsonLdSerializer
  • cz.cvut.kbss.jsonld.serialization.ContextBuildingJsonLdSerializer

Data properties are serialized as attribute values, object properties are serialized as embedded objects. If an instance is referenced in multiple places, only the first occurrence is written as a full JSON object, other occurrences are serialized as IRIs of the instance.

Context-based Serialization

Serialization with context is more favorable with respect to legacy clients that may not support JSON-LD. In that case, the output is a regular JSON with an additional @context attribute that the client may safely ignore.

Several configuration options are available for context-based serializer.

Serialization of Individuals as String

By default, an individual (value of an attribute of type URI/URL/String or an enum constant mapped to an individual) is serialized as an object with a single attribute - @id as follows:

{
  "@context": {
    "reference": "http://purl.org/dc/terms/references"
  },
  "reference": {
    "@id": "http://example.org/A"
  }
}

However, the serializeIndividualsUsingExpandedDefinition parameter can be used to tell JB4JSON-LD to serialize such references as plain string with an expanded term definition in the context. The output looks as follows:

{
  "@context": {
    "reference": {
      "@id": "http://purl.org/dc/terms/references",
      "@type": "@id"
    }
  },
  "reference": "http://example.org/A"
}
Clone this wiki locally