Skip to content

JSON Parser Configuration

Uses the built-in JSON.parse from Bun or Node.js.

Performance: ⚡⚡⚡ Fastest (baseline)
Error Messages: Standard
Security: Standard
Recommended for: Production use

import { Shokupan } from 'shokupan';
const app = new Shokupan({
jsonParser: 'native' // This is the default
});

Uses the parse-json library for enhanced error messages.

Performance: ⚡⚡⚡ ~5% slower than native (minimal overhead)
Error Messages: ✨ Excellent - shows context and helpful hints
Security: Standard
Recommended for: Development and debugging

Installation:

Terminal window
bun add parse-json

Usage:

import { Shokupan } from 'shokupan';
const app = new Shokupan({
jsonParser: 'parse-json'
});
app.post('/api/data', async (ctx) => {
const body = await ctx.body();
return ctx.json({ received: body });
});

Benefits:

  • Much better error messages when JSON is malformed
  • Shows the position and context of JSON syntax errors
  • Minimal performance impact (~5% slower on Bun, competitive with native on Node.js)

3. secure-json-parse ('secure-json-parse')

Section titled “3. secure-json-parse ('secure-json-parse')”

Uses the secure-json-parse library for protection against prototype pollution attacks.

Performance: ⚡⚡ 20-30% slower than native
Error Messages: Standard
Security: 🔒 Protected against prototype pollution
Recommended for: Parsing untrusted user input

Installation:

Terminal window
bun add secure-json-parse

Usage:

import { Shokupan } from 'shokupan';
const app = new Shokupan({
jsonParser: 'secure-json-parse'
});
app.post('/api/webhook', async (ctx) => {
// Safe from prototype pollution attacks
const body = await ctx.body();
return ctx.json({ processed: true });
});

Benefits:

  • Protects against prototype pollution attacks
  • Safely parses JSON from untrusted sources
  • Prevents __proto__, constructor, and prototype pollution

Trade-offs:

  • 20-30% performance penalty compared to native
  • Only use when parsing untrusted input

Based on benchmarks with 100,000 iterations:

ParserSimple ObjectsNested ObjectsArraysLarge Datasets
native (Bun)14.5M ops/sec4.1M ops/sec79.8K ops/sec9.7K ops/sec
parse-json (Bun)10.4M ops/sec2.8M ops/sec51K ops/sec3.2K ops/sec
secure-json-parse (Bun)8.2M ops/sec2.2M ops/sec44.5K ops/sec2.6K ops/sec

Full benchmark results available in src/test/json-performance-results.md

✅ Use 'native' (default) for best performance

const app = new Shokupan({
jsonParser: 'native' // or omit this line
});

✅ Use 'parse-json' for better error messages with minimal overhead

const app = new Shokupan({
jsonParser: process.env.NODE_ENV === 'development' ? 'parse-json' : 'native'
});

✅ Use 'secure-json-parse' for webhooks or user-generated content

// Main app uses native parser
const app = new Shokupan({
jsonParser: 'native'
});
// Separate router for untrusted webhooks
const webhookRouter = new ShokupanRouter({
jsonParser: 'secure-json-parse'
});
webhookRouter.post('/webhook', async (ctx) => {
const payload = await ctx.body(); // Safely parsed
return ctx.json({ ok: true });
});
app.mount('/external', webhookRouter);
// Standard JSON error
{
"error": "Unexpected token 'i', \"invalid\" is not valid JSON"
}
// Enhanced error with context
{
"error": "Unexpected token i in JSON at position 1 while parsing '{invalid}'\n\n > 1 | {invalid}\n | ^"
}

No code changes needed - just update config:

const app = new Shokupan({
jsonParser: 'parse-json'
});

No code changes needed - just update config:

const app = new Shokupan({
jsonParser: 'secure-json-parse'
});
  • If a parser library is not installed, Shokupan will automatically fall back to native JSON.parse with a warning
  • The parser configuration applies to all JSON body parsing in the application
  • Both parse-json and secure-json-parse are optional peer dependencies
  • Performance impact is most noticeable on high-throughput APIs processing large payloads