add_task(async function read_json() {
const filename = PathUtils.join(PathUtils.tempDir, "test_ioutils_read_json.tmp");
info("Testing IOUtils.readJSON() with a serialized object...");
await IOUtils.writeUTF8(filename, JSON.stringify(OBJECT));
const readObject = await IOUtils.readJSON(filename);
const parsedObject = JSON.parse(await IOUtils.readUTF8(filename));
ok(ObjectUtils.deepEqual(OBJECT, readObject), "JSON objects should round-trip");
ok(
ObjectUtils.deepEqual(parsedObject, readObject), "IOUtils.readJSON() equivalent to JSON.parse() for objects"
);
info("Testing IOUtils.readJSON() with a serialized array...");
await IOUtils.writeUTF8(filename, JSON.stringify(ARRAY));
const readArray = await IOUtils.readJSON(filename);
const parsedArray = JSON.parse(await IOUtils.readUTF8(filename));
ok(ObjectUtils.deepEqual(ARRAY, readArray), "JSON arrays should round-trip");
ok(
ObjectUtils.deepEqual(parsedArray, readArray), "IOUtils.readJSON() equivalent to JSON.parse(IOUtils.readUTF8()) for arrays"
);
info("Testing IOUtils.readJSON() with serialized primitives...");
for (const primitive of PRIMITIVES) {
await IOUtils.writeUTF8(filename, JSON.stringify(primitive));
const readPrimitive = await IOUtils.readJSON(filename);
const parsedPrimitive = JSON.parse(await IOUtils.readUTF8(filename));
ok(primitive === readPrimitive, `JSON primitive ${primitive} should round trip`);
ok(
readPrimitive === parsedPrimitive,
`${readPrimitive} === ${parsedPrimitive} -- IOUtils.readJSON() equivalent to JSON.parse() for primitive`
);
}
info("Testing IOUtils.readJSON() with a file that does not exist...");
const notExistsFilename = PathUtils.join(PathUtils.tempDir, "test_ioutils_read_json_not_exists.tmp");
ok(!await IOUtils.exists(notExistsFilename), `${notExistsFilename} should not exist`);
await Assert.rejects(
IOUtils.readJSON(notExistsFilename),
/NotFoundError: Could not open `.*'/, "IOUtils::readJSON rejects when file does not exist"
);
info("Testing IOUtils.readJSON() with a file that does not contain JSON");
const invalidFilename = PathUtils.join(PathUtils.tempDir, "test_ioutils_read_json_invalid.tmp");
await IOUtils.writeUTF8(invalidFilename, ":)");
await Assert.rejects(
IOUtils.readJSON(invalidFilename),
/SyntaxError: JSON\.parse/, "IOUTils::readJSON rejects when the file contains invalid JSON"
);
await cleanup(filename, invalidFilename);
});
add_task(async function write_json() {
const filename = PathUtils.join(PathUtils.tempDir, "test_ioutils_write_json.tmp");
info("Testing IOUtils.writeJSON() with an object...");
await IOUtils.writeJSON(filename, OBJECT);
const readObject = await IOUtils.readJSON(filename);
const readObjectStr = await IOUtils.readUTF8(filename);
ok(ObjectUtils.deepEqual(OBJECT, readObject), "JSON objects should round-trip");
ok(
readObjectStr === JSON.stringify(OBJECT), "IOUtils.writeJSON() eqvuialent to JSON.stringify() for an object"
);
info("Testing IOUtils.writeJSON() with an array...");
await IOUtils.writeJSON(filename, ARRAY);
const readArray = await IOUtils.readJSON(filename);
const readArrayStr = await IOUtils.readUTF8(filename);
ok(ObjectUtils.deepEqual(ARRAY, readArray), "JSON arrays should round-trip");
ok(
readArrayStr === JSON.stringify(ARRAY), "IOUtils.writeJSON() equivalent to JSON.stringify() for an array"
);
info("Testing IOUtils.writeJSON() with primitives...");
for (const primitive of PRIMITIVES) {
await IOUtils.writeJSON(filename, primitive);
const readPrimitive = await IOUtils.readJSON(filename);
const readPrimitiveStr = await IOUtils.readUTF8(filename);
ok(
primitive === readPrimitive,
`${primitive} === ${readPrimitive} -- IOUtils.writeJSON() should round trip primitive`
);
ok(
readPrimitiveStr === JSON.stringify(primitive),
`${readPrimitiveStr} === ${JSON.stringify(primitive)} -- IOUtils.writeJSON() equivalent to JSON.stringify for primitive`
);
}
ok(Object.hasOwn(result, "size"), "result has size property");
ok(Object.hasOwn(result, "jsonLength"), "result has jsonLength property");
is(result.size, size, "Should have written the expected number of bytes");
is(result.jsonLength, expectedJson.length, "Should have written the expected number of UTF-16 codepoints");
}
isnot(result.size, size, "Should have written a different number of bytes due to compression");
is(result.jsonLength, expectedJson.length, "Should have written the same number of UTF-16 codepoints");
}
await cleanup(filename);
});
add_task(async function test_append_json() {
const filename = PathUtils.join(PathUtils.tempDir, "test_ioutils_append_json.tmp");
await IOUtils.writeJSON(filename, OBJECT);
await Assert.rejects(
IOUtils.writeJSON(filename, OBJECT, {mode: "append"}),
/NotSupportedError: Could not write to `.*': IOUtils.writeJSON does not support appending to files/, "IOUtils.writeJSON() cannot append"
);
await cleanup(filename);
});
add_task(async function test_read_json_bom() {
const tmpFileName = PathUtils.join(PathUtils.tempDir, "test_ioutils_read_json_bom.tmp");
const raw = `\uFEFF${JSON.stringify({hello: "world"})}`;
await IOUtils.writeUTF8(tmpFileName, raw);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.