Issues (81)

src/ub.files.json.js (2 issues)

1
/** global: UB */
2
//removeIf(nodejs)
3
4
5
6
var objectFuncs = {
7
8
	encodeJSON: function() {
9
		var object = this;
10
		try  {
11
			return JSON.stringify(object);
12
		} catch (err) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
13
		}
14
		return "{}";
15
	},
16
17
    none:null,
18
}
19
20
// register funcs
21
UB.registerFuncs(Object.prototype, objectFuncs);
22
23
24
25
var stringFuncs = {
26
27
	decodeJSON: function () {
28
		var data = this.toString();
29
		try  {
30
			return JSON.parse(data);
31
		} catch (err) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
32
		}
33
		return null;
34
	},
35
36
    none:null,
37
}
38
39
// register funcs
40
UB.registerFuncs(String.prototype, stringFuncs);
41
42
43
44
45
46
/* File Utils - NodeJS only */
47
48
var fs = require('fs');
49
50
51
52
objectFuncs = {
53
54
	saveToJSON: function(filePath) {
55
		var data = this;
56
57
		// write JSON to string
58
		var str = data.encodeJSON();
59
60
		// save data as string via filestream
61
		str.saveToText(filePath);
62
	},
63
64
    none:null,
65
}
66
67
// register funcs
68
UB.registerFuncs(Object.prototype, objectFuncs);
69
70
71
72
stringFuncs = {
73
74
	loadJSON: function(encoding = "utf8") {
75
76
		// load text file
77
		var file = this.toString();
78
		var text = file.loadText(encoding);
79
		if (text == null) {
80
			return null;
81
		}
82
83
		// parse JSON string into Object
84
		return text.decodeJSON();
85
	},
86
87
    none:null,
88
}
89
90
// register funcs
91
UB.registerFuncs(String.prototype, stringFuncs);
92
93
//endRemoveIf(nodejs)
94