forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize.ts
More file actions
112 lines (107 loc) · 3.78 KB
/
serialize.ts
File metadata and controls
112 lines (107 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Formula from './formula'
import Serializer from './serializer'
import {
ContentType,
JSONLDContentType,
N3ContentType,
N3LegacyContentType,
NQuadsAltContentType,
NQuadsContentType,
NTriplesContentType,
RDFXMLContentType,
TurtleContentType,
TurtleLegacyContentType,
} from './types'
import IndexedFormula from './store'
import { BlankNode, NamedNode } from './tf-types'
/**
* Serialize to the appropriate format
*/
export default function serialize (
/** The graph or nodes that should be serialized */
target: Formula | NamedNode | BlankNode | null,
/** The store */
kb: Formula,
base?: unknown,
/**
* The mime type.
* Defaults to Turtle.
*/
contentType?: string | ContentType,
callback?: (err: Error | undefined | null, result?: string) => any,
options?: {
/**
* A string of letters, each of which set an options
* e.g. `deinprstux`
*/
flags?: string,
/**
* A set of [prefix, uri] pairs that define namespace prefixes
*/
namespaces?: Record<string, string>
}
): string | undefined {
base = base || target?.value
const opts = options || {}
contentType = contentType || TurtleContentType // text/n3 if complex?
var documentString: string | undefined = undefined
try {
var sz = Serializer(kb)
if ((opts as any).flags) sz.setFlags((opts as any).flags)
var newSts = kb!.statementsMatching(undefined, undefined, undefined, target as NamedNode)
// If an IndexedFormula, use the namespaces from the given graph as suggestions
if ('namespaces' in kb) {
sz.suggestNamespaces( (kb as IndexedFormula).namespaces);
}
// use the provided options.namespaces are mandatory prefixes
if (opts.namespaces) {
sz.setNamespaces( opts.namespaces);
}
sz.setBase(base)
switch (contentType) {
case RDFXMLContentType:
documentString = sz.statementsToXML(newSts)
return executeCallback(null, documentString)
case N3ContentType:
case N3LegacyContentType:
documentString = sz.statementsToN3(newSts)
return executeCallback(null, documentString)
case TurtleContentType:
case TurtleLegacyContentType:
// Suppress = for sameAs and => for implies; preserve any user-specified flags (e.g., 'o')
sz.setFlags('si' + (opts.flags ? (' ' + opts.flags) : ''))
documentString = sz.statementsToN3(newSts)
return executeCallback(null, documentString)
case NTriplesContentType:
sz.setFlags('deinprstux') // Suppress nice parts of N3 to make ntriples
documentString = sz.statementsToNTriples(newSts)
return executeCallback(null, documentString)
case JSONLDContentType:
// turtle + dr (means no default, no relative prefix); preserve user flags
sz.setFlags('si dr' + (opts.flags ? (' ' + opts.flags) : ''))
documentString = sz.statementsToJsonld(newSts) // convert via turtle
return executeCallback(null, documentString)
case NQuadsContentType:
case NQuadsAltContentType: // @@@ just outpout the quads? Does not work for collections
sz.setFlags('deinprstux q') // Suppress nice parts of N3 to make ntriples
documentString = sz.statementsToNTriples(newSts) // q in flag means actually quads
return executeCallback(null, documentString)
default:
throw new Error('Serialize: Content-type ' + contentType + ' not supported for data write.')
}
} catch (err) {
if (callback) {
// @ts-ignore
return callback(err, undefined)
}
throw err // Don't hide problems from caller in sync mode
}
function executeCallback (err: Error | null | undefined, result: string | undefined): string | undefined {
if (callback) {
callback(err, result)
return
} else {
return result as string
}
}
}