Skip to content

Deserialization

Martin Ledvinka edited this page Dec 16, 2024 · 3 revisions

Deserialization can handle most kinds of JSON-LD formats, because we are using Titanium JSON-LD to process the input before parsing it. Therefore, compacted, flattened, framed or expanded forms of JSON-LD can be all handled by the deserializer.

Again, multiple references to the same entity in the JSON should be realized by using the entity's IRI in all but the first occurrence.

Object References

When deserializing JSON-LD input, JB4JSON-LD will attempt to use the same instance for occurrences of an object with the same identifier. In particular, it expects the input contains the object once in full form (with all attributes) and all its other occurrences can be just references using the id. For example, in the following snippet, the memberOf and headOf will point to the same Java instance:

{
  "@context": {
    "uri": "@id",
    "types": "@type",
    "firstName": "http://xmlns.com/foaf/0.1/firstName",
    "lastName": "http://xmlns.com/foaf/0.1/lastName",
    "username": "http://xmlns.com/foaf/0.1/accountName",
    "name": "http://xmlns.com/foaf/0.1/name",
    "memberOf": "http://example.com/ontologies/organization/memberOf",
    "headOf": "http://example.com/ontologies/organization/headOf",
  },
  "uri": "http://example.com/organizations/world-government/members/Adam+Thorn",
  "types": ["http://xmlns.com/foaf/0.1/Person"],
  "firstName": "Adam",
  "lastName": "Thorn",
  "username": "[email protected]",
  "memberOf": {
    "@id": "http://example.com/organizations/world-government",
    "name": "World government",
    "types": ["http://xmlns.com/foaf/0.1/Organization"]
  },
  "headOf": {
    "@id": "http://example.com/organizations/world-government"
  }
}

Target Type

When deserializing an object, JB4JSON-LD tries to match the values of the object's @type attribute with the type mapped by the target Java class. Class hierarchies are supported, so the target class may be a superclass and JB4JSON-LD will instantiate the appropriate subclass.

If the JSON-LD object does not contain type information, an exception may be thrown (if the target class has, for example, subclasses). To force deserialization to use the requested target class, use the assumeTargetType parameter. The same solution may be applied for cases where the JSON-LD object has only the @id attribute and JB4JSON-LD would normally treat it as a reference waiting to be resolved to a full object. Setting assumeTargetType makes JB4JSON-LD just instantiate the target class if no such full object is found, otherwise a UnresolvedReferenceException would be thrown.

Clone this wiki locally