Skip to content

Commit

Permalink
change option name to specialNumbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoniangreen committed Aug 17, 2024
1 parent 877a0e1 commit c2ef245
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ 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" />
### specialNumbers <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.
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.

::: 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.
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.
:::

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

function serializeNumber({gen, data, self}: SerializeCxt): void {
if (self.opts.safeNumbers === "null") {
if (self.opts.specialNumbers === "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") {
} else if (self.opts.specialNumbers === "string") {
gen.if(
_`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`,
() => gen.add(N.json, str`"${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
safeNumbers?: "string" | "null" // JTD only
specialNumbers?: "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 @@ -170,7 +170,7 @@ describe("JSON Type Definition", () => {
})
})
describe("to null", () => {
const ajv = new _AjvJTD({safeNumbers: "null"})
const ajv = new _AjvJTD({specialNumbers: "null"})

it(`should serialize Infinity to null`, () => {
const serialize = ajv.compileSerializer({type: "float64"})
Expand All @@ -193,7 +193,7 @@ describe("JSON Type Definition", () => {
})

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

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

0 comments on commit c2ef245

Please sign in to comment.