Logo

Programming-Idioms

History of Idiom 91 > diff from v9 to v10

Edit summary for version 10 by :

Version 9

2015-11-04, 00:36:03

Version 10

2015-11-04, 00:44:59

Idiom #91 Load JSON file into struct

Read from file data.json and write its content into object x.
Assume the JSON data is suitable for the type of x.

Idiom #91 Load JSON file into struct

Read from file data.json and write its content into object x.
Assume the JSON data is suitable for the type of x.

Imports
import std.file: readText;
import std.json: parseJSon;
Imports
import std.file: readText;
import std.json: parseJSon;
Code
auto x = "data.json".readText.parseJSON;
Code
JSONValue x = "data.json".readText.parseJSON;

// We could use x as it is, but if we want to fill a structure
// the best is to define a constructor taking the JSONValue
struct User {
    int age;
    string name;

    this(JSONValue user) {
        age  = user["age"];
        name = user["name"];
    }
}

auto user = User(x);
Doc URL
http://dlang.org/phobos/std_json.html#.parseJSON
Doc URL
http://dlang.org/phobos/std_json.html#.parseJSON