require() caches when requiring a file for the first time and then uses that cache for future require() calls, so use fs.readFileSync() if the content of the JSON file changes during runtime.
JSONValue x = "data.json".readText.parseJSON;
struct User {
int age;
string name;
this(JSONValue user) {
age = user["age"];
name = user["name"];
}
}
auto user = User(x);
We could use the JSONValue x as it is, but if we want to fill a given structure the best is to define a specific constructor.
JSONValue x = "data.json".readText.parseJSON;
struct User {
int age;
string name;
this(JSONValue user) {
age = user["age"];
name = user["name"];
}
}
auto user = User(x);
defmodule JsonTest do
def get_json(filename) do
with {:ok, body} <- File.read(filename),
{:ok, json} <- Poison.decode(body), do: {:ok, json}
end
end
x = JsonTest.get_json("data.json")