@ranchonyx/mjson
v1.0.8
Published
A simple system to resolve JSON-references at runtime.
Downloads
14
Readme
mJSON - Modular JSON
Brief
mJSON resolves @import(...)-statements in JSON plaintext or files.
In essence, mJSON allows splitting of large JSON files into several smaller ones which are "assembled" at runtime.
Features
- Runtime resolution of
@import(...)-statements - Resolution of local path imports and URL imports
- Asynchronous and synchronous APIs
- Note: URL imports are only available in the asynchronous API, the synchronous API will throw.
- Cyclic import detection
- Note: When a cyclic import is detected, the API will throw.
Note
When an imported file is being "parsed", it will be treated as mJSON automatically, meaning if it has
any @import(...)-statements, they will be resolved.
This results in a practically "infinite" import depth.
Basic usage example (local file import)
The @import(...)-statement in below JSON will be resolved to the parsed JSON contents of the imported file.
Before resolution:
{
"integer": 1,
"float": 3.1415,
"string": "hello there",
"object": "@import(dist/test/importable-object.mjson)"
}Executing the resolver:
import {Async} from "@ranchonyx/mjson"
const filePath = "./aboveFile.mjson"
const resolved = await Async.parseFile(filePath);
console.log(resolved);After resolution:
{
"integer": 1,
"float": 3.1415,
"string": "hello there",
"object": {
"name": "Jane Doe",
"age": 29
}
}Side note
If you wish to import a JSON config from a URL / online resource, simply import the URL using @import(https://something.somedomain.com/config).
The resulting text content will be parsed as JSON and then interpreted as mJSON.
Testing
The tests are written using mocha and chai.
Install the devDependencies and run npm test to test the package.
Addendum
I do not see much of any practical purpose for this package, honestly.
I wrote it because I wanted to see if I can write something like this.
If it's any use to you, I'd be glad to hear that.
