本文整理汇总了TypeScript中json5.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: readKitsFile
export async function readKitsFile(filepath: string): Promise<Kit[]> {
if (!await fs.exists(filepath)) {
log.debug(`Not reading non-existent kits file: ${filepath}`);
return [];
}
log.debug('Reading kits file', filepath);
const content_str = await fs.readFile(filepath);
let kits_raw: object[] = [];
try {
kits_raw = json5.parse(content_str.toLocaleString());
} catch (e) {
log.error('Failed to parse cmake-kits.json:', e);
return [];
}
const validator = await loadSchema('schemas/kits-schema.json');
const is_valid = validator(kits_raw);
if (!is_valid) {
const errors = validator.errors!;
log.error(`Invalid cmake-kits.json (${filepath}):`);
for (const err of errors) {
log.error(` >> ${err.dataPath}: ${err.message}`);
}
return [];
}
const kits = kits_raw as Kit[];
log.info(`Successfully loaded ${kits.length} kits from ${filepath}`);
return dropNulls(kits);
}
示例2: handleObjectData
params.forEach(param => {
if (typeof param === 'object') {
handleObjectData(param, level)
} else if (isObjectAfterStringify(param)) {
handleObjectData(JSON5.parse(param), level)
} else {
consoleLog(param, level);
}
})
示例3: readFableConfigOptions
/** Reads options from fableconfig.json, requires json5 */
function readFableConfigOptions(opts: any) {
opts.workingDir = path.resolve(opts.workingDir || process.cwd());
if (typeof opts.projFile === "string") {
opts.projFile = [opts.projFile];
}
var cfgFile = fableLib.pathJoin(opts.workingDir, constants.FABLE_CONFIG_FILE);
if (Array.isArray(opts.projFile) && opts.projFile.length === 1) {
var fullProjFile = fableLib.pathJoin(opts.workingDir, opts.projFile[0]);
var projDir = fs.statSync(fullProjFile).isDirectory()
? fullProjFile
: path.dirname(fullProjFile);
cfgFile = fableLib.pathJoin(projDir, constants.FABLE_CONFIG_FILE);
// Delete projFile from opts if it isn't a true F# project
if (!fableLib.isFSharpProject(fullProjFile)) {
delete opts.projFile;
}
}
if (fs.existsSync(cfgFile)) {
// Change workingDir to where fableconfig.json is if necessary
if (opts.workingDir !== path.dirname(cfgFile)) {
for (var key in opts) {
opts[key] = resolvePath(key, opts[key], opts.workingDir);
}
opts.workingDir = path.dirname(cfgFile);
}
var cfg = json5.parse(fs.readFileSync(cfgFile).toString());
for (var key in cfg) {
if (key in opts === false)
opts[key] = cfg[key];
}
// Check if a target is requested
if (opts.debug) { opts.target = "debug" }
if (opts.production) { opts.target = "production" }
if (opts.target) {
if (!opts.targets || !opts.targets[opts.target]) {
throw "Target " + opts.target + " is missing";
}
cfg = opts.targets[opts.target];
for (key in cfg) {
if ((typeof cfg[key] === "object") && !Array.isArray(cfg[key]) &&
(typeof opts[key] === "object") && !Array.isArray(opts[key])) {
for (var key2 in cfg[key])
opts[key][key2] = cfg[key][key2];
}
else {
opts[key] = cfg[key];
}
}
}
}
return opts;
}
示例4: isObjectAfterStringify
function isObjectAfterStringify(s: string) {
//#region @backend
try {
const json = JSON5.parse(s);
return true;
} catch (error) {
return false;
}
//#endregion
}
示例5: default
export default ( value ) => {
if ( typeof value === "string" ) {
let param;
try {
param = JSON5.parse( value );
} catch ( e ) {
errorMessage( 'Format of "' + value + '" is incorrect. Please correct the format JSON5.' );
param = {};
}
return param;
} else {
return value;
}
};
示例6: readConfigFile
export function readConfigFile(pathToFile: string, dontThrowOnError?: boolean){
var resolvedPath = path.resolve(pathToFile);
var tsdFile;
try {
tsdFile = fs.readFileSync(resolvedPath,'utf8');
} catch(e) {
if(!dontThrowOnError) throw e;
return null;
}
var content = json.parse(tsdFile) || {};
var tsdConfigFile = makeTsdConfigFileBase(pathToFile);
tsdConfigFile.content = content;
if(content.path){
tsdConfigFile.definitionPath = path.resolve(tsdConfigFile.dir,content.path);
}
return tsdConfigFile;
}
示例7:
let workspaceFolders = vscode.workspace.workspaceFolders.filter((folder) =>
{
let asconfigPath = path.resolve(folder.uri.fsPath, "asconfig.json");
if(!fs.existsSync(asconfigPath) || fs.statSync(asconfigPath).isDirectory())
{
return false;
}
try
{
let contents = fs.readFileSync(asconfigPath, "utf8");
let result = json5.parse(contents);
if ("type" in result && result.type !== "app")
{
return false;
}
}
catch(error)
{
return false;
}
return true;
});
示例8: initTemplates
private initTemplates(location) {
let parsedJSON;
try {
parsedJSON = json5.parse(fs.readFileSync(location).toString()); // invalid JSON or permission issue can happen here
} catch (error) {
console.error(error);
return;
}
if (parsedJSON) {
const variables: IPlDocVariablesCustom = {
author: this.plDocAuthor,
date: new Date()
};
Object.keys(parsedJSON).forEach(key => {
// Doc
if (key === 'pldoc') {
if (this.plDocEnable && parsedJSON.pldoc.body) {
this.plDocTemplate = parsedJSON.pldoc;
this.addTemplateInfo(this.plDocTemplate);
}
else
this.plDocTemplate = null;
} else { // Other custom snippet
const snippet = parsedJSON[key];
snippet.body.forEach( (text, index) =>
snippet.body[index] = this.replaceText(variables, text)
);
if (!this.plDocSnippets)
this.plDocSnippets = [];
this.plDocSnippets.push(snippet);
}
});
}
}
示例9: _parseCondition
private static _parseCondition(source: string): ConditionBlock[] {
let ret: ConditionBlock[] = [];
const found = source.indexOf("@Condition(", 0);
if (found < 0) {
ret.push(ConditionBlock._createPlain(source));
return ret; // When there was no more found
}
let beginConditionPoint = found + 10;
const endConditionPoint: number = ConditionBlock._getEndBracketIndex(source, beginConditionPoint, "(", ")");
if (endConditionPoint < 1) {
// error no bracket matching
console.error("Invalid bracket matching!");
return null;
}
let beginContentPoint = source.indexOf("{", endConditionPoint);
if (beginContentPoint < 0) {
// error no bracket matching
console.error("Invalid bracket matching!");
return null;
}
const endContentPoint = ConditionBlock._getEndBracketIndex(source, beginContentPoint, "{", "}");
if (endContentPoint < 0) {
// error no bracket matching
console.error("Invalid bracket matching!");
return null;
}
let before: string = source.substring(0, found);
let condition: string = source.substring(beginConditionPoint + 1, endConditionPoint);
let jsonCondition: JSON = JSON5.parse(condition);
let content: string = source.substring(beginContentPoint + 1, endContentPoint);
let after: string = source.substring(endContentPoint + 1, source.length);
ret.push(ConditionBlock._createPlain(before));
ret.push(new ConditionBlock(jsonCondition, ConditionBlock._parseCondition(content), false, null));
ret = ret.concat(ConditionBlock._parseCondition(after));
return ret;
}
示例10: getAnimateFile
function getAnimateFile(folder: vscode.WorkspaceFolder)
{
let asconfigPath = path.resolve(folder.uri.fsPath, "asconfig.json");
if(!fs.existsSync(asconfigPath) || fs.statSync(asconfigPath).isDirectory())
{
return null;
}
try
{
let contents = fs.readFileSync(asconfigPath, "utf8");
let result = json5.parse(contents);
if ("type" in result && result.type !== "app")
{
return null;
}
if(!("animateOptions" in result))
{
return null;
}
let animateOptions = result.animateOptions;
if(!("file" in animateOptions))
{
return null;
}
let flaPath = animateOptions.file;
if(path.isAbsolute(flaPath))
{
return flaPath;
}
return path.resolve(folder.uri.fsPath, flaPath);
}
catch(error)
{
return null;
}
}