【问题标题】:Solid: correct way to store a JSON objectSolid:存储 JSON 对象的正确方法
【发布时间】:2021-05-16 18:49:05
【问题描述】:

我想在用户的 Solid pod 中存储一个标准 JSON 对象。完成 Solid 入门教程后,我发现我可以在 VCARD.note 参数中获取/设置对象,但我怀疑这不是正确的方法。

关于如何正确执行此操作的任何建议? JSON 对象会定期更新,通常会有大约 10-100 个密钥对。

【问题讨论】:

    标签: solid


    【解决方案1】:

    这里有两个选项。

    选项 1 - 存储为 RDF(推荐)

    通常,建议不要将数据存储为标准 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/ 阅读有关此方法的更多信息

    选项 2 - 直接存储 JSON blob

    另一种选择确实是将 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多