From f2522f9ae58c1c163022a1fdc8b39a55dcc899a4 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Sat, 24 Aug 2024 15:01:24 -0500 Subject: [PATCH] Adds *DEFAULT-JSON-TYPE* to JSON-LD Allows customization of the type assumed for an unrecognized JSON-LD object-type. --- src/json-ld.lisp | 15 ++++++++++++--- t/activity-vocabulary.lisp | 3 ++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/json-ld.lisp b/src/json-ld.lisp index 6c6cda9..00a33c1 100644 --- a/src/json-ld.lisp +++ b/src/json-ld.lisp @@ -23,6 +23,8 @@ #:parse #:define-json-type ;; Symbols #:no-@context + ;; Globals + #:*default-json-type* ;; Accessors #:json-ld-context #:json-ld-etc #:json-ld-id #:json-ld-type ;; Slots @@ -33,6 +35,13 @@ ;;; Globals ;;; ———————————————————————————————————————— +(defvar *default-json-type* "*" + "When parsing JSON-LD, objects of unrecognized types will be assumed to be +of this type. Should be a string, the IRI corresponding to a registered type. +For example: “https://www.w3.org/ns/activitystreams#Object” + +The default value “*” refers to the base JSON-LD-OBJECT type.") + (defvar *http-cache* (make-hash-table :test #'equal)) (defvar *json-types* (make-hash-table :test #'equal)) @@ -277,9 +286,9 @@ name, though it might be unresolved if context was unprovided or lacking." (let ((ctx (parse-context (gethash "@context" table) ctx))) ;; Now, actually parse. (let* ((parsed-table (parse-table-inplace table ctx)) - (type (identify-json-type table ctx)) - (type-def (or (gethash type *json-types*) - (gethash "*" *json-types*)))) + (type (identify-json-type parsed-table ctx)) + (type-def (or (gethash type *json-types*) + (gethash *default-json-type* *json-types*)))) (parse-table-into-object parsed-table type-def ctx)))) (defun parse-table-inplace (table ctx) diff --git a/t/activity-vocabulary.lisp b/t/activity-vocabulary.lisp index 8e227c1..049ee7f 100644 --- a/t/activity-vocabulary.lisp +++ b/t/activity-vocabulary.lisp @@ -24,7 +24,8 @@ (defun run () "Run all ACTIVITY-VOCABULARY tests." - (lisp-unit2:run-tests :package :activity-servist/tests/activity-vocabulary)) + (let ((json-ld:*default-json-type* "https://www.w3.org/ns/activitystreams#Object")) + (lisp-unit2:run-tests :package :activity-servist/tests/activity-vocabulary))) (defun run-with-summary () "Run tests with summary for ACTIVITY-VOCABULARY."