Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoniangreen committed Aug 18, 2024
1 parent 412498c commit 420d3ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
10 changes: 8 additions & 2 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,16 @@ This option makes JTD validation and parsing more permissive and non-standard. T

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

Defines how special case numbers, Infinity, -Infinity and NaN are handled. Use `specialNumbers: "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 `specialNumbers: "string"` which will serialize them to strings. If this option is not set the values will be included as the original literal values.
Defines how special case numbers `Infinity`, `-Infinity` and `NaN` are handled.

Option values:

- `"fast"` - (default): Do not treat special numbers differently to normal numbers. This is the fastest method but also can produce invalid JSON if the data contains special numbers.
- `"null"` - Special numbers will be serialized to `null` which is the correct behavior according to the JSON spec and is also the same behavior as `JSON.stringify`.
- `"string"` - Special numbers will be serialized as strings, for example `"Infinity"`. This, while non-standard behavior, will allow parsing of the data without losing what the original values were.

::: warning The default behavior can produce invalid JSON
If `specialNumbers` 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.
Using `specialNumbers: "fast" or undefined` can produce invalid JSON when there are any special case numbers in the data.
:::

### int32range <Badge text="JTD only" />
Expand Down
22 changes: 15 additions & 7 deletions lib/compile/jtd/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,23 @@ function serializeString({gen, data}: SerializeCxt): void {
}

function serializeNumber({gen, data, self}: SerializeCxt): void {
const condition = _`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`
const addNumber = (): CodeGen => gen.add(N.json, _`"" + ${data}`)
const condition = _`${data} === Infinity || ${data} === -Infinity || ${data} !== ${data}`

if (self.opts.specialNumbers === "null") {
gen.if(condition, () => gen.add(N.json, _`null`), addNumber)
} else if (self.opts.specialNumbers === "string") {
gen.if(condition, () => gen.add(N.json, str`"${data}"`), addNumber)
if (self.opts.specialNumbers === undefined || self.opts.specialNumbers === "fast") {
gen.add(N.json, _`"" + ${data}`)
} else if (self.opts.specialNumbers === "null") {
gen.if(
condition,
() => gen.add(N.json, _`null`),
() => gen.add(N.json, _`"" + ${data}`)
)
} else {
addNumber()
// specialNumbers === "string"
gen.if(
condition,
() => gen.add(N.json, str`"${data}"`),
() => gen.add(N.json, _`"" + ${data}`)
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export interface CurrentOptions {
timestamp?: "string" | "date" // JTD only
parseDate?: boolean // JTD only
allowDate?: boolean // JTD only
specialNumbers?: "string" | "null" // JTD only
specialNumbers?: "fast" | "string" | "null" // JTD only
$comment?:
| true
| ((comment: string, schemaPath?: string, rootSchema?: AnySchemaObject) => unknown)
Expand Down
4 changes: 2 additions & 2 deletions spec/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ describe("JSON Type Definition", () => {
})

describe("serialize special numeric values", () => {
describe("default", () => {
const ajv = new _AjvJTD()
describe("fast", () => {
const ajv = new _AjvJTD({specialNumbers: "fast"})

it(`should serialize Infinity to literal`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
Expand Down

0 comments on commit 420d3ca

Please sign in to comment.