diff --git a/docs/options.md b/docs/options.md index f7b573f01..e8465e815 100644 --- a/docs/options.md +++ b/docs/options.md @@ -212,7 +212,6 @@ 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 Using `specialNumbers: "fast" or undefined` can produce invalid JSON when there are any special case numbers in the data. diff --git a/lib/compile/jtd/serialize.ts b/lib/compile/jtd/serialize.ts index a38d3ff61..42a47cffc 100644 --- a/lib/compile/jtd/serialize.ts +++ b/lib/compile/jtd/serialize.ts @@ -233,17 +233,11 @@ function serializeNumber({gen, data, self}: SerializeCxt): void { 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 { - // specialNumbers === "string" + // specialNumbers === "null" gen.if( condition, - () => gen.add(N.json, str`"${data}"`), + () => gen.add(N.json, _`null`), () => gen.add(N.json, _`"" + ${data}`) ) } diff --git a/lib/core.ts b/lib/core.ts index 3ae030f8b..6ceedf541 100644 --- a/lib/core.ts +++ b/lib/core.ts @@ -107,7 +107,7 @@ export interface CurrentOptions { timestamp?: "string" | "date" // JTD only parseDate?: boolean // JTD only allowDate?: boolean // JTD only - specialNumbers?: "fast" | "string" | "null" // JTD only + specialNumbers?: "fast" | "null" // JTD only $comment?: | true | ((comment: string, schemaPath?: string, rootSchema?: AnySchemaObject) => unknown) diff --git a/spec/jtd-schema.spec.ts b/spec/jtd-schema.spec.ts index 9c712110f..b9cf3ab69 100644 --- a/spec/jtd-schema.spec.ts +++ b/spec/jtd-schema.spec.ts @@ -191,29 +191,6 @@ describe("JSON Type Definition", () => { assert.equal(JSON.parse(res), null) }) }) - - describe("to string", () => { - const ajv = new _AjvJTD({specialNumbers: "string"}) - - it(`should serialize Infinity to string`, () => { - const serialize = ajv.compileSerializer({type: "float64"}) - const res = serialize(Infinity) - assert.equal(res, '"Infinity"') - assert.equal(JSON.parse(res), "Infinity") - }) - it(`should serialize -Infinity to string`, () => { - const serialize = ajv.compileSerializer({type: "float64"}) - const res = serialize(-Infinity) - assert.equal(res, '"-Infinity"') - assert.equal(JSON.parse(res), "-Infinity") - }) - it(`should serialize NaN to string`, () => { - const serialize = ajv.compileSerializer({type: "float64"}) - const res = serialize(NaN) - assert.equal(res, '"NaN"') - assert.equal(JSON.parse(res), "NaN") - }) - }) }) describe("parse", () => {