UNPKG

decova-dotnet-developer

Version:

This package provides fundumentals that a .net developer may miss while working with Typescript, whether they are missing functinalities or funcionalities provided in a non-elegant design in javascript. Bad naming, bad design of optional parameters, non-c

46 lines 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Json = void 0; const decova_filesystem_1 = require("decova-filesystem"); const Exceptions_1 = require("../Exceptions/Exceptions"); 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 Exceptions_1.Exception(`Couldn't read file [${path}]`); } try { output = JSON.parse(content); } catch (err) { throw new Exceptions_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, encoding = decova_filesystem_1.Encoding.utf8) { const file = new decova_filesystem_1.FileInfo(path); if (file.Exists() && overwriteExisting === false) { return false; } file.WriteAllText(JSON.stringify(obj)); return true; } static FromObject(obj) { return JSON.stringify(obj); } static Parse(json) { return JSON.parse(json); } } exports.Json = Json; //# sourceMappingURL=Json.js.map