Description
We are using jsonata with our json api payloads and it's been working well except we do something unusual - we convert the values of our json payload keys to object wrappers i.e. String, Number, Boolean.
The reason is that we attach some meta data as (non-enumerable) properties of these (object wrapped) values in order to access the meta data after jsonata has transformed the json.
So (for one example of our "metadata") our users can assign user friendly names to the json properties that they want to appear as headings in a table, but they define these against the original payloads in the form the apis return them.
(along with other metadata) we attach these "friendly names" to the (object-wrapped) values of the json payloads, then run our jsonata expression, followed by walking what jsonata returns to get back the metadata from the now transformed payload.
This works well in general except the function signature validation that jsonata does assumes primitive values only.
e.g. I was using $toMillis in my jsonata expression but it was throwing an exception since it's first argument was a String object not a string primitive because of our "object wrappers".
stepping into jsonata it looked like I could pretty easily avoid the issue with a small mod to the function "getSymbol" to recognize String/Number/Boolean as equivalent to the corresponding primitives.
Unfortunately I can't see any way to monkey patch my change into the jsonata code, but otoh I'm not sure you guys would want my change in jsonata proper (I recognize what we're doing is unusual we still can't decide if it's genius or insanity :).
I'll paste my little mod below so u can see - would u guys be open to a PR with such a change?
If not would u be open to a change that would introduce some way for me to monkey patch the getSymbol function so I could introduce my change without having to copy jsonata (which I don't want to do if at all possible).
Here was my little test change it's identical to the real getSymbol function except for the three ifs you see checking for String/Number/Boolean:
var getSymbol = function (value) {
var symbol;
if (utils.isFunction(value)) {
symbol = 'f';
} else {
var type = typeof value;
if (type === 'object' && value instanceof String) {
type = 'string';
}
if (type === 'object' && value instanceof Number) {
type = 'number';
}
if (type === 'object' && value instanceof Boolean) {
type = 'boolean';
}
switch (type) {
case 'string':
symbol = 's';
break;
case 'number':
symbol = 'n';
break;
case 'boolean':
symbol = 'b';
break;
case 'object':
if (value === null) {
symbol = 'l';
} else if (Array.isArray(value)) {
symbol = 'a';
} else {
symbol = 'o';
}
break;
case 'undefined':
default:
// any value can be undefined, but should be allowed to match
symbol = 'm'; // m for missing
}
}
return symbol;
};
sorry for being wordy (wanted to give the context for why this unusual request)