Compare commits
2 Enmetoj
6fab9f42d3
...
4df575610e
Author | SHA1 | Date | |
---|---|---|---|
Jaidyn Ann | 4df575610e | ||
Jaidyn Ann | bae4fbbd13 |
|
@ -3,315 +3,129 @@
|
|||
;; Copyright © 2024 Jaidyn Ann <jadedctrl@posteo.at>
|
||||
;;
|
||||
;; This program is free software: you can redistribute it and/or
|
||||
;; modify it under the terms of the GNU General Public License as
|
||||
;; published by the Free Software Foundation, either version 3 of
|
||||
;; modify it under the terms of the GNU Affero General Public License
|
||||
;; as published by the Free Software Foundation, either version 3 of
|
||||
;; the License, or (at your option) any later version.
|
||||
;;
|
||||
;; This program is distributed in the hope that it will be useful,
|
||||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;; GNU General Public License for more details.
|
||||
;; GNU Affero General Public License for more details.
|
||||
;;
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; You should have received a copy of the GNU Affero General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
(defpackage #:activity-servist/activity-streams
|
||||
(:use #:cl)
|
||||
(:nicknames "AS/AS" "ACTIVITY-STREAMS")
|
||||
(:export
|
||||
;; Functions
|
||||
:parse :encode
|
||||
:define-class-encoders
|
||||
;; Globals
|
||||
:*ap-packages* :*default-class*
|
||||
;; Classes
|
||||
:object
|
||||
;; Accessors
|
||||
:object-@context :object-unsupported
|
||||
#'object-actor #'object-attachment #'object-attributed-to #'object-audience
|
||||
#'object-content #'object-context #'object-name #'object-end-time
|
||||
#'object-generator #'object-icon #'object-image #'object-in-reply-to
|
||||
#'object-location #'object-preview #'object-published #'object-replies
|
||||
#'object-start-time #'object-summary #'object-tag #'object-updated
|
||||
#'object-url #'object-to #'object-bto #'object-cc #'object-bcc
|
||||
#'object-media-type #'object-duration
|
||||
;; Slots
|
||||
:@context :@type :type :@id :id :unsupported))
|
||||
:attachment :attributed-to :audience :content :context :name :end-time
|
||||
:generator :icon :image :in-reply-to :location :preview :published :replies
|
||||
:start-time :summary :tag :updated :url :to :bto :cc :bcc :media-type :duration))
|
||||
|
||||
(in-package #:activity-servist/activity-streams)
|
||||
|
||||
|
||||
|
||||
;;; Globals
|
||||
;;; ————————————————————————————————————————
|
||||
(defparameter *ap-packages* (list :activity-servist/activity-vocabulary *package*)
|
||||
"A list of packages in which we should search for AP classes and slot-symbols
|
||||
during JSON parsing. The class-name searched for is simply the value of the JSON
|
||||
object’s “type” key. The package first in the list to export such a symbol
|
||||
is the winner.")
|
||||
|
||||
(defparameter *default-class* 'activity-servist/activity-streams:object
|
||||
"The class used for ActivityStreams objects found during parsing that don’t
|
||||
have a corresponding class defined. Notably, all keys and values without
|
||||
corresponding slots are placed in the UNSUPPORTED slot.
|
||||
The class you choose should inherit ACTIVITY-SERVIST/ACTIVITY-STREAMS:OBJECT.")
|
||||
|
||||
;; Private, internal variable.
|
||||
(defparameter *@context* nil
|
||||
"Used in YASON:ENCODE to ensure that a single top-level @context can be
|
||||
created where AP objects contain other AP objects in their slots.
|
||||
This variable is overridden locally during encoding (LET), and should never be
|
||||
modified globally (as we expect it to be nil in top-level objects.")
|
||||
|
||||
|
||||
|
||||
;;; Macros
|
||||
;;; ————————————————————————————————————————
|
||||
;; This macro and the following function are related to JSON serialization; see
|
||||
;; the below “JSON serialization” section for other related functions.
|
||||
(defmacro define-yason-encode-slots (class)
|
||||
"Define a YASON:ENCODE-SLOTS method for a CLASS, which simply encodes all of
|
||||
CLASS’es slots with JSON keys based on the camel-cased slot name."
|
||||
(append
|
||||
`(defmethod yason:encode-slots progn ((obj ,class)))
|
||||
(mapcar (yason-encode-slot-function)
|
||||
(class-slots-activity-alist class))))
|
||||
|
||||
(defun yason-encode-slot-function ()
|
||||
"Helper-function for the DEFINE-YASON-ENCODE-SLOTS macro.
|
||||
This returns a function to create a quoted function that should be called for each slot,
|
||||
again and again, by YASON:ENCODE-SLOTS."
|
||||
(lambda (slot-key-pair)
|
||||
`(let ((key ',(car slot-key-pair))
|
||||
(value (ignore-errors (slot-value obj ',(car slot-key-pair)))))
|
||||
(cond ((eq key '@context) ; Actually in YASON:ENCODE-OBJECT using *@context*
|
||||
(setq *@context* (merge-@contexts *@context* (object-@context obj))))
|
||||
((eq key 'unsupported)
|
||||
;; Keys/values without a slot are stored in this UNSUPPORTED alist.
|
||||
(mapcar (lambda (cell)
|
||||
(yason:encode-object-element
|
||||
(camel-case (symbol-name (car cell)))
|
||||
(cdr cell)))
|
||||
value))
|
||||
((eq key 'type) ; Encode type based on class-name or TYPE slot
|
||||
(yason:encode-object-element
|
||||
"type" (or value
|
||||
(class-pretty-name (class-of obj)))))
|
||||
(value
|
||||
(yason:encode-object-element ,(cdr slot-key-pair) value))))))
|
||||
|
||||
|
||||
|
||||
;;; Core class
|
||||
;;; ————————————————————————————————————————
|
||||
(defclass object ()
|
||||
((@context :initform nil)
|
||||
(as/as:@type :initform nil)
|
||||
(as/as:type :initform nil)
|
||||
(as/as:@id :initform nil)
|
||||
(as/as:id :initform nil)
|
||||
(as/as:unsupported :initform nil :accessor object-unsupported)))
|
||||
|
||||
|
||||
|
||||
;;; Accessors
|
||||
;;; ————————————————————————————————————————
|
||||
(defgeneric object-@context (obj)
|
||||
(:documentation "Accessor for an object’s associated JSON-LD @context.
|
||||
As @context can sometimes vary on an object’s contents, on-the-fly, this
|
||||
method is invoked during JSON encoding of an object. The @CONTEXT
|
||||
slot-value should be prioritized over the @CONTEXT value is calculated
|
||||
by this method."))
|
||||
|
||||
(defmethod object-@context ((obj object))
|
||||
(or (slot-value obj '@context)
|
||||
"https://www.w3.org/ns/activitystreams"))
|
||||
|
||||
(defmethod (setf object-@context) (context (obj object))
|
||||
(setf (slot-value obj '@context) context))
|
||||
|
||||
|
||||
|
||||
;;; JSON parsing
|
||||
;;; ————————————————————————————————————————
|
||||
(defun parse (string)
|
||||
"Parse a string containing JSON into an ActivityPub object."
|
||||
(parse-table (yason:parse string)))
|
||||
|
||||
(defun parse-table (table)
|
||||
"Parse a hash-table corresponding to YASON-parsed JSON into an ActivityPub object."
|
||||
(let* ((found-class (car (find-registered-classes (param-case (gethash "type" table)))))
|
||||
(class (or found-class (find-class *default-class*)))
|
||||
(obj (make-instance class)))
|
||||
(loop for key being each hash-key of table
|
||||
for val being each hash-value of table
|
||||
do (let* ((slot-name (string-upcase (param-case key)))
|
||||
(slot-sym (car (find-registered-symbols slot-name)))
|
||||
(val (parse-value val)))
|
||||
(handler-case
|
||||
(setf (slot-value obj slot-sym) val)
|
||||
(error nil
|
||||
(setf (slot-value obj 'unsupported)
|
||||
(append (ignore-errors (slot-value obj 'unsupported))
|
||||
(list (cons (intern slot-name) val))))))))
|
||||
obj))
|
||||
|
||||
(defun parse-value (val)
|
||||
"Parse the value of a key found in YASON-parsed JSON.
|
||||
All ActivityPub objects (hash-tables containing “type”) will be parsed into
|
||||
ActivityPub objects; all others will parsed into associative lists."
|
||||
(typecase val
|
||||
(hash-table (maybe-parse-table val))
|
||||
(list (mapcar (lambda (a)
|
||||
(if (hash-table-p a)
|
||||
(maybe-parse-table a)
|
||||
a))
|
||||
val))
|
||||
(t val)))
|
||||
|
||||
(defun maybe-parse-table (table)
|
||||
"If a hash-table seems to be a valid ActivityPub object, attempt parsing it
|
||||
into one. Otherwise, parse it into an associative list."
|
||||
(if (gethash "type" table)
|
||||
(parse-table table)
|
||||
(alexandria:hash-table-alist table)))
|
||||
|
||||
|
||||
|
||||
;;; JSON serialization
|
||||
;;; ————————————————————————————————————————
|
||||
(defun define-class-encoders (classes)
|
||||
"For each class in CLASSES, define an YASON:ENCODE-SLOTS method for that class,
|
||||
which only encodes slots unique to that class (as ENCODE-SLOTS is called for each
|
||||
inherited class). Each slot’s name is converted to camel-case, as per convention."
|
||||
(mapcar (lambda (class)
|
||||
(closer-mop:finalize-inheritance class)
|
||||
(eval `(define-yason-encode-slots ,class)))
|
||||
classes))
|
||||
|
||||
(defun encode (obj &optional (stream *standard-output*))
|
||||
"Encode OBJ into JSON. A mere wrapper around YASON:ENCODE."
|
||||
(yason:encode obj stream))
|
||||
|
||||
(defmethod yason:encode ((obj object) &optional (stream *standard-output*))
|
||||
(yason:with-output (stream)
|
||||
(yason:encode-object obj)))
|
||||
|
||||
(defmethod yason:encode-object ((obj object))
|
||||
(typecase *@context*
|
||||
(null ; If this is the top-level (non-nested) object, establish a @context.
|
||||
(let ((*@context* 'top-level))
|
||||
(yason:encode-object obj)))
|
||||
(symbol ; In the top-level, encode slots and then @context.
|
||||
(setq *@context* (object-@context obj))
|
||||
(yason:with-object ()
|
||||
(yason:encode-slots obj)
|
||||
(yason:encode-object-element "@context" *@context*)))
|
||||
(T ; In nested objects, only encode slots — not *@context*.
|
||||
(yason:with-object ()
|
||||
(yason:encode-slots obj)))))
|
||||
|
||||
(defun class-slots-activity-alist (class)
|
||||
"Return an associative list containing CLASSes slots’ symbols consed with
|
||||
their sanitized string keys appropriate for ActivityVocabular custom.
|
||||
A class with slots MAP-AWAY and COLLECTION-AGAIN would return
|
||||
((MAP-AWAY . “mapAway”)(COLLECTION-AGAIN . “collectionAgain”))"
|
||||
(alist-mapcdr #'camel-case
|
||||
(class-slots-alist class)))
|
||||
|
||||
(defun merge-@contexts (a b)
|
||||
"Given two @context lists, A and B, merge them into one JSON-LD @context list
|
||||
containing both of their elements."
|
||||
(cond ((equal a b) a)
|
||||
((not b) a)
|
||||
((not a) b)
|
||||
((and (listp a)
|
||||
(find b a :test #'equal))
|
||||
a)
|
||||
(T
|
||||
(merge-lists
|
||||
(if (listp a) a (list a))
|
||||
(if (listp b) b (list b))))))
|
||||
|
||||
|
||||
|
||||
;;; Util
|
||||
;;; ————————————————————————————————————————
|
||||
(defun camel-case (string)
|
||||
"Convert a STRING to camel-casing. That is, casingLikeThis.
|
||||
Wrapper around STR:CAMEL-CASE, working around a bug that a non-alphanumeric
|
||||
character at the start of the string gets erroneously (or at least undesireably,
|
||||
to us) removed."
|
||||
(keep-nonalphanumeric-prefix string
|
||||
(str:camel-case string)))
|
||||
|
||||
(defun param-case (string)
|
||||
"Convert a STRING to param-casing. That is, casing-like-this.
|
||||
Wrapper around STR:PARAM-CASE, working around a bug that a non-alphanumeric
|
||||
character at the start of the string gets erroneously (or at least undesireably,
|
||||
to us) removed."
|
||||
(keep-nonalphanumeric-prefix string
|
||||
(str:param-case string)))
|
||||
(defun camel-case (str)
|
||||
"Convert a STRING to camel-casing.
|
||||
Wrapper around STR:CAMEL-CASE, working around a bug that a non-alphanumeric
|
||||
character at the start of the string gets erroneously (or at least undesireably,
|
||||
to us) removed."
|
||||
(keep-nonalphanumeric-prefix str (str:camel-case str)))
|
||||
|
||||
(defun keep-nonalphanumeric-prefix (str child-str)
|
||||
"This ensures that a CHILD-STR derived from STR has the same nonalphanumeric
|
||||
prefix as STR, as some functions like to remove such prefixes."
|
||||
(if (not (alphanumericp (aref str 0)))
|
||||
(concatenate 'string
|
||||
(string (aref str 0))
|
||||
child-str)
|
||||
child-str))
|
||||
|
||||
(defun class-pretty-name (class)
|
||||
"Return a CLASS’es name in a “pretty” (sentence-capitalized) string."
|
||||
(string-capitalize (symbol-name (class-name class))))
|
||||
|
||||
(defun merge-lists (a b)
|
||||
"Given lists A and B, merge them into one list non-redundantly — all unique
|
||||
items in each will be contained in the resultant list."
|
||||
(append a (remove-if (lambda (item) (find item a :test #'equal)) b)))
|
||||
|
||||
(defun find-registered-symbols (str)
|
||||
"Find all symbols identified by string STR within packages in the
|
||||
*ap-packages* list."
|
||||
(remove-if
|
||||
#'not
|
||||
(mapcar (lambda (package)
|
||||
(multiple-value-bind (sym context)
|
||||
(find-symbol (string-upcase str) package)
|
||||
(unless (eq context :inherited)
|
||||
sym)))
|
||||
*ap-packages*)))
|
||||
|
||||
(defun find-registered-classes (str)
|
||||
"Find all classes identified by string STR within pacakges in the
|
||||
*ap-packages* list."
|
||||
(remove-if
|
||||
#'not
|
||||
(mapcar (lambda (sym) (find-class sym))
|
||||
(find-registered-symbols str))))
|
||||
|
||||
(defun alist-mapcdr (function alist)
|
||||
"Apply a FUNCTION to all values (cdrs) of an ALIST’s pairs. Returns a new ALIST
|
||||
of the same keys, whose values are the results of FUNCTION."
|
||||
(mapcar
|
||||
(lambda (cell)
|
||||
(cons (car cell)
|
||||
(funcall function (cdr cell))))
|
||||
alist))
|
||||
|
||||
(defun class-slots-alist (class)
|
||||
"Return an associative list of a CLASS’es direct slots (by symbol) matched with
|
||||
their names as strings. For instance, a class with slots MAP-AWAY and
|
||||
COLLECTION-AGAIN would return:
|
||||
((MAP-AWAY . “MAP-AWAY”)(COLLECTION-AGAIN . “COLLECTION-AGAIN”)"
|
||||
(mapcar
|
||||
(lambda (slot)
|
||||
(let ((name (closer-mop:slot-definition-name slot)))
|
||||
(cons name (symbol-name name))))
|
||||
(closer-mop:class-direct-slots class)))
|
||||
|
||||
|
||||
|
||||
;;; Defining YASON:ENCODE-SLOTS
|
||||
;;; ————————————————————————————————————————
|
||||
(as/as:define-class-encoders (list (find-class 'object)))
|
||||
(json-ld::define-json-type (object "Object") () "https://www.w3.org/ns/activitystreams"
|
||||
((actor
|
||||
"actor"
|
||||
:documentation "Describes one or more entities that either performed or are expected to perform the activity. Any single activity can have multiple actors. The actor MAY be specified using an indirect Link.")
|
||||
(attachment
|
||||
"attachment"
|
||||
:documentation "Identifies a resource attached or related to an object that potentially requires special handling. The intent is to provide a model that is at least semantically similar to attachments in email.")
|
||||
(attributed-to
|
||||
"attributedTo"
|
||||
:documentation "Identifies one or more entities to which this object is attributed. The attributed entities might not be Actors. For instance, an object might be attributed to the completion of another activity.")
|
||||
(audience
|
||||
"audience"
|
||||
:documentation "Identifies one or more entities that represent the total population of entities for which the object can considered to be relevant.")
|
||||
(content
|
||||
"content"
|
||||
:documentation "The content or textual representation of the Object encoded as a JSON string. By default, the value of content is HTML. The media-type property can be used in the object to indicate a different content type.
|
||||
The content MAY be expressed using multiple language-tagged values. ")
|
||||
(context
|
||||
"context"
|
||||
:documentation "Identifies the context within which the object exists or an activity was performed.
|
||||
The notion of “context” used is intentionally vague. The intended function is to serve as a means of grouping objects and activities that share a common originating context or purpose. An example could be all activities relating to a common project or event.")
|
||||
(name
|
||||
"name"
|
||||
:documentation "A simple, human-readable, plain-text name for the object. HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.")
|
||||
(end-time
|
||||
"endTime"
|
||||
:documentation "The date and time describing the actual or expected ending time of the object. When used with an Activity object, for instance, the end-time property specifies the moment the activity concluded or is expected to conclude.")
|
||||
(generator
|
||||
"generator"
|
||||
:documentation "Identifies the entity (e.g. an application) that generated the object.")
|
||||
(icon
|
||||
"icon"
|
||||
:documentation "Indicates an entity that describes an icon for this object. The image should have an aspect ratio of one (horizontal) to one (vertical) and should be suitable for presentation at a small size.")
|
||||
(image
|
||||
"image"
|
||||
:documentation "Indicates an entity that describes an image for this object. Unlike the icon property, there are no aspect ratio or display size limitations assumed.")
|
||||
(in-reply-to
|
||||
"inReplyTo"
|
||||
:documentation "Indicates one or more entities for which this object is considered a response.")
|
||||
(location
|
||||
"location"
|
||||
:documentation "Indicates one or more physical or logical locations associated with the object.")
|
||||
(preview
|
||||
"preview"
|
||||
:documentation "Identifies an entity that provides a preview of this object.")
|
||||
(published
|
||||
"published"
|
||||
:documentation "The date and time describing the actual or expected starting time of the object. When used with an Activity object, for instance, the start-time property specifies the moment the activity began or is scheduled to begin.")
|
||||
(replies
|
||||
"replies"
|
||||
:documentation "Identifies a Collection containing objects considered to be responses to this object.")
|
||||
(start-time
|
||||
"startTime"
|
||||
:documentation "The date and time describing the actual or expected starting time of the object. When used with an Activity object, for instance, the start-time property specifies the moment the activity began or is scheduled to begin.")
|
||||
(summary
|
||||
"summary"
|
||||
:documentation "A natural language summarization of the object encoded as HTML. Multiple language tagged summaries MAY be provided.")
|
||||
(tag
|
||||
"tag"
|
||||
:documentation "One or more “tags” that have been associated with an objects. A tag can be any kind of Object. The key difference between attachment and tag is that the former implies association by inclusion, while the latter implies associated by reference.")
|
||||
(updated
|
||||
"updated"
|
||||
:documentation "The date and time at which the object was updated.")
|
||||
(url
|
||||
"url"
|
||||
:documentation "Identifies one or more links to representations of the object.")
|
||||
(to
|
||||
"to"
|
||||
:documentation "Identifies an entity considered to be part of the public primary audience of an Object.")
|
||||
(bto
|
||||
"bto"
|
||||
:documentation "Identifies an Object that is part of the private primary audience of this Object.")
|
||||
(cc
|
||||
"cc"
|
||||
:documentation "Identifies an Object that is part of the public secondary audience of this Object.")
|
||||
(bcc
|
||||
"bcc"
|
||||
:documentation "Identifies one or more Objects that are part of the private secondary audience of this Object.")
|
||||
(media-type
|
||||
"mediaType"
|
||||
:documentation "When used on a Link, identifies the MIME media type of the referenced resource.
|
||||
When used on an Object, identifies the MIME media type of the value of the content property. If not specified, the content property is assumed to contain text/html content.")
|
||||
(duration
|
||||
"duration"
|
||||
:documentation "When the object describes a time-bound resource, such as an audio or video, a meeting, etc, the duration property indicates the object's approximate duration. The value MUST be expressed as an xsd:duration as defined by [ xmlschema11-2], section 3.3.6 (e.g. a period of 5 seconds is represented as “PT5S”)."))
|
||||
(:documentation
|
||||
"Describes an object of any kind. The Object type serves as the base type for
|
||||
most of the other kinds of objects defined in the Activity Vocabulary,
|
||||
including other Core types such as Activity, IntransitiveActivity, Collection
|
||||
and OrderedCollection."))
|
||||
|
|
125
src/json-ld.lisp
125
src/json-ld.lisp
|
@ -19,7 +19,11 @@
|
|||
(:use #:cl)
|
||||
(:nicknames "AS/JSON-LD" "JSON-LD")
|
||||
(:export
|
||||
#:define-json-type #:json-ld-context))
|
||||
#:define-json-type
|
||||
;; Accessors
|
||||
#:json-ld-context #:json-ld-etc #:json-ld-id #:json-ld-type
|
||||
;; Slots
|
||||
:@context :@id :@type :.etc))
|
||||
|
||||
(in-package #:activity-servist/json-ld)
|
||||
|
||||
|
@ -33,7 +37,7 @@
|
|||
|
||||
;;; Base class
|
||||
;;; ————————————————————————————————————————
|
||||
(defclass json-ld-type ()
|
||||
(defclass json-ld-object ()
|
||||
((@context
|
||||
:initform nil
|
||||
:documentation
|
||||
|
@ -41,28 +45,57 @@
|
|||
The method JSON-LD-CONTEXT is how the contents of encoded @context is
|
||||
determined; to change a class’es default/calculated @context, override that
|
||||
method. This slot is for changing a specific object’s @context.")
|
||||
(etc
|
||||
(@id
|
||||
:initform nil
|
||||
:accessor json-ld-id
|
||||
:documentation
|
||||
"Provides the globally unique identifier for an object.")
|
||||
(@type
|
||||
:initform nil
|
||||
:accessor json-ld-type
|
||||
:documentation
|
||||
"Identifies the type of an object. Used to determine the corresponding CLOS-object.")
|
||||
(.etc
|
||||
:initform nil
|
||||
:accessor json-ld-etc
|
||||
:documentation
|
||||
"Components of the JSON object which, during parsing, did not match any specific
|
||||
slot. This is often filled up in the case of undefined node-types or non-adherent
|
||||
object definitions.")))
|
||||
|
||||
(setf (gethash "*" *json-types*)
|
||||
'((json-ld-object)
|
||||
("@context" @context . "@context")
|
||||
("@id" @id . "@id")
|
||||
("@type" @type . "@type")))
|
||||
|
||||
(defgeneric json-ld-context (obj)
|
||||
(:documentation
|
||||
"Returns a JSON-LD CLOS object’s @context, for use in JSON-encoding of the
|
||||
object.
|
||||
The implementation for the JSON-LD-TYPE class simply returns the activitystreams
|
||||
The implementation for the JSON-LD-OBJECT class simply returns the activitystreams
|
||||
URL.
|
||||
If you would like to change @context on a class-level, override this method.
|
||||
If you would like to change it on an object-level, set the @CONTEXT slot."))
|
||||
|
||||
(defmethod json-ld-context ((obj json-ld-type))
|
||||
(defmethod json-ld-context ((obj json-ld-object))
|
||||
(or (slot-value obj '@context)
|
||||
"https://www.w3.org/ns/activitystreams"))
|
||||
|
||||
(defmethod yason:encode-slots progn ((obj json-ld-type))
|
||||
(defmethod yason:encode-slots progn ((obj json-ld-object))
|
||||
(let ((context (json-ld-context obj))
|
||||
(id (json-ld-id obj))
|
||||
(type (json-ld-type obj)))
|
||||
(when context
|
||||
(yason:encode-object-element "@context" (json-ld-context obj)))
|
||||
(when id
|
||||
(yason:encode-object-element "@id" (json-ld-id obj)))
|
||||
(when type
|
||||
(yason:encode-object-element "@type" (json-ld-type obj))))
|
||||
(mapcar (lambda (alist-cell)
|
||||
(yason:encode-object-element (car alist-cell)
|
||||
(cdr alist-cell)))
|
||||
(json-ld-etc obj)))
|
||||
|
||||
|
||||
|
||||
|
@ -78,8 +111,9 @@ encoding as @type’s value. If only the CLOS class-name is provided, @type will
|
|||
not be encoded for this object.
|
||||
|
||||
DIRECT-SUPERCLASSES is a list of JSON-LD CLOS classes, whose slots and context
|
||||
this should inherit. JSON-LD-TYPE should be somewhere in the hierarchy, in order
|
||||
to provide “@context”; if no superclasses are provided, JSON-LD-TYPE is default.
|
||||
this should inherit. JSON-LD-OBJECT should be somewhere in the hierarchy, in order
|
||||
to provide “@context”, “@id”, and “@type”; if no superclasses are provided,
|
||||
JSON-LD-OBJECT is default.
|
||||
|
||||
CONTEXT is a context hash-table, parsed from a JSON-LD context by JSON-LD:PARSE
|
||||
or JSON-LD:PARSE-CONTEXT. Any terms defined in this context will be used to
|
||||
|
@ -94,13 +128,7 @@ encoded nor decoded in JSON.
|
|||
|
||||
SLOT-OPTIONS are key/value pairs in the format of DEFCLASS’es slot-options.
|
||||
Keywords such as :INITFORM, :DOCUMENTATION, etc. can be used.
|
||||
There are two keywords with behavior unlike DEFCLASS, however:
|
||||
:REQUIRED and :ACCESSOR.
|
||||
|
||||
By default, a slot will have an init-form of NIL; this can of course be
|
||||
overridden by putting :INITFORM yourself in the slot definition.
|
||||
Set :REQUIRED to T to not set :INITFORM at all, effectively making the slot
|
||||
“required.”
|
||||
There is one keyword with behavior unlike DEFCLASS, however — :ACCESSOR.
|
||||
|
||||
By default, a slot will have an accessor named after the class and slot, like
|
||||
PLACE-RADIUS for the class PLACE and the slot RADIUS.
|
||||
|
@ -115,17 +143,15 @@ Here is a brief example partially defining the “Place” type from ActivityStr
|
|||
((altitude “altitude”
|
||||
:documentation “Indicates the altitude of a place.”)
|
||||
(latitude “latitude”
|
||||
:required T
|
||||
:documentation “The latitude of a place.”)
|
||||
(longitude “longitude”
|
||||
:required T
|
||||
:documentation “The longitude of a place.”)))"
|
||||
`(let ((json-class
|
||||
(define-json-clos-class ,names
|
||||
,(or direct-superclasses `(json-ld-type))
|
||||
,(or direct-superclasses `(json-ld-object))
|
||||
,direct-slots ,options)))
|
||||
(define-json-type-encoder ,(car names) ,direct-slots)
|
||||
(register-json-type ',names ',direct-slots ,context)
|
||||
(register-json-type ',names (or ',direct-superclasses '(json-ld-object)) ',direct-slots ,context)
|
||||
json-class))
|
||||
|
||||
(defmacro define-json-clos-class (names direct-superclasses direct-slots options)
|
||||
|
@ -153,21 +179,15 @@ applying default slot-options, etc."
|
|||
|
||||
(defun json-type-implicit-slot-options (class-name slot-name)
|
||||
"Return default property-list slot options for a json-type CLOS class."
|
||||
(list :initform nil
|
||||
:accessor (intern (format nil "~A-~A" class-name slot-name))))
|
||||
(list :accessor (intern (format nil "~A-~A" class-name slot-name))))
|
||||
|
||||
(defun json-type-normalize-slot-options (slot-opts)
|
||||
"Take property-list slot options from a DEFINE-JSON-TYPE format and massage it
|
||||
into a DEFCLASS format."
|
||||
(let* ((required (getf slot-opts :required))
|
||||
(sans-required (alexandria:remove-from-plist slot-opts :required))
|
||||
(sans-initform-maybe (if required
|
||||
(alexandria:remove-from-plist sans-required :initform)
|
||||
sans-required))
|
||||
(sans-accessor-maybe (if (and (find :accessor slot-opts)
|
||||
(let* ((sans-accessor-maybe (if (and (find :accessor slot-opts)
|
||||
(not (getf slot-opts :accessor)))
|
||||
(alexandria:remove-from-plist sans-initform-maybe :accessor)
|
||||
sans-initform-maybe)))
|
||||
(alexandria:remove-from-plist slot-opts :accessor)
|
||||
slot-opts)))
|
||||
sans-accessor-maybe))
|
||||
|
||||
|
||||
|
@ -181,28 +201,32 @@ CLASS is the class-name; see DEFINE-JSON-TYPE’s docstring about DIRECT-SLOTS."
|
|||
(append
|
||||
`(defmethod yason:encode-slots progn ((obj ,class)))
|
||||
(mapcar (lambda (slot)
|
||||
`(yason:encode-object-element ,(cadr slot) (slot-value obj ',(car slot))))
|
||||
`(when (slot-boundp obj ',(car slot))
|
||||
(yason:encode-object-element ,(cadr slot) (slot-value obj ',(car slot)))))
|
||||
direct-slots)))
|
||||
|
||||
|
||||
|
||||
;;; Parsing
|
||||
;;; ————————————————————————————————————————
|
||||
(defun register-json-type (names direct-slots context)
|
||||
(defun register-json-type (names direct-superclasses direct-slots context)
|
||||
"Register a JSON node-type. This allows PARSE to recognize the type (and
|
||||
corresponding CLOS class) of a node."
|
||||
(let* ((ctx (parse-context context))
|
||||
(type-iri (getf (gethash (cadr names) ctx) :id))
|
||||
(type-name (or type-iri (cadr names))))
|
||||
(setf (gethash type-name *json-types*)
|
||||
(json-type-registry-list names ctx direct-slots))))
|
||||
(json-type-registry-list names direct-superclasses ctx direct-slots))))
|
||||
|
||||
(defun json-type-registry-list (names parsed-context direct-slots)
|
||||
(defun json-type-registry-list (names direct-superclasses parsed-context direct-slots)
|
||||
"Return a REGISTER-JSON-TYPE-formed registry entry, a simple list of the form:
|
||||
(TYPE-IRI (PROPERTY-NAME SLOT-NAME) ⋯ (PROPERTY-NAME SLOT-NAME))
|
||||
… where TYPE-IRI is the (hopefully) fully-resolved IRI form of the node-type’s
|
||||
name, though it might be unresolved if context was unprovided or lacking."
|
||||
(append (list (cons (car names) (cadr names)))
|
||||
(append
|
||||
;; The class-name and type-name.
|
||||
(list (cons (car names) (cadr names)))
|
||||
;; Add the class’es direct slots.
|
||||
(mapcar
|
||||
(lambda (slot)
|
||||
(when (cadr slot)
|
||||
|
@ -212,7 +236,16 @@ name, though it might be unresolved if context was unprovided or lacking."
|
|||
(url (or (getf ctx-item :id)
|
||||
property-name)))
|
||||
(cons url (cons slot-name property-name)))))
|
||||
direct-slots)))
|
||||
direct-slots)
|
||||
;; Add the slots of parent-classes.
|
||||
(reduce (lambda (slots-a slots-b)
|
||||
(append slots-a slots-b))
|
||||
(mapcar (lambda (class-name)
|
||||
(let* ((type-name (class-json-type-name class-name))
|
||||
(type-registry (gethash type-name *json-types*)))
|
||||
(if type-registry
|
||||
type-registry)))
|
||||
direct-superclasses))))
|
||||
|
||||
(defun parse (str)
|
||||
"Parse the JSON-LD document contained in STR."
|
||||
|
@ -240,11 +273,10 @@ name, though it might be unresolved if context was unprovided or lacking."
|
|||
(getf val :id))))
|
||||
;; Now, actually parse.
|
||||
(let* ((parsed-table (parse-table-inplace table ctx))
|
||||
(type (identify-json-type table ctx rev-ctx))
|
||||
(typedef (gethash type *json-types*)))
|
||||
(if typedef
|
||||
(parse-table-into-object parsed-table typedef ctx rev-ctx) ; We prefer this!
|
||||
parsed-table)))) ; … but just in case you wanna use an undefined type…
|
||||
(type (identify-json-type table ctx))
|
||||
(type-def (or (gethash type *json-types*)
|
||||
(gethash "*" *json-types*))))
|
||||
(parse-table-into-object parsed-table type-def ctx rev-ctx))))
|
||||
|
||||
(defun parse-table-inplace (table ctx)
|
||||
"Expand a YASON-parsed JSON-LD node-object in TABLE. That is, replace all
|
||||
|
@ -279,17 +311,17 @@ CTX is the according parsed-context, and REV-CTX is the reversed
|
|||
(lambda (property value)
|
||||
(let* ((property-def (assoc property type-def :test #'equal))
|
||||
(slot-name (second property-def))
|
||||
(etc-value (slot-value obj 'etc)))
|
||||
(etc-value (slot-value obj '.etc)))
|
||||
(if property-def
|
||||
(setf (slot-value obj slot-name) value)
|
||||
(setf (slot-value obj 'etc)
|
||||
(setf (slot-value obj '.etc)
|
||||
(append etc-value
|
||||
(list (cons property value)))))))
|
||||
table)
|
||||
(setf (slot-value obj '@context) (gethash "@context" table))
|
||||
obj))
|
||||
|
||||
(defun identify-json-type (table ctx rev-ctx)
|
||||
(defun identify-json-type (table ctx)
|
||||
"Given an parsed JSON-LD object’s hash-TABLE, return the name/IRI of the
|
||||
JSON-type that best suits the object — using the types registered into
|
||||
*JSON-TYPES* with REGISTER-JSON-TYPE."
|
||||
|
@ -297,6 +329,13 @@ JSON-type that best suits the object — using the types registered into
|
|||
(or (getf (gethash type ctx) :id)
|
||||
type)))
|
||||
|
||||
(defun class-json-type-name (class-name)
|
||||
"Return the name (IRI) of a registered JSON-type its CLOS class’es name."
|
||||
(loop for iri being the hash-keys in *json-types*
|
||||
for registry being the hash-values in *json-types*
|
||||
if (eq class-name (caar registry))
|
||||
return iri))
|
||||
|
||||
|
||||
|
||||
;;; Context-parsing
|
||||
|
|
Ŝarĝante…
Reference in New Issue