mirror of
https://github.com/Unitech/pm2.git
synced 2025-12-08 20:35:53 +00:00
add JSON5 support - better sample file - restructure samples
This commit is contained in:
parent
4abd5bebdf
commit
90f1181d8c
@ -26,8 +26,8 @@ var csts = {
|
||||
PREFIX_MSG_WARNING : '\x1B[33m[PM2] [WARN] \x1B[39m',
|
||||
PREFIX_MSG_SUCCESS : '\x1B[36;1m[PM2] \x1B[39;0m',
|
||||
|
||||
SAMPLE_FILE_PATH : '../lib/sample.json',
|
||||
SAMPLE_CONF_FILE : '../lib/sample-conf.js',
|
||||
SAMPLE_FILE_PATH : '../lib/samples/sample.json5',
|
||||
SAMPLE_CONF_FILE : '../lib/samples/sample-conf.js',
|
||||
|
||||
CENTOS_STARTUP_SCRIPT : '../lib/scripts/pm2-init-centos.sh',
|
||||
UBUNTU_STARTUP_SCRIPT : '../lib/scripts/pm2-init.sh',
|
||||
@ -77,7 +77,7 @@ var default_conf = {
|
||||
|
||||
INTERACTOR_LOG_FILE_PATH : p.join(PM2_ROOT_PATH, 'agent.log'),
|
||||
INTERACTOR_PID_PATH : p.join(PM2_ROOT_PATH, 'agent.pid'),
|
||||
INTERACTION_CONF : p.join(PM2_ROOT_PATH, 'agent.json')
|
||||
INTERACTION_CONF : p.join(PM2_ROOT_PATH, 'agent.json5')
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
21
lib/CLI.js
21
lib/CLI.js
@ -18,6 +18,8 @@ var extItps = require('./interpreter.json');
|
||||
var InteractorDaemonizer = require('./Interactor/InteractorDaemonizer');
|
||||
var p = path;
|
||||
|
||||
var json5 = require('./tools/json5.js');
|
||||
|
||||
var Deploy = require('pm2-deploy');
|
||||
|
||||
var CLI = module.exports = {};
|
||||
@ -231,7 +233,7 @@ CLI.deploy = function(file, commands, cb) {
|
||||
env = args[1];
|
||||
|
||||
try {
|
||||
var json_conf = JSON.parse(fs.readFileSync(file));
|
||||
var json_conf = json5.parse(fs.readFileSync(file));
|
||||
} catch (e) {
|
||||
printError(e);
|
||||
return cb ? cb(e) : exitCli(cst.ERROR_EXIT);
|
||||
@ -281,10 +283,10 @@ CLI.actionFromJson = function(action, file, jsonVia, cb) {
|
||||
var appConf;
|
||||
|
||||
if (jsonVia == 'pipe')
|
||||
appConf = JSON.parse(file);
|
||||
appConf = json5.parse(file);
|
||||
else {
|
||||
var data = fs.readFileSync(file);
|
||||
appConf = JSON.parse(data);
|
||||
appConf = json5.parse(data);
|
||||
// v2 JSON declaration
|
||||
if (appConf.apps) appConf = appConf.apps;
|
||||
}
|
||||
@ -352,14 +354,15 @@ CLI.startJson = function(cmd, opts, jsonVia, cb) {
|
||||
var appConf;
|
||||
|
||||
if (jsonVia == 'pipe')
|
||||
appConf = JSON.parse(cmd);
|
||||
appConf = json5.parse(cmd);
|
||||
else {
|
||||
var data = fs.readFileSync(cmd);
|
||||
appConf = JSON.parse(data);
|
||||
appConf = json5.parse(data);
|
||||
// v2 JSON declaration
|
||||
if (appConf.apps) appConf = appConf.apps;
|
||||
}
|
||||
|
||||
|
||||
if (!Array.isArray(appConf)) appConf = [appConf]; //convert to array
|
||||
|
||||
(function ex(apps) {
|
||||
@ -612,7 +615,7 @@ CLI.resurrect = function(cb) {
|
||||
return ex(apps);
|
||||
});
|
||||
return false;
|
||||
})(JSON.parse(apps));
|
||||
})(json5.parse(apps));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -674,7 +677,7 @@ CLI.dump = function(cb) {
|
||||
* @return
|
||||
*/
|
||||
function fin(err) {
|
||||
fs.writeFileSync(cst.DUMP_FILE_PATH, JSON.stringify(env_arr));
|
||||
fs.writeFileSync(cst.DUMP_FILE_PATH, json5.stringify(env_arr));
|
||||
if (cb) return cb(null, {success:true});
|
||||
else return exitCli(cst.SUCCESS_EXIT);
|
||||
}
|
||||
@ -867,7 +870,7 @@ CLI._jsonStartOrAction = function(action, json_conf, opts, cb) {
|
||||
return cb ? cb(e) : exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
|
||||
var appConf = JSON.parse(data);
|
||||
var appConf = json5.parse(data);
|
||||
// v2 JSON declaration
|
||||
if (appConf.apps) appConf = appConf.apps;
|
||||
|
||||
@ -1114,7 +1117,7 @@ CLI._stop = function(process_name, cb) {
|
||||
CLI.generateSample = function() {
|
||||
var sample = fs.readFileSync(path.join(__dirname, cst.SAMPLE_FILE_PATH));
|
||||
var dt = sample.toString();
|
||||
var f_name = 'ecosystem.json';
|
||||
var f_name = 'ecosystem.json5';
|
||||
|
||||
fs.writeFileSync(path.join(process.env.PWD, f_name), dt);
|
||||
printOut('File %s generated', path.join(process.env.PWD, f_name));
|
||||
|
||||
@ -9,7 +9,8 @@ var rpc = require('pm2-axon-rpc');
|
||||
var Common = require('../Common');
|
||||
var debug = require('debug')('pm2:interface:daemon');
|
||||
var axon = require('pm2-axon');
|
||||
var chalk = require('chalk');
|
||||
var chalk = require('chalk');
|
||||
var json5 = require('../tools/json5.js');
|
||||
|
||||
var InteractorDaemonizer = module.exports = {};
|
||||
|
||||
@ -294,7 +295,7 @@ InteractorDaemonizer.getSetKeys = function(secret_key, public_key, machine_name,
|
||||
|
||||
// If object
|
||||
if (secret_key && typeof(secret_key) == 'object') {
|
||||
var cpy = JSON.parse(JSON.stringify(secret_key));
|
||||
var cpy = json5.parse(json5.stringify(secret_key));
|
||||
cb = public_key;
|
||||
secret_key = cpy.secret_key;
|
||||
public_key = cpy.public_key;
|
||||
@ -302,7 +303,7 @@ InteractorDaemonizer.getSetKeys = function(secret_key, public_key, machine_name,
|
||||
}
|
||||
|
||||
try {
|
||||
var interaction_conf = JSON.parse(fs.readFileSync(cst.INTERACTION_CONF));
|
||||
var interaction_conf = json5.parse(fs.readFileSync(cst.INTERACTION_CONF));
|
||||
|
||||
secret_key = secret_key ? secret_key : interaction_conf.secret_key;
|
||||
public_key = public_key ? public_key : interaction_conf.public_key;
|
||||
@ -339,8 +340,9 @@ InteractorDaemonizer.getSetKeys = function(secret_key, public_key, machine_name,
|
||||
public_key : public_key,
|
||||
machine_name : machine_name,
|
||||
reverse_interact : reverse_interact
|
||||
|
||||
};
|
||||
fs.writeFileSync(cst.INTERACTION_CONF, JSON.stringify(new_interaction_conf, null, 4));
|
||||
fs.writeFileSync(cst.INTERACTION_CONF, json5.stringify(new_interaction_conf, null, 4));
|
||||
} catch(e) {
|
||||
console.error('Error when writting configuration file %s', cst.INTERACTION_CONF);
|
||||
}
|
||||
|
||||
@ -1,33 +0,0 @@
|
||||
{
|
||||
"apps" : [{
|
||||
"name" : "API",
|
||||
"script" : "app.js",
|
||||
"env": {
|
||||
"COMMON_VARIABLE": "true"
|
||||
},
|
||||
"env_production" : {
|
||||
"NODE_ENV": "production"
|
||||
}
|
||||
},{
|
||||
"name" : "WEB",
|
||||
"script" : "web.js"
|
||||
}],
|
||||
"deploy" : {
|
||||
"production" : {
|
||||
"user" : "node",
|
||||
"host" : "212.83.163.1",
|
||||
"ref" : "origin/master",
|
||||
"repo" : "git@github.com:repo.git",
|
||||
"path" : "/var/www/production",
|
||||
"post-deploy" : "pm2 startOrRestart ecosystem.json --env production"
|
||||
},
|
||||
"dev" : {
|
||||
"user" : "node",
|
||||
"host" : "212.83.163.1",
|
||||
"ref" : "origin/master",
|
||||
"repo" : "git@github.com:repo.git",
|
||||
"path" : "/var/www/development",
|
||||
"post-deploy" : "pm2 startOrRestart ecosystem.json --env dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
58
lib/sample.json5
Normal file
58
lib/sample.json5
Normal file
@ -0,0 +1,58 @@
|
||||
{
|
||||
/**
|
||||
* This is a sample configuration file for PM2
|
||||
*/
|
||||
|
||||
/**
|
||||
* Here we declare the apps that must be managed by PM2
|
||||
* All options are listed here:
|
||||
* https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#json-app-declaration
|
||||
*
|
||||
*/
|
||||
apps : [
|
||||
|
||||
// First application
|
||||
{
|
||||
name : "API",
|
||||
script : "app.js",
|
||||
env: {
|
||||
COMMON_VARIABLE: "true"
|
||||
},
|
||||
env_production : {
|
||||
NODE_ENV: "production"
|
||||
}
|
||||
},
|
||||
|
||||
// Second application
|
||||
{
|
||||
name : "WEB",
|
||||
script : "web.js"
|
||||
}
|
||||
|
||||
],
|
||||
|
||||
|
||||
/**
|
||||
* PM2 help you to deploy apps over your servers
|
||||
* For more help go to :
|
||||
* https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#deployment-pm2--090
|
||||
*/
|
||||
deploy : {
|
||||
production : {
|
||||
user : "node",
|
||||
host : "212.83.163.1",
|
||||
ref : "origin/master",
|
||||
repo : "git@github.com:repo.git",
|
||||
path : "/var/www/production",
|
||||
"post-deploy" : "pm2 startOrRestart ecosystem.json --env production"
|
||||
},
|
||||
dev : {
|
||||
user : "node",
|
||||
host : "212.83.163.1",
|
||||
ref : "origin/master",
|
||||
repo : "git@github.com:repo.git",
|
||||
path : "/var/www/development",
|
||||
"post-deploy" : "pm2 startOrRestart ecosystem.json --env dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -31,7 +31,7 @@ module.exports = function(DEFAULT_HOME) {
|
||||
|
||||
INTERACTOR_LOG_FILE_PATH : p.join(PM2_HOME, 'agent.log'),
|
||||
INTERACTOR_PID_PATH : p.join(PM2_HOME, 'agent.pid'),
|
||||
INTERACTION_CONF : p.join(PM2_HOME, 'agent.json')
|
||||
INTERACTION_CONF : p.join(PM2_HOME, 'agent.json5')
|
||||
};
|
||||
|
||||
return pm2_conf || null;
|
||||
754
lib/tools/json5.js
Normal file
754
lib/tools/json5.js
Normal file
@ -0,0 +1,754 @@
|
||||
// json5.js
|
||||
// Modern JSON. See README.md for details.
|
||||
//
|
||||
// This file is based directly off of Douglas Crockford's json_parse.js:
|
||||
// https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
|
||||
|
||||
var JSON5 = (typeof exports === 'object' ? exports : {});
|
||||
|
||||
JSON5.parse = (function () {
|
||||
"use strict";
|
||||
|
||||
// This is a function that can parse a JSON5 text, producing a JavaScript
|
||||
// data structure. It is a simple, recursive descent parser. It does not use
|
||||
// eval or regular expressions, so it can be used as a model for implementing
|
||||
// a JSON5 parser in other languages.
|
||||
|
||||
// We are defining the function inside of another function to avoid creating
|
||||
// global variables.
|
||||
|
||||
var at, // The index of the current character
|
||||
ch, // The current character
|
||||
escapee = {
|
||||
"'": "'",
|
||||
'"': '"',
|
||||
'\\': '\\',
|
||||
'/': '/',
|
||||
'\n': '', // Replace escaped newlines in strings w/ empty string
|
||||
b: '\b',
|
||||
f: '\f',
|
||||
n: '\n',
|
||||
r: '\r',
|
||||
t: '\t'
|
||||
},
|
||||
ws = [
|
||||
' ',
|
||||
'\t',
|
||||
'\r',
|
||||
'\n',
|
||||
'\v',
|
||||
'\f',
|
||||
'\xA0',
|
||||
'\uFEFF'
|
||||
],
|
||||
text,
|
||||
|
||||
error = function (m) {
|
||||
|
||||
// Call error when something is wrong.
|
||||
|
||||
var error = new SyntaxError();
|
||||
error.message = m;
|
||||
error.at = at;
|
||||
error.text = text;
|
||||
throw error;
|
||||
},
|
||||
|
||||
next = function (c) {
|
||||
|
||||
// If a c parameter is provided, verify that it matches the current character.
|
||||
|
||||
if (c && c !== ch) {
|
||||
error("Expected '" + c + "' instead of '" + ch + "'");
|
||||
}
|
||||
|
||||
// Get the next character. When there are no more characters,
|
||||
// return the empty string.
|
||||
|
||||
ch = text.charAt(at);
|
||||
at += 1;
|
||||
return ch;
|
||||
},
|
||||
|
||||
peek = function () {
|
||||
|
||||
// Get the next character without consuming it or
|
||||
// assigning it to the ch varaible.
|
||||
|
||||
return text.charAt(at);
|
||||
},
|
||||
|
||||
identifier = function () {
|
||||
|
||||
// Parse an identifier. Normally, reserved words are disallowed here, but we
|
||||
// only use this for unquoted object keys, where reserved words are allowed,
|
||||
// so we don't check for those here. References:
|
||||
// - http://es5.github.com/#x7.6
|
||||
// - https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables
|
||||
// - http://docstore.mik.ua/orelly/webprog/jscript/ch02_07.htm
|
||||
// TODO Identifiers can have Unicode "letters" in them; add support for those.
|
||||
|
||||
var key = ch;
|
||||
|
||||
// Identifiers must start with a letter, _ or $.
|
||||
if ((ch !== '_' && ch !== '$') &&
|
||||
(ch < 'a' || ch > 'z') &&
|
||||
(ch < 'A' || ch > 'Z')) {
|
||||
error("Bad identifier");
|
||||
}
|
||||
|
||||
// Subsequent characters can contain digits.
|
||||
while (next() && (
|
||||
ch === '_' || ch === '$' ||
|
||||
(ch >= 'a' && ch <= 'z') ||
|
||||
(ch >= 'A' && ch <= 'Z') ||
|
||||
(ch >= '0' && ch <= '9'))) {
|
||||
key += ch;
|
||||
}
|
||||
|
||||
return key;
|
||||
},
|
||||
|
||||
number = function () {
|
||||
|
||||
// Parse a number value.
|
||||
|
||||
var number,
|
||||
sign = '',
|
||||
string = '',
|
||||
base = 10;
|
||||
|
||||
if (ch === '-' || ch === '+') {
|
||||
sign = ch;
|
||||
next(ch);
|
||||
}
|
||||
|
||||
// support for Infinity (could tweak to allow other words):
|
||||
if (ch === 'I') {
|
||||
number = word();
|
||||
if (typeof number !== 'number' || isNaN(number)) {
|
||||
error('Unexpected word for number');
|
||||
}
|
||||
return (sign === '-') ? -number : number;
|
||||
}
|
||||
|
||||
// support for NaN
|
||||
if (ch === 'N' ) {
|
||||
number = word();
|
||||
if (!isNaN(number)) {
|
||||
error('expected word to be NaN');
|
||||
}
|
||||
// ignore sign as -NaN also is NaN
|
||||
return number;
|
||||
}
|
||||
|
||||
if (ch === '0') {
|
||||
string += ch;
|
||||
next();
|
||||
if (ch === 'x' || ch === 'X') {
|
||||
string += ch;
|
||||
next();
|
||||
base = 16;
|
||||
} else if (ch >= '0' && ch <= '9') {
|
||||
error('Octal literal');
|
||||
}
|
||||
}
|
||||
|
||||
switch (base) {
|
||||
case 10:
|
||||
while (ch >= '0' && ch <= '9' ) {
|
||||
string += ch;
|
||||
next();
|
||||
}
|
||||
if (ch === '.') {
|
||||
string += '.';
|
||||
while (next() && ch >= '0' && ch <= '9') {
|
||||
string += ch;
|
||||
}
|
||||
}
|
||||
if (ch === 'e' || ch === 'E') {
|
||||
string += ch;
|
||||
next();
|
||||
if (ch === '-' || ch === '+') {
|
||||
string += ch;
|
||||
next();
|
||||
}
|
||||
while (ch >= '0' && ch <= '9') {
|
||||
string += ch;
|
||||
next();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
while (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {
|
||||
string += ch;
|
||||
next();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if(sign === '-') {
|
||||
number = -string;
|
||||
} else {
|
||||
number = +string;
|
||||
}
|
||||
|
||||
if (!isFinite(number)) {
|
||||
error("Bad number");
|
||||
} else {
|
||||
return number;
|
||||
}
|
||||
},
|
||||
|
||||
string = function () {
|
||||
|
||||
// Parse a string value.
|
||||
|
||||
var hex,
|
||||
i,
|
||||
string = '',
|
||||
delim, // double quote or single quote
|
||||
uffff;
|
||||
|
||||
// When parsing for string values, we must look for ' or " and \ characters.
|
||||
|
||||
if (ch === '"' || ch === "'") {
|
||||
delim = ch;
|
||||
while (next()) {
|
||||
if (ch === delim) {
|
||||
next();
|
||||
return string;
|
||||
} else if (ch === '\\') {
|
||||
next();
|
||||
if (ch === 'u') {
|
||||
uffff = 0;
|
||||
for (i = 0; i < 4; i += 1) {
|
||||
hex = parseInt(next(), 16);
|
||||
if (!isFinite(hex)) {
|
||||
break;
|
||||
}
|
||||
uffff = uffff * 16 + hex;
|
||||
}
|
||||
string += String.fromCharCode(uffff);
|
||||
} else if (ch === '\r') {
|
||||
if (peek() === '\n') {
|
||||
next();
|
||||
}
|
||||
} else if (typeof escapee[ch] === 'string') {
|
||||
string += escapee[ch];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if (ch === '\n') {
|
||||
// unescaped newlines are invalid; see:
|
||||
// https://github.com/aseemk/json5/issues/24
|
||||
// TODO this feels special-cased; are there other
|
||||
// invalid unescaped chars?
|
||||
break;
|
||||
} else {
|
||||
string += ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
error("Bad string");
|
||||
},
|
||||
|
||||
inlineComment = function () {
|
||||
|
||||
// Skip an inline comment, assuming this is one. The current character should
|
||||
// be the second / character in the // pair that begins this inline comment.
|
||||
// To finish the inline comment, we look for a newline or the end of the text.
|
||||
|
||||
if (ch !== '/') {
|
||||
error("Not an inline comment");
|
||||
}
|
||||
|
||||
do {
|
||||
next();
|
||||
if (ch === '\n' || ch === '\r') {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
} while (ch);
|
||||
},
|
||||
|
||||
blockComment = function () {
|
||||
|
||||
// Skip a block comment, assuming this is one. The current character should be
|
||||
// the * character in the /* pair that begins this block comment.
|
||||
// To finish the block comment, we look for an ending */ pair of characters,
|
||||
// but we also watch for the end of text before the comment is terminated.
|
||||
|
||||
if (ch !== '*') {
|
||||
error("Not a block comment");
|
||||
}
|
||||
|
||||
do {
|
||||
next();
|
||||
while (ch === '*') {
|
||||
next('*');
|
||||
if (ch === '/') {
|
||||
next('/');
|
||||
return;
|
||||
}
|
||||
}
|
||||
} while (ch);
|
||||
|
||||
error("Unterminated block comment");
|
||||
},
|
||||
|
||||
comment = function () {
|
||||
|
||||
// Skip a comment, whether inline or block-level, assuming this is one.
|
||||
// Comments always begin with a / character.
|
||||
|
||||
if (ch !== '/') {
|
||||
error("Not a comment");
|
||||
}
|
||||
|
||||
next('/');
|
||||
|
||||
if (ch === '/') {
|
||||
inlineComment();
|
||||
} else if (ch === '*') {
|
||||
blockComment();
|
||||
} else {
|
||||
error("Unrecognized comment");
|
||||
}
|
||||
},
|
||||
|
||||
white = function () {
|
||||
|
||||
// Skip whitespace and comments.
|
||||
// Note that we're detecting comments by only a single / character.
|
||||
// This works since regular expressions are not valid JSON(5), but this will
|
||||
// break if there are other valid values that begin with a / character!
|
||||
|
||||
while (ch) {
|
||||
if (ch === '/') {
|
||||
comment();
|
||||
} else if (ws.indexOf(ch) >= 0) {
|
||||
next();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
word = function () {
|
||||
|
||||
// true, false, or null.
|
||||
|
||||
switch (ch) {
|
||||
case 't':
|
||||
next('t');
|
||||
next('r');
|
||||
next('u');
|
||||
next('e');
|
||||
return true;
|
||||
case 'f':
|
||||
next('f');
|
||||
next('a');
|
||||
next('l');
|
||||
next('s');
|
||||
next('e');
|
||||
return false;
|
||||
case 'n':
|
||||
next('n');
|
||||
next('u');
|
||||
next('l');
|
||||
next('l');
|
||||
return null;
|
||||
case 'I':
|
||||
next('I');
|
||||
next('n');
|
||||
next('f');
|
||||
next('i');
|
||||
next('n');
|
||||
next('i');
|
||||
next('t');
|
||||
next('y');
|
||||
return Infinity;
|
||||
case 'N':
|
||||
next( 'N' );
|
||||
next( 'a' );
|
||||
next( 'N' );
|
||||
return NaN;
|
||||
}
|
||||
error("Unexpected '" + ch + "'");
|
||||
},
|
||||
|
||||
value, // Place holder for the value function.
|
||||
|
||||
array = function () {
|
||||
|
||||
// Parse an array value.
|
||||
|
||||
var array = [];
|
||||
|
||||
if (ch === '[') {
|
||||
next('[');
|
||||
white();
|
||||
while (ch) {
|
||||
if (ch === ']') {
|
||||
next(']');
|
||||
return array; // Potentially empty array
|
||||
}
|
||||
// ES5 allows omitting elements in arrays, e.g. [,] and
|
||||
// [,null]. We don't allow this in JSON5.
|
||||
if (ch === ',') {
|
||||
error("Missing array element");
|
||||
} else {
|
||||
array.push(value());
|
||||
}
|
||||
white();
|
||||
// If there's no comma after this value, this needs to
|
||||
// be the end of the array.
|
||||
if (ch !== ',') {
|
||||
next(']');
|
||||
return array;
|
||||
}
|
||||
next(',');
|
||||
white();
|
||||
}
|
||||
}
|
||||
error("Bad array");
|
||||
},
|
||||
|
||||
object = function () {
|
||||
|
||||
// Parse an object value.
|
||||
|
||||
var key,
|
||||
object = {};
|
||||
|
||||
if (ch === '{') {
|
||||
next('{');
|
||||
white();
|
||||
while (ch) {
|
||||
if (ch === '}') {
|
||||
next('}');
|
||||
return object; // Potentially empty object
|
||||
}
|
||||
|
||||
// Keys can be unquoted. If they are, they need to be
|
||||
// valid JS identifiers.
|
||||
if (ch === '"' || ch === "'") {
|
||||
key = string();
|
||||
} else {
|
||||
key = identifier();
|
||||
}
|
||||
|
||||
white();
|
||||
next(':');
|
||||
object[key] = value();
|
||||
white();
|
||||
// If there's no comma after this pair, this needs to be
|
||||
// the end of the object.
|
||||
if (ch !== ',') {
|
||||
next('}');
|
||||
return object;
|
||||
}
|
||||
next(',');
|
||||
white();
|
||||
}
|
||||
}
|
||||
error("Bad object");
|
||||
};
|
||||
|
||||
value = function () {
|
||||
|
||||
// Parse a JSON value. It could be an object, an array, a string, a number,
|
||||
// or a word.
|
||||
|
||||
white();
|
||||
switch (ch) {
|
||||
case '{':
|
||||
return object();
|
||||
case '[':
|
||||
return array();
|
||||
case '"':
|
||||
case "'":
|
||||
return string();
|
||||
case '-':
|
||||
case '+':
|
||||
case '.':
|
||||
return number();
|
||||
default:
|
||||
return ch >= '0' && ch <= '9' ? number() : word();
|
||||
}
|
||||
};
|
||||
|
||||
// Return the json_parse function. It will have access to all of the above
|
||||
// functions and variables.
|
||||
|
||||
return function (source, reviver) {
|
||||
var result;
|
||||
|
||||
text = String(source);
|
||||
at = 0;
|
||||
ch = ' ';
|
||||
result = value();
|
||||
white();
|
||||
if (ch) {
|
||||
error("Syntax error");
|
||||
}
|
||||
|
||||
// If there is a reviver function, we recursively walk the new structure,
|
||||
// passing each name/value pair to the reviver function for possible
|
||||
// transformation, starting with a temporary root object that holds the result
|
||||
// in an empty key. If there is not a reviver function, we simply return the
|
||||
// result.
|
||||
|
||||
return typeof reviver === 'function' ? (function walk(holder, key) {
|
||||
var k, v, value = holder[key];
|
||||
if (value && typeof value === 'object') {
|
||||
for (k in value) {
|
||||
if (Object.prototype.hasOwnProperty.call(value, k)) {
|
||||
v = walk(value, k);
|
||||
if (v !== undefined) {
|
||||
value[k] = v;
|
||||
} else {
|
||||
delete value[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return reviver.call(holder, key, value);
|
||||
}({'': result}, '')) : result;
|
||||
};
|
||||
}());
|
||||
|
||||
// JSON5 stringify will not quote keys where appropriate
|
||||
JSON5.stringify = function (obj, replacer, space) {
|
||||
if (replacer && (typeof(replacer) !== "function" && !isArray(replacer))) {
|
||||
throw new Error('Replacer must be a function or an array');
|
||||
}
|
||||
var getReplacedValueOrUndefined = function(holder, key, isTopLevel) {
|
||||
var value = holder[key];
|
||||
|
||||
// Replace the value with its toJSON value first, if possible
|
||||
if (value && value.toJSON && typeof value.toJSON === "function") {
|
||||
value = value.toJSON();
|
||||
}
|
||||
|
||||
// If the user-supplied replacer if a function, call it. If it's an array, check objects' string keys for
|
||||
// presence in the array (removing the key/value pair from the resulting JSON if the key is missing).
|
||||
if (typeof(replacer) === "function") {
|
||||
return replacer.call(holder, key, value);
|
||||
} else if(replacer) {
|
||||
if (isTopLevel || isArray(holder) || replacer.indexOf(key) >= 0) {
|
||||
return value;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
function isWordChar(char) {
|
||||
return (char >= 'a' && char <= 'z') ||
|
||||
(char >= 'A' && char <= 'Z') ||
|
||||
(char >= '0' && char <= '9') ||
|
||||
char === '_' || char === '$';
|
||||
}
|
||||
|
||||
function isWordStart(char) {
|
||||
return (char >= 'a' && char <= 'z') ||
|
||||
(char >= 'A' && char <= 'Z') ||
|
||||
char === '_' || char === '$';
|
||||
}
|
||||
|
||||
function isWord(key) {
|
||||
if (typeof key !== 'string') {
|
||||
return false;
|
||||
}
|
||||
if (!isWordStart(key[0])) {
|
||||
return false;
|
||||
}
|
||||
var i = 1, length = key.length;
|
||||
while (i < length) {
|
||||
if (!isWordChar(key[i])) {
|
||||
return false;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// export for use in tests
|
||||
JSON5.isWord = isWord;
|
||||
|
||||
// polyfills
|
||||
function isArray(obj) {
|
||||
if (Array.isArray) {
|
||||
return Array.isArray(obj);
|
||||
} else {
|
||||
return Object.prototype.toString.call(obj) === '[object Array]';
|
||||
}
|
||||
}
|
||||
|
||||
function isDate(obj) {
|
||||
return Object.prototype.toString.call(obj) === '[object Date]';
|
||||
}
|
||||
|
||||
isNaN = isNaN || function(val) {
|
||||
return typeof val === 'number' && val !== val;
|
||||
};
|
||||
|
||||
var objStack = [];
|
||||
function checkForCircular(obj) {
|
||||
for (var i = 0; i < objStack.length; i++) {
|
||||
if (objStack[i] === obj) {
|
||||
throw new TypeError("Converting circular structure to JSON");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function makeIndent(str, num, noNewLine) {
|
||||
if (!str) {
|
||||
return "";
|
||||
}
|
||||
// indentation no more than 10 chars
|
||||
if (str.length > 10) {
|
||||
str = str.substring(0, 10);
|
||||
}
|
||||
|
||||
var indent = noNewLine ? "" : "\n";
|
||||
for (var i = 0; i < num; i++) {
|
||||
indent += str;
|
||||
}
|
||||
|
||||
return indent;
|
||||
}
|
||||
|
||||
var indentStr;
|
||||
if (space) {
|
||||
if (typeof space === "string") {
|
||||
indentStr = space;
|
||||
} else if (typeof space === "number" && space >= 0) {
|
||||
indentStr = makeIndent(" ", space, true);
|
||||
} else {
|
||||
// ignore space parameter
|
||||
}
|
||||
}
|
||||
|
||||
// Copied from Crokford's implementation of JSON
|
||||
// See https://github.com/douglascrockford/JSON-js/blob/e39db4b7e6249f04a195e7dd0840e610cc9e941e/json2.js#L195
|
||||
// Begin
|
||||
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
||||
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
||||
meta = { // table of character substitutions
|
||||
'\b': '\\b',
|
||||
'\t': '\\t',
|
||||
'\n': '\\n',
|
||||
'\f': '\\f',
|
||||
'\r': '\\r',
|
||||
'"' : '\\"',
|
||||
'\\': '\\\\'
|
||||
};
|
||||
function escapeString(string) {
|
||||
|
||||
// If the string contains no control characters, no quote characters, and no
|
||||
// backslash characters, then we can safely slap some quotes around it.
|
||||
// Otherwise we must also replace the offending characters with safe escape
|
||||
// sequences.
|
||||
escapable.lastIndex = 0;
|
||||
return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
|
||||
var c = meta[a];
|
||||
return typeof c === 'string' ?
|
||||
c :
|
||||
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
||||
}) + '"' : '"' + string + '"';
|
||||
}
|
||||
// End
|
||||
|
||||
function internalStringify(holder, key, isTopLevel) {
|
||||
var buffer, res;
|
||||
|
||||
// Replace the value, if necessary
|
||||
var obj_part = getReplacedValueOrUndefined(holder, key, isTopLevel);
|
||||
|
||||
if (obj_part && !isDate(obj_part)) {
|
||||
// unbox objects
|
||||
// don't unbox dates, since will turn it into number
|
||||
obj_part = obj_part.valueOf();
|
||||
}
|
||||
switch(typeof obj_part) {
|
||||
case "boolean":
|
||||
return obj_part.toString();
|
||||
|
||||
case "number":
|
||||
if (isNaN(obj_part) || !isFinite(obj_part)) {
|
||||
return "null";
|
||||
}
|
||||
return obj_part.toString();
|
||||
|
||||
case "string":
|
||||
return escapeString(obj_part.toString());
|
||||
|
||||
case "object":
|
||||
if (obj_part === null) {
|
||||
return "null";
|
||||
} else if (isArray(obj_part)) {
|
||||
checkForCircular(obj_part);
|
||||
buffer = "[";
|
||||
objStack.push(obj_part);
|
||||
|
||||
for (var i = 0; i < obj_part.length; i++) {
|
||||
res = internalStringify(obj_part, i, false);
|
||||
buffer += makeIndent(indentStr, objStack.length);
|
||||
if (res === null || typeof res === "undefined") {
|
||||
buffer += "null";
|
||||
} else {
|
||||
buffer += res;
|
||||
}
|
||||
if (i < obj_part.length-1) {
|
||||
buffer += ",";
|
||||
} else if (indentStr) {
|
||||
buffer += "\n";
|
||||
}
|
||||
}
|
||||
objStack.pop();
|
||||
buffer += makeIndent(indentStr, objStack.length, true) + "]";
|
||||
} else {
|
||||
checkForCircular(obj_part);
|
||||
buffer = "{";
|
||||
var nonEmpty = false;
|
||||
objStack.push(obj_part);
|
||||
for (var prop in obj_part) {
|
||||
if (obj_part.hasOwnProperty(prop)) {
|
||||
var value = internalStringify(obj_part, prop, false);
|
||||
isTopLevel = false;
|
||||
if (typeof value !== "undefined" && value !== null) {
|
||||
buffer += makeIndent(indentStr, objStack.length);
|
||||
nonEmpty = true;
|
||||
var key = isWord(prop) ? prop : escapeString(prop);
|
||||
buffer += key + ":" + (indentStr ? ' ' : '') + value + ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
objStack.pop();
|
||||
if (nonEmpty) {
|
||||
buffer = buffer.substring(0, buffer.length-1) + makeIndent(indentStr, objStack.length) + "}";
|
||||
} else {
|
||||
buffer = '{}';
|
||||
}
|
||||
}
|
||||
return buffer;
|
||||
default:
|
||||
// functions and undefined should be ignored
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// special case...when undefined is used inside of
|
||||
// a compound object/array, return null.
|
||||
// but when top-level, return undefined
|
||||
var topLevelHolder = {"":obj};
|
||||
if (obj === undefined) {
|
||||
return getReplacedValueOrUndefined(topLevelHolder, '', true);
|
||||
}
|
||||
return internalStringify(topLevelHolder, '', true);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user