wv-gotcha
Version:
 wv-local-service-bus # Purpose To accumulate your walkthroughs to accomplish your common tasks with less effort.
47 lines • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Json = void 0;
const decova_filesystem_1 = require("decova-filesystem");
const decova_dotnet_developer_1 = require("decova-dotnet-developer");
class Json {
static Load(path, encoding = decova_filesystem_1.Encoding.utf8) {
let file = new decova_filesystem_1.FileInfo(path);
if (file.exists() === false)
throw new Error(`Path [${path}] doesn't exist.`);
let output;
let content = '';
try {
content = file.readAllText(encoding);
}
catch (err) {
throw new decova_dotnet_developer_1.Exception(`Couldn't read file [${path}]`);
}
try {
output = JSON.parse(content);
}
catch (err) {
throw new decova_dotnet_developer_1.Exception(`Couldn't parse file content as JSON [${content}]`);
}
return output;
}
static populate(obj, jsonFile) {
Object.assign(obj, this.Load(jsonFile));
}
static TrySave(path, obj, overwriteExisting, beautify = false, encoding = decova_filesystem_1.Encoding.utf8) {
const file = new decova_filesystem_1.FileInfo(path);
if (file.exists() && overwriteExisting === false) {
return false;
}
const text = beautify ? JSON.stringify(obj, null, '\t') : JSON.stringify(obj);
file.writeAllText(text);
return true;
}
static FromObject(obj) {
return JSON.stringify(obj);
}
static Parse(json) {
return JSON.parse(json);
}
}
exports.Json = Json;
//# sourceMappingURL=Json.js.map