Skip to content

This plugin enable your server to process JSON5 payloads

License

Notifications You must be signed in to change notification settings

Eomm/fastify-json5

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

fastify-json5

ci npm JavaScript Style Guide

This plugin enable your server to process JSON5 payloads.
It adds a new application/json5 content type parser to Fastify and decorate the reply object with a sendJSON5() utility.

Install

npm install fastify-json5

Compatibility

Plugin version Fastify version
^2.0.0 ^5.0.0
^1.0.0 ^4.0.0

Usage

const fastify = require('fastify')
const fastifyJson5 = require('fastify-json5')

const app = fastify()
app.register(fastifyJson5, { 
  reviver: (key, value) => value // optionally pass a reviver function
})

app.post('/', (req, reply) => {
  console.log(req.body)
  reply.sendJSON5(req.body, {
    replacer, // optionally
    space,    // optionally
    quote     // optionally
  })
})

app.inject({
  method: 'POST',
  url: '/',
  headers: { 'content-type': 'application/json5' },
  payload: `{
    // comments
    unquoted: 'and you can quote me on that',
    singleQuotes: 'I can use "double quotes" here',
    lineBreaks: "Look, Mom! \
  No \\n's!",
    hexadecimal: 0xdecaf,
    leadingDecimalPoint: .8675309, andTrailing: 8675309.,
    positiveSign: +1,
    negativeSign: -9,
    trailingComma: 'in objects', andIn: ['arrays',],
    "backwardsCompatible": "with JSON",
  }`
})

Options

This plugin uses the json5 under the hood. So you can provide the same options of the json5 module.

When you register the plugin you can pass the JSON.parse options:

  • reviver

When you call the reply.sendJSON5() you can pass the JSON.stringify:

  • replacer
  • space
  • quote

Security

By default, the json5 module behaves like the JSON.parse and JSON.stringify methods. So it parse and add to the prototype the __proto__ property.

This module implements a security check to avoid the __proto__ property to be added to the prototype. In this case the plugin will throw an http 400 error.

License

Copyright Manuel Spigolon, Licensed under MIT.

About

This plugin enable your server to process JSON5 payloads

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Contributors 2

  •  
  •