-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto-json.js
60 lines (46 loc) · 1.16 KB
/
to-json.js
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
let jsedn = require('jsedn')
let assert = require('assert');
exports.toJson = function toJson(ednObj) {
// console.log("Data to convert", ednObj)
if (ednObj instanceof jsedn.Keyword) {
return ednObj.val.slice(1)
}
if (typeof ednObj === 'number') {
return ednObj
}
if (typeof ednObj === 'string') {
return ednObj
}
if (ednObj instanceof jsedn.Map) {
let ret = {}
ednObj.keys.forEach((k, idx) => {
let key = toJson(k)
assert(typeof key === 'string', "Key for an object should be a string!")
ret[key] = toJson(ednObj.vals[idx])
})
return ret
}
if (ednObj instanceof jsedn.Set) {
return ednObj.val.map(toJson)
}
if (ednObj instanceof jsedn.Vector) {
return ednObj.val.map(toJson)
}
if (ednObj instanceof jsedn.List) {
return ednObj.val.map(toJson)
}
if (ednObj instanceof jsedn.Symbol) {
throw new Error('Symbol not supported')
}
if (ednObj == null) {
return null
}
if (ednObj === true) {
return true
}
if (ednObj === false) {
return false
}
console.error('Unkown data:', ednObj, 'in type:', typeof ednObj)
throw new Error('Not known how to convert')
}