mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-03 19:51:53 +00:00
Add JSON5 missing type
This commit is contained in:
Vendored
+14
-1
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user