JSON with Property Comments — A lightweight JSON variant that allows single-line comments //, trailing commas, and extended literals (NaN, Infinity, ISO dates, BigInt).
jsonpc follows these rules for comment:
- Trailing commas in arrays and objects are allowed
- Only single-line comments starting with
//are supported - Comments must occupy an entire line
- Multiple consecutive comment lines are allowed
- Valid comment positions:
- Top of the file (before any JSON content)
- Bottom of the file (after all JSON content)
- Above property names
Other positions and block comments are not allowed.
On top of standard JSON values, jsonpc also accepts four extended literals:
NaN, Infinity (+Infinity / -Infinity), bare ISO dates and BigInt values —
see Extended Values.
The full grammar is specified in JSONPC_SPEC.md.
import { parse } from 'jsonpc-ts';
// Parse jsonpc text into an operatable instance
const jsonpc = parse(text);
// Get both value and comments for a property path
const entry = jsonpc.get('profile');
// → { value: { age: 25 }, comments: ['Nested object comment'] }
// Get value only
const name = jsonpc.get('name')?.value; // → "Alice"
const age = jsonpc.get('profile.age')?.value; // → 25
const age2 = jsonpc.get(['profile','age'])?.value; // → 25
// Get comments only
const comments = jsonpc.get('name')?.comments;
// → ['This is a name comment']
// Handle non-existent paths
const entry = jsonpc.get('nonexistent');
// → undefined
jsonpc.set('profile', {
value: { age: 26 },
comments: ['Updated profile comment']
});
// Sets 'profile' to be an object with age 26 and updates its comments
// Set top-level comments
jsonpc.top = ['New top comment'];
// Set bottom-level comments
jsonpc.bottom = ['New bottom comment'];Comment strings are stored without the
//prefix. They are prefixed automatically when serialized.
import { parse } from 'jsonpc-ts';
const jsonpc = parse(text);
// Get top-level file comments
const topComments = jsonpc.top;
// → ['This is a top comment', 'Another top comment']
// Get bottom-level file comments
const bottomComments = jsonpc.bottom;
// → ['This is a bottom comment']
// Get/Set top-level comments
jsonpc.top = ['New top comment'];
// Get/Set bottom-level comments
jsonpc.bottom = ['New bottom comment'];
// Release internal references and clear internal containers
jsonpc.destroy();// Serialize back to JSON text with comments
const output = jsonpc.stringify();
// Custom indentation and replacer
const custom = jsonpc.stringify(null, 4);
// Get clean JSON object without comments
const clean = jsonpc.toObject();
// → { name: "Alice", profile: { age: 25 }, items: [1, 2] }Standard JSON cannot represent NaN, Infinity, Date or BigInt. jsonpc lets you
write them as bare literals, anywhere a value is allowed (object property
values and array elements):
{
// Not a number
"nan": NaN,
// Infinity, with an optional sign
"max": Infinity,
"min": -Infinity,
// Date, written exactly like Date#toISOString()
"createdAt": 2024-01-02T03:04:05.678Z,
// BigInt, with the `n` suffix
"id": 9007199254740993n,
"offsets": [NaN, -7n],
}| Literal | Resulting JS value |
|---|---|
NaN |
NaN |
Infinity / +Infinity |
Infinity |
-Infinity |
-Infinity |
YYYY-MM-DDTHH:mm:ss.sssZ |
Date |
-?<digits>n |
BigInt |
Parsing, serialization and toObject() all preserve these values:
import { parse } from 'jsonpc-ts';
const jsonpc = parse(text);
jsonpc.get('nan')?.value; // → NaN
jsonpc.get('createdAt')?.value; // → Date instance
jsonpc.get('id')?.value; // → 9007199254740993n
jsonpc.toObject();
// → { nan: NaN, createdAt: Date, id: 9007199254740993n, ... }// Values set programmatically are written back as bare literals
jsonpc.set('nan', { value: NaN });
jsonpc.set('createdAt', { value: new Date('2024-01-02T03:04:05.678Z') });
jsonpc.set('id', { value: 42n });
jsonpc.stringify();
// {
// "nan": NaN,
// "createdAt": 2024-01-02T03:04:05.678Z,
// "id": 42n,
// }Notes:
- Number literals are case- and spelling-sensitive:
NaNandInfinity/+Infinity/-Infinityonly.inf,INFandundefinedare not supported. - Dates are recognized only in exact
Date#toISOString()form: 3-digit milliseconds and a trailingZ. Timezone offsets and 6/9-digit microseconds are rejected. Extended years (+010000-…,-000001-…) are accepted. - Invalid dates are rejected at parse time, including calendar-invalid ones
such as
2024-02-31T00:00:00.000Z(which JS would silently roll over) and2024-13-01T00:00:00.000Z. - BigInt may be negative (
-42n). Integers beyond ±2^53 are only exact when written with thensuffix; without it, precision follows the host language's number type. - Quoted look-alikes are never converted:
"NaN","Infinity","42n"and"2024-01-02T03:04:05.678Z"remain plain strings. - A custom
reviverpassed toparse()receives already-convertedNaN/Infinity/Date/BigIntvalues. - Files using extended literals are not valid standard JSON — other
parsers will reject them.
toObject()and quoted strings are the interop-friendly alternatives.
| Solution | Custom Parser | Arbitrary Position Comments | Trailing Commas | NaN / Infinity / Date / BigInt | Size |
|---|---|---|---|---|---|
| jsonpc | ❌ | ❌ | ✅ | ✅ | Small |
| json5 | ✅ | ✅ | ✅ | Partial (NaN, Infinity) |
Large |
| JSONC | ✅ | ✅ | ❌ | ❌ | Medium |
jsonpc trades some flexibility for simplicity and performance by:
- Only allowing comments at specific, predictable positions (above properties)
- Using standard JSON parsing with comment pre-processing
- Maintaining a lightweight codebase
Issues and Pull Requests are welcome!
