Skip to content

Commit

Permalink
feat: add safeNumbers option
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoniangreen committed Aug 17, 2024
1 parent d7b6e92 commit 1f6f26c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
8 changes: 8 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ Defines how date-time strings are parsed and validated. By default Ajv only allo
This option makes JTD validation and parsing more permissive and non-standard. The date strings without time part will be accepted by Ajv, but will be rejected by other JTD validators.
:::

### safeNumbers <Badge text="JTD only" />

Defines how special case numbers, Infinity, -Infinity and NaN are handled. Use `safeNumbers: "null"` to serialize them to `null` which is correct behavior according to the JSON spec. If you wish to handle these values, however, and not lose the data you can use `safeNumbers: "string"` which will serialize them to strings. If this option is not set the values will be included as the original literal values.

::: warning The default behavior can produce invalid JSON
If `safeNumbers` is left undefined, the serializer will produce invalid JSON when there are any special case numbers in the data. This is, however, the fastest mode and so should be used unless you expect to encounter special case numbers.
:::

### int32range <Badge text="JTD only" />

Can be used to disable range checking for `int32` and `uint32` types.
Expand Down
22 changes: 16 additions & 6 deletions lib/compile/jtd/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,22 @@ function serializeString({gen, data}: SerializeCxt): void {
gen.add(N.json, _`${useFunc(gen, quote)}(${data})`)
}

function serializeNumber({gen, data}: SerializeCxt): void {
gen.if(
_`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`,
() => gen.add(N.json, _`null`),
() => gen.add(N.json, _`"" + ${data}`)
)
function serializeNumber({gen, data, self}: SerializeCxt): void {
if (self.opts.safeNumbers === "null") {
gen.if(
_`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`,
() => gen.add(N.json, _`null`),
() => gen.add(N.json, _`"" + ${data}`)
)
} else if (self.opts.safeNumbers === "string") {
gen.if(
_`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`,
() => gen.add(N.json, str`"${data}"`),
() => gen.add(N.json, _`"" + ${data}`)
)
} else {
gen.add(N.json, _`"" + ${data}`)
}
}

function serializeRef(cxt: SerializeCxt): void {
Expand Down
1 change: 1 addition & 0 deletions lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface CurrentOptions {
timestamp?: "string" | "date" // JTD only
parseDate?: boolean // JTD only
allowDate?: boolean // JTD only
safeNumbers?: "string" | "null" // JTD only
$comment?:
| true
| ((comment: string, schemaPath?: string, rootSchema?: AnySchemaObject) => unknown)
Expand Down
40 changes: 32 additions & 8 deletions spec/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,40 @@ describe("JSON Type Definition", () => {
}
})

describe("serialize special numeric values", () => {
const ajv = new _AjvJTD()
describe.only("serialize special numeric values to null", () => {
describe("to null", () => {
const ajv = new _AjvJTD({safeNumbers: "null"})

it(`should serialize Infinity to null`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
assert.deepStrictEqual(JSON.parse(serialize(Infinity)), null)
it(`should serialize Infinity to null`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
assert.deepStrictEqual(JSON.parse(serialize(Infinity)), null)
})
it(`should serialize -Infinity to null`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
assert.deepStrictEqual(JSON.parse(serialize(-Infinity)), null)
})
it(`should serialize NaN to null`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
assert.deepStrictEqual(JSON.parse(serialize(NaN)), null)
})
})
it(`should serialize NaN to null`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
assert.deepStrictEqual(JSON.parse(serialize(NaN)), null)

describe("to string", () => {
const ajv = new _AjvJTD({safeNumbers: "string"})

it(`should serialize Infinity to string`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
console.log(serialize(Infinity))
assert.deepStrictEqual(JSON.parse(serialize(Infinity)), "Infinity")
})
it(`should serialize -Infinity to string`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
assert.deepStrictEqual(JSON.parse(serialize(-Infinity)), "-Infinity")
})
it(`should serialize NaN to string`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
assert.deepStrictEqual(JSON.parse(serialize(NaN)), "NaN")
})
})
})

Expand Down

0 comments on commit 1f6f26c

Please sign in to comment.