fromFile

Deserializes data from a file.

Files are assumed to be UTF-8 encoded. If no format (or AutoDetect) is specified, an attempt at autodetection is made.

Supports $(SUPPORTEDSTRUCTURES).

T
fromFile
(
T
Format = AutoDetect
DeSiryulize flags = DeSiryulize.none
)
(
string path
)
if (
isSiryulizer!Format ||
is(Format == AutoDetect)
)

Parameters

T

Type stored in the file

Format

Serialization format ($(SUPPORTEDFORMATS))

path string

Absolute or relative path to the file

Return Value

Type: T

Data from the file in the format specified

Examples

import std.exception : assertThrown;
import std.file : exists, remove;
import std.stdio : File;
struct TestStruct {
	string a;
}
//Write some example files...
File("int.yml", "w").write("---\n9");
File("string.json", "w").write(`"test"`);
File("struct.yml", "w").write("---\na: b");
scope(exit) { //Clean up when done
	if ("int.yml".exists) {
		remove("int.yml");
	}
	if ("string.json".exists) {
		remove("string.json");
	}
	if ("struct.yml".exists) {
		remove("struct.yml");
	}
}
//Read examples from respective files
assert(fromFile!(uint, YAML)("int.yml") == 9);
assert(fromFile!(string, JSON)("string.json") == "test");
assert(fromFile!(TestStruct, YAML)("struct.yml") == TestStruct("b"));
//Read examples from respective files using automatic format detection
assert(fromFile!uint("int.yml") == 9);
assert(fromFile!string("string.json") == "test");
assert(fromFile!TestStruct("struct.yml") == TestStruct("b"));

assertThrown("file.obviouslybadextension".fromFile!uint);

Meta