我想在用户的 Solid pod 中存储一个标准 JSON 对象。在完成 Solid 入门教程后,我发现我可以在 VCARD.note 参数中获取/设置对象,但我怀疑这不是正确的方法。
关于如何正确执行此操作的任何建议?JSON 对象会定期更新,通常会有大约 10-100 个密钥对。
我想在用户的 Solid pod 中存储一个标准 JSON 对象。在完成 Solid 入门教程后,我发现我可以在 VCARD.note 参数中获取/设置对象,但我怀疑这不是正确的方法。
关于如何正确执行此操作的任何建议?JSON 对象会定期更新,通常会有大约 10-100 个密钥对。
这里有两个选项。
通常,建议不要将数据存储为标准 JSON 对象,而是将数据保存为 RDF。例如,如果您有一个 JSON 对象,例如
const user = {
name: "Vincent",
};
假设您使用的是 JavaScript 库@inrupt/solid-client
,您将创建它所谓的“事物”,如下所示:
import { createThing, addStringNoLocale } from "@inrupt/solid-client";
import { foaf } from "rdf-namespaces";
let userThing = createThing();
userThing = addStringNoLocale(userThing, foaf.fn, "Vincent");
您可以在https://docs.inrupt.com/developer-tools/javascript/client-libraries/tutorial/read-write-data/阅读有关此方法的更多信息
另一种选择确实是将 JSON 文件直接存储在 Pod 中。这可行,尽管它有点违背 Solid 的精神,并且要求您每次都覆盖整个文件,而不是允许您在更新数据时只更新单个属性。你可以这样做:
import { overwriteFile } from "@inrupt/solid-client";
const user = {
name: "Vincent",
};
// This is assuming you're working in the browser;
// in Node, you'll have to create a Buffer instead of a Blob.
overwriteFile(
"https://my.pod/location-of-the-file.json",
new Blob([
JSON.stringify(user),
]),
{ type: "application/json" },
).then(() => console.log("Saved the JSON file.}));
您可以在此处阅读有关此方法的更多信息:https ://docs.inrupt.com/developer-tools/javascript/client-libraries/tutorial/read-write-files/