@programmerraj/jsonf
v1.0.0
Published
Json with functions.
Downloads
3
Readme
jsonf
Json with functions.
Why
Json is good because of it's simplicity, but it can't do logical things. By being able to call functions, you can do many things with json.
Let's say we want to create an array of 100 numbers from 0 to 99.
[
0,
1,
2,
"That's a lot of numbers"
]
We could store the data in this long array, but there are a few problems.
- It takes up a lot of space. This can make it hard to read.
- It's hard to change. If you change your mind and only want to have 50 numbers, or want to add 100 more numbers, then you'll have to do a lot of deleting / adding.
The solution: jsonf.
{
"$jsonf": ["jsFn", 100]
}
Your javascript function could look like this:
const jsFn = numberOfNumbers => {
const arr = []
for (let i = 0; i < numberOfNumbers; i++) {
arr.push(i)
}
return arr
}
Now the json is very easy to read, and you can easily change the number of numbers by changing the 100
to something else, like 50
or 200
.
Syntax
'Calling' a function.
You can 'call' a function by having an object with a $jsonf
key. The value is an array, where the first element is the name of the function, and the following elements are the parameters given to that function.
{
"$jsonf": [
"nameOfMyFn",
"param 1",
{
"object": "is param 2",
"params": "Can be any json value"
}
]
}
This will transform into whatever nameOfMyFn
returned.
"Whatever `nameOfMyFn` returned"
Security
Can random js code be executed?
No. Inside the json, functions can only be called. In order for a function to be callable, they must be passed to the jsonf
function.
Install
@programmerraj/transform-json
is a peerDependency
.
npm i @programmerraj/transform-json @programmerraj/jsonf
Usage
More information about transform-json
ES
import jsonf from 'jsonf'
CommonJS
const jsonf = require('jsonf').default
Transforming Json
jsonf
accepts an object where the keys are the names of the functions and the values are the functions.
transform({
$jsonf: ['myFn', 5, 4]
}, [jsonf({
myFn: (n1, n2) => n1 * n2
})])`
Transforms into 20
.
Using with jsonv
Make sure to put the jsonv
visitor before the jsonf
visitor.
transform({
$jsonv: {
n: 50
},
key: {
$jsonf: ['add', { $jsonv: 'n' }, 2]
}
}, [
jsonv(),
jsonf({ add: (a, b) => a + b })
])
Transforms to
{
"key": 52
}
In the example above, the variables are getting converted first, which means that add(50, 2)
is being called. If we didn't have the jsonv
before the jsonf
visitor, then add(50, { $jsonv: 'n' })
would be called and add
would try to add a number and an object.