Add JSON5 missing type

This commit is contained in:
Kacper Wiszczuk
2018-08-13 15:14:43 +02:00
parent 01f7c09835
commit 49d55176a9
2 changed files with 31 additions and 4 deletions
+14 -1
View File
@@ -40,5 +40,18 @@ Comments
Both inline (single-line) and block (multi-line) comments are allowed.
*/
declare var json5: JSON;
type JSONReplacer = (key: string, value: any) => any | (number | string)[] | null;
interface JSON5 {
// Old JSON methods
parse(text: string, reviver?: (key: any, value: any) => any): any;
stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string;
stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
// New JSON5 stringify function
stringify(value: any, options?: { space?: number | string, quote?: string, replacer?: JSONReplacer }): string;
}
declare var json5: JSON5;
export = json5;
+17 -3
View File
@@ -1,7 +1,21 @@
import JSON5 = require('json5');
var obj = JSON5.parse("{ key:'val', 'key2':[0,1,2,] } //comment ");
var str = JSON5.stringify(obj, null, "\t");
console.log(str);
const STR = "{ key:'val', 'key2':[0,1,2,] } //comment ";
const OBJ = { key: 'value', key2: [0, 1, 2] };
function reviverFunction(key: any, value: any): any {
return { [key]: value };
}
function replacerFunction(key: string, value: any) {
return { [key]: value };
}
const str1: string = JSON5.stringify(OBJ);
const str2: string = JSON5.stringify(OBJ, null, 4);
const str3: string = JSON5.stringify(OBJ, undefined, '2');
const str4: string = JSON5.stringify(OBJ, replacerFunction, 2);
JSON.parse(STR);
JSON.parse(STR, reviverFunction);