jsonvar
v1.0.4
Published
JSONV, or Variable JSON, is a specialized JSON-like format that allows for the inclusion of variables within JSON objects, making it ideal for dynamic data handling and configuration.
Downloads
23
Readme
JSONV Module
The jsonv module is a TypeScript class that allows you to load and parse Variable JSON (JSONV) objects, which are JSON objects containing variables. This module enables you to inject variables into the loaded jsonv file at runtime.
Table of Contents
Installation
To use the JSONV module in your TypeScript project, you can install it via npm:
npm install jsonv
Usage
To use the jsonv module in your TypeScript project, follow these steps:
Install the jsonv module via npm:
npm install jsonv
Import the jsonv class if you are parsing strings directly, otherwise import the load method to parse jsonv files
// import for loading strings which contain variables in json format import { JSONV } from "jsonv";
// import 'load' method for importing directly from a .jsonv file import { load } from "jsonv";
Load the jsonv file
To load the file directly, we can use the load method from the jsonvar module. The object returned from the load method is an instance of the class JSONV that contains the validated stub object and the mapping from the variable name to the path reference within the stub object.
In case, the variable JSON also has to be loaded from a string, a new instance of the JSONV class can be created directly by calling the constructor of the class which accepts a string as an argument.
import { load, JSONV } from "jsonvar"; // load from a file const variableJSONLoadedFromFile: JSONV = load( "./cancelled-orders-for-users.jsonv" ); // create from a string const variableJSONFromAString: JSONV = new JSONV('{"active":#active}');
Injecting variables on runtime
To inject variables into the stub object, use the inject method which takes an object as an argument.
import { load } from "jsonvar"; // load from a file const variableJSON: JSONV = load("./cancelled-orders-for-users.jsonv");