https://github.com/coderofsalvation/json-dsl
easily create dsl's from json by evaluating json keys and values, json decode on steroids
https://github.com/coderofsalvation/json-dsl
Last synced: about 1 year ago
JSON representation
easily create dsl's from json by evaluating json keys and values, json decode on steroids
- Host: GitHub
- URL: https://github.com/coderofsalvation/json-dsl
- Owner: coderofsalvation
- License: other
- Created: 2015-10-28T08:36:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-05-28T19:09:20.000Z (about 6 years ago)
- Last Synced: 2025-04-12T20:08:54.272Z (about 1 year ago)
- Language: CoffeeScript
- Size: 4.88 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README

Generate your own minilanguages fast using json as a startingpoint.
No parsetrees, no lexical analyzers, just plain json.
# Usage
$ npm install json-dsl
in your code
jdsl = require('json-dsl').parse
output = jdsl( yourjson )
# basics: xml
By default nested json is converted into xml
{
"div": {
"div": {
"div": "foo"
}
}
}
gets converted into
foo
# customize key evaluation
lets evaluate the keys in a different way:
jsondsl = require('json-dsl')
jsondsl.parseKey = function(k) {
return k + "[%s]";
};
output = jdsl( yourjson )
output:
div[div[div[foo]]]
# customize value evaluation
lets take the previous example and lets add `parseValue`
data = { foo: "bar" }
jsondsl.parseValue = function(v,data) {
return data[v]
}
output = jdsl( yourjson, data )
output:
div[div[div[bar]]]
# example: html template language
> NOTE: the dsl below is a stripped down version of the [brown](https://npmjs.org/packages/brown) template engine, a hyperminimalistic template dsl which borrows from emmet and mustache.
setup dsl:
var jdsl = require('json-dsl');
var zen = require('zen-coding');
jdsl.parseKey = function(k) {
return zen(k + '>{%s}');
};
jdsl.parseValue = function(v, data) {
return data[v];
};
lets test it:
var json = {
'div#foo.flop>fieldset>div>ul': {
'li.one>a[href="/"]': 'one',
'li.two>a[href="/"]': 'two'
}
};
var data = {
'one': 'hello',
'two': 'world'
};
console.log(JSON.stringify(json, null, 2));
console.log(jdsl.parse(json, data));
outputs: