### Importing Jsonic in Node.js and Browser Environments Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html This snippet demonstrates how to import the Jsonic library into your JavaScript project, providing examples for both Node.js environments using `require` and browser environments using `import`. ```JavaScript const { Jsonic } = require('jsonic') ``` ```JavaScript import { Jsonic } from 'jsonic' ``` -------------------------------- ### Parsing Basic Jsonic Syntax Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html This example shows how to use the `Jsonic` function to parse a simple Jsonic string into a JavaScript object. It illustrates the basic usage of the library, taking a string input and returning a parsed JavaScript variable. ```JavaScript let data = Jsonic('a:1') console.log( JSON.stringify(data) ) // prints {"a":1} ``` -------------------------------- ### Create Custom Jsonic Instances Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Explains how to create a new, customizable instance of the Jsonic parser using `Jsonic.make()`. This allows for independent configuration of parsing options without affecting the global Jsonic function. ```JavaScript let myjsonic = Jsonic.make() ``` -------------------------------- ### Initialize Jsonic.js instance Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Initializes a new Jsonic.js instance with default configurations. This is a common starting point for using the library and demonstrates the basic setup. ```JavaScript let jsonic = Jsonic.make({}) ``` -------------------------------- ### Handle Syntax Errors in Jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Shows an example of a syntax error in Jsonic input and how the library provides detailed, developer-friendly error messages. The error output (not included as code) typically includes the location, unexpected characters, and suggestions for debugging. ```JavaScript Jsonic(`{ a: 1 ] }`) ``` -------------------------------- ### Extend Jsonic with Plugins Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Illustrates how to extend Jsonic's functionality by using plugins, specifically demonstrating the CSV plugin. Plugins allow Jsonic to parse different data formats beyond its standard capabilities. ```JavaScript const { Csv } = require('jsonic/plugin/csv') let csv = Jsonic.use(Csv) // === [{"a": 1, "b": 2}, {"a": 3, "b": 4}] csv(` a, b 1, 2 3, 4 `) ``` -------------------------------- ### Define Deep Properties with Jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Demonstrates how Jsonic allows for concise definition of deeply nested properties using a simple colon-separated syntax, similar to the Cue configuration language. This simplifies the creation of complex object structures. ```JavaScript Jsonic('a: b: c: 1') ``` -------------------------------- ### Jsonic Syntax: Supporting Various Comment Styles Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic supports multiple comment styles, including single-line (`//`, `#`) and multi-line (`/* ... */`) comments. This enables developers to properly document their Jsonic configurations and data, enhancing maintainability. ```JavaScript Jsonic(`{ a: 1, // A comment # Also a comment /* * Still a comment */ b: 2, }`) ``` -------------------------------- ### Jsonic Syntax: Implicit Top-Level Objects and Arrays Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic allows for implicit top-level objects and arrays, meaning you don't always need to wrap the entire source in `{}` or `[]`. This is particularly convenient for quickly defining small, simple data structures. ```JavaScript Jsonic('a:1,b:2') ``` ```JavaScript Jsonic('a,b') ``` -------------------------------- ### Jsonic Syntax: Unquoted Object Keys Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html This snippet demonstrates Jsonic's ability to parse object keys without requiring quotes. This makes Jsonic syntax more concise and readable, especially for configuration files or data structures where keys are simple identifiers. ```JavaScript Jsonic('{a:1}') // === {"a":1} ``` -------------------------------- ### Merge Duplicate Properties in Jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Illustrates Jsonic's ability to intelligently merge properties that are defined multiple times within the same structure. This feature is particularly useful for building system specifications by allowing incremental definition of objects. ```JavaScript Jsonic(` a: {b: 1} a: {c: 2} a: d: 3 `) ``` -------------------------------- ### Jsonic Syntax: Single and Double Quoted Strings Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic supports both single and double quotes for string literals, providing flexibility similar to JavaScript. This allows developers to choose their preferred quoting style or mix them as needed. ```JavaScript Jsonic("\"a\"") // === 'a' Jsonic("'a'") // === 'a' ``` -------------------------------- ### Install Jsonic Globally and Use CLI Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/install.html Installs Jsonic globally, making its command-line utility available system-wide. This allows direct execution of the `jsonic` command to convert jsonic arguments into traditional JSON, useful for debugging. ```Shell $ npm install -g jsonic ``` ```Shell $ jsonic a:1 ``` -------------------------------- ### Jsonic Multifile Plugin Syntax Examples Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/multifile.html This section illustrates the extended syntax supported by the `Multifile` plugin, including regular expressions and ISO date formats. These examples show how the plugin interprets specific unquoted text patterns within Jsonic input into corresponding JavaScript `RegExp` and `Date` objects. ```Jsonic /a/ // === new RegExp('a') /\//g // === new RegExp('\\/','g') when: 2021-03-19T17:15:51.845Z // == new Date('2021-03-19T17:15:51.845Z') ``` -------------------------------- ### Disable Comments in Jsonic Instances Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Demonstrates how to disable specific comment styles or all comments for a custom Jsonic instance using the `options()` method. This provides fine-grained control over how comments are parsed. ```JavaScript myjsonic.options({comment:{lex:false}}) myjsonic('a:1,#b:2') // === {"a":1, "#b":2} Jsonic('a:1,#b:2') // === {"a":1} ``` -------------------------------- ### Jsonic Syntax: Unquoted String Values Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html For values that are not numbers, booleans, or null, Jsonic permits unquoted strings. This simplifies the syntax for common string values, making the Jsonic source more concise and less verbose. ```JavaScript Jsonic('{a:foo, b:bar}') ``` -------------------------------- ### Quick Example of Jsonic JSON Plugin Usage Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/json.html This example illustrates how to initialize a Jsonic parser with the `json` plugin enabled. It then shows how to parse a string containing a `NaN` value, demonstrating the plugin's ability to extend Jsonic's default parsing capabilities for special keywords. ```JavaScript let { Json } = require('jsonic/plugin/json') // or import let extra = Jsonic.make().use(Json) extra('a:NaN') // === {"a": NaN} ``` -------------------------------- ### Quick Example of Jsonic Native Plugin Usage Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/native.html This example illustrates how to initialize Jsonic with the `native` plugin and parse a string containing a custom value like `NaN`. The `make()` method is used to create a Jsonic instance, and `use()` registers the plugin. ```JavaScript let { Native } = require('jsonic/plugin/native') // or import let extra = Jsonic.make().use(Native) extra('a:NaN') // === {"a": NaN} ``` -------------------------------- ### Validate Local Jsonic Installation Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/install.html Runs a simple Jsonic command to verify that the locally installed package is working correctly. It demonstrates basic usage and confirms the output is strict JSON. ```JavaScript console.log(Jsonic('a:1')) ``` -------------------------------- ### Disable Specific Comment Markers in Jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Shows how to selectively disable specific comment markers (e.g., `#`) when creating a new Jsonic instance. This allows users to customize which comment styles are recognized. ```JavaScript let nohash = Jsonic.make({comment:{marker:{'#':false}}}) nohash('a:1,#b:2') // === {"a":1, "#b":2} nohash('a:1,//b:2') // === {"a":1} ``` -------------------------------- ### Quick Example of Jsonic Hoover Plugin Usage Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/hoover.html This example illustrates how to quickly integrate and use the `hoover` plugin with Jsonic. It shows the process of making a Jsonic instance, applying the `hoover` plugin, and then parsing a string that includes a `NaN` value, demonstrating the plugin's extended parsing capability. ```JavaScript let { Hoover } = require('jsonic/plugin/hoover') // or import let extra = Jsonic.make().use(Hoover) extra('a:NaN') // === {"a": NaN} ``` -------------------------------- ### Initialize Jsonic with Default Options Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Shows the basic initialization of a Jsonic instance without specific options. This example is used across various option sections (line.lex, line.row, line.sep, comment.lex, comment.balance, comment.marker, space.lex, number.lex) to illustrate the default behavior. ```javascript let jsonic = Jsonic.make({}) ``` -------------------------------- ### Jsonic Syntax: Multi-line Strings with Backticks Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Similar to JavaScript's template literals, Jsonic allows multi-line strings using backticks. This feature is useful for embedding longer text blocks or structured data directly within Jsonic source, improving readability for complex values. ```JavaScript Jsonic(`{ a: 1, b: 2, }`) ``` -------------------------------- ### Jsonic Instance Options API Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Documents the `.options()` method for Jsonic instances, allowing users to get and set configuration options. It specifies the method signature, return type, and parameters, along with availability constraints. ```APIDOC .options(options: object): object _Returns_: object merged object containing the full option tree * `options: object` _required_ : Partial options tree. * 🍒 Only available on: `jsonic = Jsonic.make()` * 🍓 Method: `jsonic.options(...)` * 🍐 Property set of option tree: `jsonic.options.number.lex` ``` -------------------------------- ### Install Jsonic via npm Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/install.html Installs the Jsonic package locally into your project using the Node Package Manager (npm). This is the standard way to add Jsonic as a dependency. ```Shell $ npm install jsonic ``` -------------------------------- ### Integrate Jsonic in Browser Environments Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/install.html Explains two primary methods for using Jsonic in a browser: importing via a module bundler (like webpack or rollup) for modern workflows, or directly including the minified script via a ` ``` -------------------------------- ### Jsonic Syntax: JavaScript-like Numbers and String Escapes Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic provides parity with JavaScript's handling of numbers (decimal, scientific, hexadecimal, octal, binary) and string escapes (hex, unicode, unicode code point). This ensures familiar and consistent parsing behavior for various data types. ```JavaScript Jsonic('20.0, 2e1, 0x14, 0o24, 0b10100') ``` ```JavaScript Jsonic("\"\x61\", \"\u0061\", \"\u{000061}\"") ``` -------------------------------- ### Jsonic Syntax: Block Text with Indent Removal Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic supports block text literals using triple single quotes (`'''`), which automatically remove leading indentation. This feature is ideal for embedding multi-line text blocks while maintaining clean formatting in the source. ```JavaScript Jsonic(` ''' ship! dish! ball! ''' `) ``` -------------------------------- ### Load Jsonic in Node.js (CommonJS) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/install.html Demonstrates the recommended and traditional ways to load the Jsonic module in Node.js using `require` for CommonJS environments. Both destructuring and direct assignment methods are shown, allowing immediate use of the Jsonic function. ```JavaScript const { Jsonic } = require('jsonic') // And then just use it directly! console.log(Jsonic('a:1')) ``` ```JavaScript const Jsonic = require('jsonic') // Nothing broken! console.log(Jsonic('a:1')) ``` -------------------------------- ### Register a Basic Jsonic Plugin Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Shows how to register a simple plugin using the `.use()` method. The example defines a plugin that modifies the token option to use a pipe `|` as a separator, demonstrating how plugins can customize parsing rules. ```javascript let jsonic = Jsonic.make().use(function piper(jsonic) { jsonic.options({token: {'#CA':{c:'|'}}}) }) jsonic('a|b|c') // === ['a', 'b', 'c'] ``` -------------------------------- ### Jsonic Syntax: Handling Spaces in Unquoted Strings Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic automatically trims surrounding spaces from unquoted string values. This ensures that values like 'foo bar' are correctly parsed as a single string, providing intuitive handling of text data. ```JavaScript Jsonic('{a: foo bar }') ``` -------------------------------- ### Using Jsonic Multifile Plugin for Extended Parsing Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/multifile.html This example shows how to initialize Jsonic with the `Multifile` plugin and parse a string containing a special value like `NaN`. It illustrates the plugin's ability to extend standard JSON parsing to recognize and convert non-standard keywords. ```JavaScript let { Multifile } = require('jsonic/plugin/multifile') // or import let extra = Jsonic.make().use(Multifile) extra('a:NaN') // === {"a": NaN} ``` -------------------------------- ### Jsonic Syntax: Newlines as Value Separators Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic allows newlines to act as separators between elements in objects and arrays, reducing the need for explicit commas. This feature can make Jsonic syntax more compact and visually aligned, especially for structured data. ```JavaScript Jsonic(`{ a: 1 b: [ 2 3 ] }`) ``` -------------------------------- ### Parse String and Display Output with Jsonic.js Source: https://github.com/jsonicjs/jsonic/blob/master/browser.html This snippet illustrates how to use the `Jsonic` library to parse a string like 'a:b' into a JavaScript object. The resulting object is then stringified into JSON format using `JSON.stringify()` and displayed within an HTML element with the ID 'out'. This demonstrates a typical way to show library output in a web browser. ```JavaScript Jsonic('a:b') = document.getElementById('out').innerHTML = JSON.stringify(Jsonic('a:b')) ``` -------------------------------- ### Jsonic Syntax: Ignoring Trailing Commas Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/getting-started.html Jsonic allows for trailing commas in arrays and objects, similar to modern JavaScript. This feature improves readability and simplifies code generation by preventing syntax errors when adding or reordering elements. ```JavaScript Jsonic('[ 1, 2, ]') // === [1, 2] Jsonic('{ "a":3, "b":4, }') // === {"a":3, "b":4} ``` -------------------------------- ### Jsonic Native Plugin ISO Date Syntax Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/native.html This example demonstrates how the `native` plugin automatically converts unquoted text matching the ISO date format into a JavaScript `Date` object. This simplifies parsing date strings directly within Jsonic input. ```JavaScript when: 2021-03-19T17:15:51.845Z // == new Date('2021-03-19T17:15:51.845Z') ``` -------------------------------- ### Get and Set Jsonic Instance Options Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Demonstrates how to use the `.options()` method to inspect and modify Jsonic instance configurations. It shows accessing options as a method call or a direct property, and how setting `comment.lex` to `false` changes parsing behavior. ```javascript let jsonic = Jsonic.make() jsonic.options().comment.lex // === true jsonic.options.comment.lex // === true - as a convenience let no_comment = Jsonic.make() no_comment.options({comment: {lex: false}}) // Returns {"a": 1, "#b": 2} no_comment(` a: 1 #b: 2 `) // Whereas this returns only {"a": 1} as # starts a one line comment Jsonic(` a: 1 #b: 2 `) ``` -------------------------------- ### Load Jsonic as ES Module Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/install.html Shows how to import Jsonic as an ES Module in Node.js (version 14+) or browser environments using `import` statements. Both default and named imports are supported for integrating Jsonic into modular applications. ```JavaScript import Jsonic from 'jsonic' console.log(Jsonic('a:1')) ``` ```JavaScript import { Jsonic } from 'jsonic' console.log(Jsonic('a:1')) ``` -------------------------------- ### Import Jsonic and Types in TypeScript Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/install.html Illustrates how to import the Jsonic library in TypeScript, leveraging its built-in type information for better development experience. It also shows how to import specific types (like Plugin, Rule, Context) essential for plugin development. ```TypeScript import Jsonic from 'jsonic' console.log(Jsonic('a:1')) ``` ```TypeScript import { Jsonic } from 'jsonic' console.log(Jsonic('a:1')) ``` ```TypeScript // Some of the exported types (there are more...) import { Jsonic, Plugin, Rule, Context } from 'jsonic' ``` -------------------------------- ### Load Dynamic Plugin for Jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/dynamic.html Demonstrates how to import or require the `dynamic` plugin for Jsonic in both Node.js and web environments, preparing it for use with the Jsonic parser. The convention for loading Jsonic plugins is to deconstruct the module to get the plugin class. ```Node.js let { Dynamic } = require('jsonic/plugin/dynamic') ``` ```Web import { Dynamic } from 'jsonic/plugin/dynamic' ``` -------------------------------- ### Jsonic Comment Syntax (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Defines Jsonic comment syntax. Supports JavaScript-style single-line (`//`) and multi-line (`/* */`) comments. Single-line comments can also start with `#`. Balanced comments can be nested. ```APIDOC comment: Syntax: - Multi-line: /* all-chars ∖ "*/" */ - Single-line: // all-chars ∖ ("//" ∪ «\r\n») - Single-line: # all-chars ∖ ("#" ∪ «\r\n») Notes: - Works as in JavaScript. - Balanced comments can nest. ``` -------------------------------- ### Jsonic Unquoted Keys vs. JSON Quoted Keys Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html Compares key syntax between Jsonic and standard JSON. Jsonic allows unquoted keys for simple identifiers (alphanumeric, starting with letter/underscore), significantly reducing verbosity. Keys containing spaces or punctuation still require quoting in Jsonic, just as all keys do in JSON. ```Jsonic { a: 1, 1: 2, 1a: 3, "1 a": 4, "{}": 5, true: 6 } ``` ```JSON { "a": 1, "1": 2, "1a": 3, "1 a": 4, "{}": 5, "true": 6 } ``` -------------------------------- ### Jsonic Dynamic Plugin Regular Expression Syntax Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/dynamic.html Shows how the `dynamic` plugin interprets unquoted text between forward slashes as JavaScript `RegExp` objects. It includes examples of basic regular expressions and those requiring character escaping. ```APIDOC /a/ // === new RegExp('a') /\//g // === new RegExp('\/','g') ``` -------------------------------- ### Parse NaN with Jsonic Dynamic Plugin Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/dynamic.html Illustrates a quick example of using the `dynamic` plugin to parse a Jsonic string containing the `NaN` keyword. This snippet shows how to initialize Jsonic with the plugin and then parse a string, demonstrating its conversion to a JavaScript `NaN` value. ```JavaScript let { Dynamic } = require('jsonic/plugin/dynamic') // or import let extra = Jsonic.make().use(Dynamic) extra('a:NaN') // === {"a": NaN} ``` -------------------------------- ### Get String Description of Jsonic Instance Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html The `.toString()` method returns the value of the `.id` property, providing a string description of the Jsonic instance. This is useful for logging or debugging. ```APIDOC .toString(): string ``` ```JavaScript ''+Jsonic // === 'Jsonic/1614085850042/022636/-' ''+(Jsonic.make({tag:'foo'})) // === 'Jsonic/1614085851902/375149/foo' ``` -------------------------------- ### Use Jsonic CSV Plugin for Custom Values Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/csv.html Shows a quick example of how to initialize Jsonic with the `Csv` plugin and parse a string containing a custom value like `NaN`. This demonstrates the plugin's ability to extend Jsonic's parsing capabilities beyond standard JSON. ```JavaScript let { Csv } = require('jsonic/plugin/csv') // or import let extra = Jsonic.make().use(Csv) extra('a:NaN') // === {"a": NaN} ``` -------------------------------- ### JavaScript: Define Custom Lex Matcher with `lex()` Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Illustrates how to define a custom lex matcher using `jsonic.lex()`. The example creates a matcher that converts sequences of '%' characters into numeric values (10, 20, 30, etc.) within the top lex state, demonstrating how to extend Jsonic's lexing capabilities. ```JavaScript let tens = Jsonic.make() let VL = tens.token.VL let LTP = tens.token.LTP // Match characters in the top lex state (LTP) tens.lex(LTP, function tens_matcher(state) { // % -> 10, %% -> 20, %%% -> 30, etc. let marks = state.src.substring(state.sI).match(/^%+/ if(marks) { let len = marks[0].length state.token.tin = VL state.token.val = 10 * len // Update lexer position and column return { sI: state.sI + len, cI: state.cI + len } } }) tens('a:1,b:%%,c:[%%%%]') // === {a:1, b:20, c:[40]} ``` -------------------------------- ### Jsonic JSON Plugin ISO Date Conversion Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/json.html This example demonstrates the `json` plugin's capability to automatically convert unquoted text matching the ISO 8601 date format into a JavaScript `Date` object. This feature simplifies the direct parsing of date strings within Jsonic input. ```JavaScript when: 2021-03-19T17:15:51.845Z // == new Date('2021-03-19T17:15:51.845Z') ``` -------------------------------- ### Jsonic JSON Plugin Regular Expression Conversion Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/json.html This section details how the `json` plugin converts string literals enclosed in forward slashes into JavaScript `RegExp` objects. It provides examples for a simple regular expression and one that includes an escaped slash and a global flag, showing the resulting `RegExp` object. ```JavaScript /a/ // === new RegExp('a') /\//g // === new RegExp('\\/','g') ``` -------------------------------- ### JavaScript: Resolve Token Names and IDs with `token()` Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Shows how to use `jsonic.token()` to get a token's numeric ID from its name and vice-versa. It demonstrates accessing predefined token IDs like `ST` (String Token) and converting them to their corresponding names. ```JavaScript let jsonic = Jsonic.make() jsonic.token.ST // === 11, String token identification number jsonic.token(11) // === '#ST', String token name jsonic.token('#ST') // === 11, String token name ``` -------------------------------- ### Use Jsonic CSV Plugin to Parse CSV Data with Embedded JSON Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/index.html This snippet illustrates how to load and use the 'csv' plugin with Jsonic to parse CSV data that can include embedded JSON, comments, and other Jsonic syntax. It shows the 'require' (or 'import') statement and an example of parsing multi-line CSV input with headers. ```JavaScript let Csv = require('jsonic/plugin/csv') // or import let extra = Jsonic.make().use(Csv) // === [{"a":1, "b":2}, {"a":3,"b":4}] extra(` a,b // first line is headers 1,2 3,4 `) ``` -------------------------------- ### JavaScript: Modify and Create Jsonic Rules with `rule()` Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Demonstrates how to use `jsonic.rule()` to retrieve, modify, and create new parser rules. The example shows modifying an existing 'val' rule to concatenate strings instead of forming an array, and creating a new 'hundred' rule for a custom token. ```JavaScript let concat = Jsonic.make() // Get all the rules Object.keys(concat.rule()) // === ['val', 'map', 'list', 'pair', 'elem'] // Get a rule by name let val_rule = jsonic.rule('val') // val_rule.name === 'val' // Modify a rule let ST = concat.token.ST concat.rule('val',(rule)=>{ // Concatentate strings (ST) instead of forming array elements rule.def.open.unshift({s:[ST,ST], h:(alt,rule,ctx)=>{ rule.node = ctx.t0.val + ctx.t1.val // Disable default value handling rule.bc = false }}) }) concat("\"a\" \"b\"") // === 'ab' Jsonic("\"a\" \"b\"") // === ['a', 'b'] // Create a new rule (for a new token) concat.options({ token: { '#HH': {c:'%'} } }) let HH = concat.token.HH concat.rule('hundred', ()=>{ return new RuleSpec({ after_open: (rule)=>{ // % always becomes the value 100 rule.node = 100 } }) }) concat.rule('val', (rulespec)=>{ rulespec.def.open.unshift({s:[HH], p:'hundred'}) }) concat('{x:1, y:%}') // === {x:1, y:100} ``` -------------------------------- ### Jsonic Single-Line Comments Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html Demonstrates single-line comments in Jsonic using '#' or '//' characters, which extend to the end of the line. This feature improves readability and allows for inline annotations within configuration files. ```Jsonic { "a": 1, # a comment "b": 2, // also a comment, } ``` -------------------------------- ### Configure Jsonic.js rule.start property Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Defines the behavior for the `rule.start` option within Jsonic.js. This property expects a value of type `string` and is set to `'val'` by default. Lorem ipsum. ```APIDOC rule.start: Type: string Default: 'val' ``` -------------------------------- ### Importing Jsonic Multifile Plugin Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/multifile.html This snippet demonstrates how to import the `Multifile` plugin for Jsonic in both Node.js and web environments. It shows the common convention of deconstructing the plugin name from the module path, which is the first step to enable extended Jsonic parsing. ```Node.js let { Multifile } = require('jsonic/plugin/multifile') ``` ```Web import { Multifile } from 'jsonic/plugin/multifile' ``` -------------------------------- ### Jsonic Multi-Line Comments Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html Illustrates the use of multi-line comments in Jsonic, enclosed by '/*' and '*/'. This allows for larger blocks of explanatory text or temporary disabling of code sections, similar to C-style comments. ```Jsonic { "a": 1, /* * A Multiline comment */ "b": 2, "foo": foo, } ``` -------------------------------- ### Jsonic Configuration Options Reference Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Detailed reference for various configuration options available in the Jsonic library, including their types and default values. These options control aspects of lexing for lines, comments, spaces, and numbers. ```APIDOC tag: Type: string Default: - Description: Suffix to append to the Jsonic instance identifier. line: Description: Grouping of options for the lexing of lines. line.lex: Type: boolean Default: true line.row: Type: string Default: '\n' line.sep: Type: string (partial RegExp) Default: '\r*\n' comment: comment.lex: Type: boolean Default: true comment.balance: Type: boolean Default: true comment.marker: Type: {[string]: string} Default: { '#': true, '//': true, '/*': '*/' } space: space.lex: Type: boolean Default: true number: number.lex: Type: boolean Default: true ``` -------------------------------- ### Jsonic Syntax Cheatsheet with JSON Equivalents Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html This snippet demonstrates core Jsonic syntax features and their corresponding JSON representations. It covers comments, implicit top-level objects, implicit nulls, optional commas, handling of duplicate keys (last one wins), key chains, and object merging. ```Jsonic // jsonic // cheat... # comments /* * ...sheet! */ # implicit top level a: # implicit null b: 1 # optional comma c: 1 c: 2 # last duplicate wins d: f: 3 # key chains d: g: h: 4 # object merging ``` ```JSON // JSON { "a": null, "b": 1, "c": 2, "d": { "f": 3, "g": { "h": 4 } } } ``` -------------------------------- ### Jsonic Instance Plugin Registration API Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Documents the `.use()` method for registering plugins with a Jsonic instance. It specifies the method signature, return type, parameters (plugin function and optional plugin options), and availability. ```APIDOC .use(plugin: function, plugin_options?: object): Jsonic _Returns_: Jsonic instance (this allows chaining) * `plugin: function` _required_ : Plugin definition function * `(jsonic: Jsonic) => Jsonic` * `plugin_options?: object` _optional_ : Plugin-specific options * 🍒 Only available on: `jsonic = Jsonic.make()` * 🍓 Method: `jsonic.use(...)` ``` -------------------------------- ### Importing the Jsonic Native Plugin Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/native.html This snippet demonstrates how to import the `native` plugin for Jsonic in both Node.js and web browser environments. The plugin is typically deconstructed from its module path. ```JavaScript let { Native } = require('jsonic/plugin/native') ``` ```JavaScript import { Native } from 'jsonic/plugin/native' ``` -------------------------------- ### Importing the Jsonic Hoover Plugin Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/hoover.html This snippet demonstrates how to import the `hoover` plugin module for Jsonic. It shows both the Node.js `require` syntax and the ES module `import` syntax, following the convention for loading Jsonic plugins. ```JavaScript let { Hoover } = require('jsonic/plugin/hoover') ``` ```JavaScript import { Hoover } from 'jsonic/plugin/hoover' ``` -------------------------------- ### Import Jsonic CSV Plugin Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/csv.html Demonstrates how to import the `csv` plugin for Jsonic in both Node.js (using `require`) and web environments (using `import`). It also notes the convention for deconstructing plugin modules when loading Jsonic plugins. ```JavaScript // Node.js let { Csv } = require('jsonic/plugin/csv') // Web import { Csv } from 'jsonic/plugin/csv' ``` -------------------------------- ### Chain Multiple Jsonic Plugins Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Demonstrates the chaining capability of the `.use()` method, allowing multiple plugins to be registered sequentially on a single Jsonic instance. It shows how plugins can add new methods to the Jsonic instance, which can then interact with each other. ```javascript function foo(jsonic) { jsonic.foo = function() { return 1 } } function bar(jsonic) { jsonic.bar = function() { return this.foo() * 2 } } let jsonic = Jsonic.make() .use(foo) .use(bar) // jsonic.foo() === 1 // jsonic.bar() === 2 ``` -------------------------------- ### Support Single and Double Quoted Strings in jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html jsonic handles strings using both single and double quotes, similar to JavaScript, allowing for flexible string literal definitions and proper escaping. ```jsonic "a" 'b' "c'c" 'd"d' 'e\te' ``` ```JSON "a" "b" "c'c" "d\"d" "e\te" ``` -------------------------------- ### Importing the Jsonic JSON Plugin Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/json.html This snippet demonstrates how to import the `json` plugin for Jsonic in both Node.js and web environments. It uses `require` for Node.js and `import` for web, following the convention of deconstructing the plugin name from the module path. ```JavaScript // Node.js let { Json } = require('jsonic/plugin/json') // Web import { Json } from 'jsonic/plugin/json' ``` -------------------------------- ### Create Customizable Jsonic Instance Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html The `.make()` method creates and returns a new Jsonic instance that can be modified. This new instance can be used for parsing and exposes the full API for customization, unlike the top-level `Jsonic` function. ```APIDOC .make(options?: object): Jsonic options?: object - Partial options tree. ``` -------------------------------- ### Use Spaces, Tabs, Newlines as Separators in jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html In jsonic, elements can be separated by spaces, tabs, newlines, or nothing, eliminating the need for commas in many cases. This provides greater flexibility compared to JSON's strict comma-separated syntax. ```jsonic { a: 1 1: 2 b: [3 4] c: [[5][6]] } ``` ```JSON { "a": 1, "1": 2, "b": [3, 4], "c": [[5], [6]] } ``` -------------------------------- ### Jsonic Hoover Plugin Extended Syntax Reference Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/hoover.html This section details the extended syntax elements introduced by the `hoover` plugin. It covers new value keywords (`undefined`, `NaN`, `Infinity`), the recognition of regular expression literals, and the automatic conversion of unquoted ISO date strings into `Date` objects. ```APIDOC Hoover value keywords: - undefined - NaN - Infinity (optional prefix + or -) Regular Expressions: Characters between '/' and '/' are converted to a RegExp object. - /a/ // === new RegExp('a') - /\\//g // === new RegExp('\\/','g') ISO Dates: Unquoted text that matches the ISO date format is converted into a Date object. - when: 2021-03-19T17:15:51.845Z // == new Date('2021-03-19T17:15:51.845Z') ``` -------------------------------- ### Configure Jsonic Instance Tag Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Demonstrates how to set a custom tag for a Jsonic instance using the `tag` option. This tag is appended to the instance identifier, aiding in debugging multiple instances. ```javascript let jsonic = Jsonic.make({tag:'foo'}) jsonic.id // === 'Jsonic/1614085851902/375149/foo' ``` -------------------------------- ### Create Indented Block Strings with Triple Quotes in jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html jsonic offers a unique triple-quote syntax for defining indented blocks of text, automatically handling newlines and indentation to produce a clean multi-line string in JSON. ```jsonic ''' red green blue ''' ``` ```JSON "red\ngreen\nblue" ``` -------------------------------- ### Use Jsonic Native Plugin to Parse JavaScript Values Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/index.html This snippet demonstrates how to load and use the 'native' plugin with Jsonic to parse native JavaScript values like 'NaN'. It shows the 'require' (or 'import') statement and how to apply the plugin to a Jsonic instance, illustrating its effect on parsed output. ```JavaScript let Native = require('jsonic/plugin/native') // or import let extra = Jsonic.make().use(Native) extra('a:NaN') // === {"a": NaN} ``` -------------------------------- ### Jsonic CSV Plugin Syntax Extensions Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/csv.html Illustrates how the Jsonic `csv` plugin extends the standard syntax to recognize and parse regular expressions and ISO date strings into their corresponding JavaScript object types. ```Jsonic /a/ ``` ```JavaScript new RegExp('a') ``` ```Jsonic /\//g ``` ```JavaScript new RegExp('\\/','g') ``` ```Jsonic when: 2021-03-19T17:15:51.845Z ``` ```JavaScript new Date('2021-03-19T17:15:51.845Z') ``` -------------------------------- ### Jsonic Dynamic Plugin ISO Date Syntax Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/dynamic.html Explains how the `dynamic` plugin automatically converts unquoted text matching the ISO date format into JavaScript `Date` objects. This allows for direct parsing of date strings into native Date objects within Jsonic. ```APIDOC when: 2021-03-19T17:15:51.845Z // == new Date('2021-03-19T17:15:51.845Z') ``` -------------------------------- ### Use Underscores for Number Readability in jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html jsonic allows underscores within number literals to improve readability for large numbers, a feature common in many programming languages but not supported by standard JSON. ```jsonic 2_000_000 ``` ```JSON 2000000 ``` -------------------------------- ### Inherit Configuration in Jsonic Child Instances Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/api.html Illustrates how child Jsonic instances created with `make()` inherit configurations from their parent instances. It shows customizing a child instance with a new token separator while retaining the parent's number parsing setting. ```javascript let no_numbers_please = Jsonic.make({number: {lex: false}}) no_numbers_please('1,2,3') // === ['1', '2', '3'] as before let pipe_separated = no_numbers_please.make({token: {'#CA':{c:'|'}}}) pipe_separated('1|2|3') // === ['1', '2', '3'], but: pipe_separated('1,2,3') // === '1,2,3' !!! ``` -------------------------------- ### Define Multi-line Strings with Backticks in jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html jsonic introduces backtick-quoted strings, enabling multi-line string literals where newlines are preserved, a feature not natively available in standard JSON. ```jsonic `a b` ``` ```JSON "a\nb" ``` -------------------------------- ### Support JavaScript Number Formats in jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html jsonic extends number parsing to include various JavaScript number formats such as decimal, hexadecimal (0x), octal (0o), and binary (0b), which are not standard in JSON. ```jsonic 20 20.0 2e1 0x14 0o24 0b10100 ``` ```JSON 20 20 20 20 20 20 ``` -------------------------------- ### Configure Jsonic.js value.src property (object) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Defines the behavior for the `value.src` option within Jsonic.js. This property expects a value of type `{[string]: string}` and is set to `{ 'null': null, 'true': true, 'false': false, }` by default. Lorem ipsum. ```APIDOC value.src: Type: {[string]: string} Default: { 'null': null, 'true': true, 'false': false, } ``` -------------------------------- ### Configure Jsonic.js number.bin property Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Defines the behavior for the `number.bin` option within Jsonic.js. This property expects a value of type `boolean` and is set to `true` by default. Lorem ipsum. ```APIDOC number.bin: Type: boolean Default: true ``` -------------------------------- ### Jsonic Map Syntax (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Defines the syntax for Jsonic maps. Keys can precede a value, creating nested maps. Missing values default to `null`. Spurious commas are not allowed. Key-value pairs can be separated by space and newlines. ```APIDOC map: Syntax: { key : value sep } Notes: - One or more keys may precede a value. - Creates child maps as needed. - Missing values are null. - Spurious commas are not allowed. - Key-value pairs separated by space and new lines. ``` -------------------------------- ### Configure Jsonic.js debug.get_console property Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Defines the behavior for the `debug.get_console` option within Jsonic.js. This property expects a value of type `() => console` and is set to `() => console` by default. Lorem ipsum. ```APIDOC debug.get_console: Type: () => console Default: () => console ``` -------------------------------- ### Skip Top-Level Braces and Brackets in jsonic Source: https://github.com/jsonicjs/jsonic/blob/master/docs/guide/syntax-introduction.html jsonic allows omitting the outermost braces for objects and square brackets for arrays, inferring the structure from the content. This provides a more concise syntax for simple top-level structures. ```jsonic a:1,b:2 1,2 ``` ```JSON ``` ```text ``` -------------------------------- ### Configure Jsonic.js block.marker property Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Defines the behavior for the `block.marker` option within Jsonic.js. This property expects a value of type `{[string]: string}` and is set to `{ '\'\'\': '\'\'' }` by default. Lorem ipsum. ```APIDOC block.marker: Type: {[string]: string} Default: { '\'\'\': '\'\'' } ``` -------------------------------- ### Jsonic Key Syntax (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Defines the syntax for Jsonic keys. Keys are verbatim source characters, including unquoted text, number, and literal value representations, but excluding punctuation. ```APIDOC key: Syntax: space string | number | true | false | null space Notes: - Verbatim source characters. - Includes unquoted text, number, literal value representations. - Excludes punctuation ({}[],...). ``` -------------------------------- ### Jsonic List Syntax (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Defines the syntax for Jsonic lists. Values are separated by space, commas, and newlines. A trailing comma is ignored. A comma without a preceding value inserts an implicit `null`. ```APIDOC list: Syntax: [ value sep ] Notes: - Values separated by space, commas, new lines. - Trailing comma is ignored. - Comma without preceding value inserts implicit null. ``` -------------------------------- ### Jsonic Top-Level Structure (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Describes the possible top-level return types after parsing a Jsonic document: an object (map), an array (list), or a scalar value. An empty document returns `undefined`. Highlights special handling for implicit maps and lists at the top level. ```APIDOC jsonic: Returns: map | list | value | undefined Notes: - Empty document returns undefined. - Implicit maps (no braces) and lists (no brackets) are special at top level. ``` -------------------------------- ### Jsonic Space Token Syntax (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Defines the Jsonic space token. It must contain at least one space character. Can include comments, linefeed, carriage return, and horizontal tab. ```APIDOC space: Syntax: «0x20» space «0xA» linefeed «0xD» carriage return «0x9» horizontal tab comment Notes: - Must have at least one space character. - Can contain one or more comments. ``` -------------------------------- ### Jsonic Separator Syntax (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Defines the valid separators in Jsonic: commas, spaces, new lines, or nothing. All separate values equivalently. ```APIDOC sep: Syntax: , | space | (newline) | (nothing) Notes: - All separate values equivalently. ``` -------------------------------- ### Jsonic Native Plugin Regular Expression Syntax Source: https://github.com/jsonicjs/jsonic/blob/master/docs/plugin/native.html This section explains how the `native` plugin processes regular expression literals. Text enclosed in forward slashes (/) is automatically converted into a JavaScript `RegExp` object, supporting flags and escaped characters. ```JavaScript /a/ // === new RegExp('a') ``` ```JavaScript /\//g // === new RegExp('\/','g') ``` -------------------------------- ### Jsonic Number Syntax (APIDOC) Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/syntax.html Defines Jsonic number literals. Works like JavaScript numbers, with support for underscores to separate digits for readability. ```APIDOC number: Syntax: - Decimal: 0 | - | + | 0 digits [1-9][0-9]* ∪ «_» . digits [0-9]* ∪ «_» e E digits [0-9]* ∪ «_» - Hexadecimal: 0 x digits a-fA-F ∪ «_» - Octal: o digits 0-7 ∪ «_» - Binary: b digits 0-1 ∪ «_» Notes: - Underscore can be used to separate digits. ``` -------------------------------- ### Configure Jsonic.js string.multiline property Source: https://github.com/jsonicjs/jsonic/blob/master/docs/ref/options.html Defines the behavior for the `string.multiline` option within Jsonic.js. This property expects a value of type `string` and is set to `` ` `` by default. Lorem ipsum. ```APIDOC string.multiline: Type: string Default: ` ```