Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/RdfList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,8 @@ export class RdfList<T> implements Array<T> {
private get items(): Iterable<ListItem<T>> {
return this.root.items()
}

toJSON() {
return [...this]
}
}
33 changes: 31 additions & 2 deletions src/TermWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,41 @@ import type { ITermAsValueMapping } from "./type/ITermAsValueMapping.js"
import { WrappingSet } from "./WrappingSet.js"
import { WrappingMap } from "./WrappingMap.js"
import { AnyTermWithContext } from "./AnyTermWithContext.js"
import { AnyTerm } from "./AnyTerm.js"

export class TermWrapper extends AnyTermWithContext {
get [Symbol.toStringTag]() {
return this.constructor.name
}

toJSON() {
const result = {}

for (let instance = this; instance !== null; instance = Object.getPrototypeOf(instance)) {
if (instance.constructor.name === AnyTerm.name) {
continue
}

for (const [propertyName, {value: propertyValue}] of Object.entries(Object.getOwnPropertyDescriptors(instance))) {
if (propertyName === "__proto__") {
continue
}

if (propertyName === "constructor") {
continue
}

if (propertyValue !== undefined) {
continue
}

Reflect.set(result, propertyName, Reflect.get(this, propertyName))
}
}

return result
}

protected singular<T>(p: string, termAs: ITermAsValueMapping<T>): T {
const predicate = this.factory.namedNode(p)
const matches = this.dataset.match(this as Term, predicate)[Symbol.iterator]()
Expand All @@ -18,11 +47,11 @@ export class TermWrapper extends AnyTermWithContext {
const {value: first, done: none} = matches.next()

if (none) {
throw new Error(`No value found for predicate ${p} on term ${this.value}`)
throw new Error(`More than one value for predicate ${p} on term ${this.value}`)
}

if (!matches.next().done) {
throw new Error(`More than one value for predicate ${p} on term ${this.value}`)
throw new Error(`No value found for predicate ${p} on term ${this.value}`)
}

return termAs(new TermWrapper(first.object, this.dataset, this.factory))
Expand Down
4 changes: 4 additions & 0 deletions src/WrappingMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export class WrappingMap<TKey, TValue> implements Map<TKey, TValue> {
return this.constructor.name
}

toJSON(){
return Object.fromEntries(this)
}

private get matches(): Iterable<Quad> {
const p = this.subject.factory.namedNode(this.predicate)

Expand Down
4 changes: 4 additions & 0 deletions src/WrappingSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export class WrappingSet<T> implements Set<T> {
return this.constructor.name
}

toJSON() {
return [...this]
}

private quad(value: T): Quad {
const s = this.subject as Quad_Subject // TODO: guard
const p = this.subject.factory.namedNode(this.predicate)
Expand Down
Empty file added src/mapping/ObjectMapping.ts
Empty file.
Empty file added src/mapping/TermMapping.ts
Empty file.
Empty file added src/mapping/ValueMapping.ts
Empty file.
Empty file added src/type/ITermMapping.ts
Empty file.
Loading
Loading