Skip to content

Extended JSON ​

JSON has no ObjectId, no date, no decimal and no 64-bit integer. The server writes documents as relaxed MongoDB Extended JSON, so these types survive the trip to the client and back:

jsonc
{
  "_id": { "$oid": "507f1f77bcf86cd799439011" },
  "n": 5,
  "big": { "$numberLong": "9007199254740993" },
  "price": { "$numberDecimal": "10.25" },
  "when": { "$date": "2024-06-15T12:00:00Z" },
  "note": { "$string": "2024-06-15T12:00:00Z" }
}
  • Integers beyond 2^53 are $numberLong, because a JavaScript client would round them. Other numbers stay plain.
  • A text that looks like an ISO date is wrapped in $string. A plain ISO instant in a request body is stored as a date, and the wrapper keeps a text a text when it is sent back.

Helpers ​

The module auto-imports three helpers (in components and in server code):

HelperPurpose
documentId(docOrId)The id as text: the hex string of an ObjectId, the value of any other id.
fromExtendedJson(doc)Typed values: ObjectId, Date, Decimal128, …, and bigint for integers beyond 2^53.
toExtendedJson(doc)Typed values back to Extended JSON for an insert or update body. Texts that look like dates stay texts.
ts
const { documentApi } = useMongocampApi()

const doc = await documentApi.getDocument({ collectionName: 'users', documentId: id })
documentId(doc) // '507f1f77bcf86cd799439011'

const user = fromExtendedJson<{ name: string, seen: Date }>(doc)
user.seen.getFullYear()

await documentApi.update({
  collectionName: 'users',
  documentId: documentId(doc),
  requestBody: toExtendedJson({ ...user, seen: new Date() }),
})

Choosing the format ​

Every route that returns documents takes extendedJson:

ValueOutput
(not sent) / relaxedRelaxed Extended JSON (default)
canonicalEvery number wrapped too ($numberInt, $numberDouble), so number types survive a GET → PUT exactly
plainids and dates as strings, numbers as numbers
ts
await documentApi.listDocuments({ collectionName: 'users', extendedJson: 'plain' })

Use plain for code that expects plain JSON (see Migration to 2.0 for code written against 1.x). See Documents & Types for every wrapper the server accepts in a request body.

Released under the MIT License.