Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: erlef/setup-beam@v1
with:
otp-version: "27"
gleam-version: "1.10.0"
gleam-version: "1.13.0"
rebar3-version: "3"
- uses: actions/setup-node@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "gleam_json"
version = "3.0.2"
gleam = ">= 0.32.0"
gleam = ">= 1.13.0"

licences = ["Apache-2.0"]
description = "Work with JSON in Gleam"
Expand Down
33 changes: 16 additions & 17 deletions src/gleam_json_ffi.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ok, Error } from "./gleam.mjs";
import { UnexpectedByte, UnexpectedEndOfInput } from "./gleam/json.mjs";
import { Result$Ok, Result$Error, List$isNonEmpty, List$NonEmpty$first, List$NonEmpty$rest } from "./gleam.mjs";
import { DecodeError$UnexpectedByte, DecodeError$UnexpectedEndOfInput } from "./gleam/json.mjs";

export function json_to_string(json) {
return JSON.stringify(json);
Expand All @@ -14,7 +14,12 @@ export function identity(x) {
}

export function array(list) {
return list.toArray();
const array = [];
while (List$isNonEmpty(list)) {
array.push(List$NonEmpty$first(list));
list = List$NonEmpty$rest(list);
}
return array;
}

export function do_null() {
Expand All @@ -24,14 +29,14 @@ export function do_null() {
export function decode(string) {
try {
const result = JSON.parse(string);
return new Ok(result);
return Result$Ok(result);
} catch (err) {
return new Error(getJsonDecodeError(err, string));
return Result$Error(getJsonDecodeError(err, string));
}
}

export function getJsonDecodeError(stdErr, json) {
if (isUnexpectedEndOfInput(stdErr)) return new UnexpectedEndOfInput();
if (isUnexpectedEndOfInput(stdErr)) return DecodeError$UnexpectedEndOfInput();
return toUnexpectedByteError(stdErr, json);
}

Expand Down Expand Up @@ -76,11 +81,6 @@ function isUnexpectedEndOfInput(err) {
*
* For Spidermonkey, the position is reported by the runtime as a line and column number
* and the unexpected byte is found using those coordinates.
*
* @param {'chromium' | 'spidermonkey' | 'jscore'} runtime
* @param {SyntaxError} err
* @param {string} json
* @returns {UnexpectedByte}
*/
function toUnexpectedByteError(err, json) {
let converters = [
Expand All @@ -95,7 +95,7 @@ function toUnexpectedByteError(err, json) {
if (result) return result;
}

return new UnexpectedByte("", 0);
return DecodeError$UnexpectedByte("");
}

/**
Expand All @@ -110,7 +110,7 @@ function v8UnexpectedByteError(err) {
const match = regex.exec(err.message);
if (!match) return null;
const byte = toHex(match[1]);
return new UnexpectedByte(byte, -1);
return DecodeError$UnexpectedByte(byte);
}

/**
Expand All @@ -126,8 +126,7 @@ function oldV8UnexpectedByteError(err) {
const match = regex.exec(err.message);
if (!match) return null;
const byte = toHex(match[1]);
const position = Number(match[2]);
return new UnexpectedByte(byte, position);
return DecodeError$UnexpectedByte(byte);
}

/**
Expand All @@ -145,7 +144,7 @@ function spidermonkeyUnexpectedByteError(err, json) {
const column = Number(match[3]);
const position = getPositionFromMultiline(line, column, json);
const byte = toHex(json[position]);
return new UnexpectedByte(byte, position);
return DecodeError$UnexpectedByte(byte);
}

/**
Expand All @@ -159,7 +158,7 @@ function jsCoreUnexpectedByteError(err) {
const match = regex.exec(err.message);
if (!match) return null;
const byte = toHex(match[2]);
return new UnexpectedByte(byte, 0);
return DecodeError$UnexpectedByte(byte);
}

function toHex(char) {
Expand Down