blob: 0d9021ce2676bc46a3797256864b6cc555463bcf (
plain)
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
|
import { Ok, Error } from './gleam.mjs'
import { bit_string_to_string } from '../../gleam_stdlib/dist/gleam_stdlib.mjs'
import { UnexpectedByte, UnexpectedEndOfInput } from './gleam/json.mjs'
export function json_to_string(json) {
return JSON.stringify(json)
}
export function object_from(entries) {
return Object.fromEntries(entries)
}
export function array(list) {
return list.toArray()
}
export function do_null() {
return null
}
export function string(x) {
return json_to_string(x)
}
export function int(x) {
return parseInt(json_to_string(x), 10)
}
export function float(x) {
return parseFloat(json_to_string(x), 10)
}
export function bool(x) {
return Boolean(x)
}
export function decode(bit_string) {
const stringResult = bit_string_to_string(bit_string)
if (!stringResult.isOk()) return stringResult
try {
const result = JSON.parse(stringResult[0])
return new Ok(result)
} catch (err) {
return new Error(getJsonDecodeError(err))
}
}
function getJsonDecodeError(stdErr) {
if (isUnexpectedByte(stdErr)) return new toUnexpectedByteError(stdErr)
if (isUnexpectedEndOfInput(stdErr)) return new UnexpectedEndOfInput()
return undefined
}
function isUnexpectedEndOfInput(err) {
return err.message === 'Unexpected end of JSON input'
}
const unexpectedByteRegex = /Unexpected token (.) in JSON at position (\d+)/
function isUnexpectedByte(err) {
return unexpectedByteRegex.test(err)
}
function toUnexpectedByteError(err) {
const match = unexpectedByteRegex.exec(err.message)
const byte = "0x" + match[1].charCodeAt(0).toString(16).toUpperCase()
const position = Number(match[2])
return new UnexpectedByte(byte, position)
}
|