Skip to content

harehare/json5.mq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json5.mq

A JSON5 parser implemented as an mq module.

Features

  • Single-line comments (//) and block comments (/* */)
  • Single-quoted strings
  • Unquoted identifiers as object keys
  • Trailing commas in objects and arrays
  • Hexadecimal numbers (0xFF)
  • Infinity, -Infinity, NaN
  • Unicode and hex escape sequences in strings (\uXXXX, \xXX)

Installation

Copy json5.mq to your mq module directory, or place it anywhere and reference it with -L.

cp json5.mq ~/.local/mq/config/

Usage

mq -L /path/to/modules -I raw \
  'import "json5" | json5::json5_parse(.)' input.json5

If you copied it to the mq built-in module directory:

mq -I raw 'import "json5" | json5::json5_parse(.)' input.json5

API

json5_parse(input)

Parses a JSON5 string and returns the corresponding mq value.

Input type Output
String Parsed JSON5 value (object, array, string, number, bool, or None)

Raises an error if the input is not valid JSON5.

Example

Given config.json5:

{
  // Server configuration
  host: 'localhost',
  port: 8080,
  debug: true,
  tags: ['web', 'api',],  // trailing comma is fine
  limits: {
    maxRetries: 0xFF,     // hex number
    timeout: 30.5e3,
  },
}
mq -L . -I raw 'import "json5" | json5::json5_parse(.) | ."host"' config.json5
# => "localhost"

mq -L . -I raw 'import "json5" | json5::json5_parse(.) | ."limits"."maxRetries"' config.json5
# => 255

Compatibility

Requires mq v0.5 or later.

License

MIT

About

A JSON5 parser implemented as an mq module

Topics

Resources

License

Stars

Watchers

Forks

Contributors