fastify-json5
v2.0.0
Published
This plugin enable your server to process JSON5 payloads
Downloads
29
Maintainers
Readme
fastify-json5
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.