JSFiddle - React, Tailwind, and code Playground
by mayorovp
JavaScript
JSONEX = {
allow_async: false,
functions: {},
undefined_function: function(name) {
return new JSONEX.FunctionCall(name, Array.prototype.slice.call(arguments, 1));
}
};
JSONEX.configure = function(options) {
if ("allow_async" in options)
this.allow_async = options.allow_async;
if ("undefined_function" in options)
this.undefined_function = options.undefined_function;
if ("inherit_functions" in options && !options.inherit_functions)
this.functions = options.functions || {};
else if ("functions" in options)
for (var key in options.functions)
if (options.functions.hasOwnProperty(key))
this.functions[key] = options.functions[key];
};
JSONEX.parser = function(options) {
if (this.hasOwnProperty("functions"))
throw "JSONEX.parser cannot be called as function (without new keyword)";
this.functions = Object.create(this.functions);
this.parser = function() { JSONEX.parser.apply(this, arguments); };
this.parser.prototype = this;
options && this.configure(options);
};
JSONEX.parser.prototype = JSONEX;
JSONEX.parse = function(str, options, ctx) {
ctx = ctx || {};
options = options ? new this.parser(options) : this;
return JSON.parse(str, function (key, value) {
if (key === "?")
if (typeof value === "string") // { "?": "f", named_args }
return [value];
else if (value instanceof Array) { // { "?": ["f", args], named_args }
return value;
} else {
console.warn && console.warn("Malformed JSONEX: '?' property must be string or array");
return ["?", value];
}
if (typeof value === "object" && value && "?" in value) {
var args = value["?"];
if (args[0] == "?") {
args.length == 2 || console.warn && console.warn("Malformed JSONEX: masked '?' property with wrong arguments number...