Skip to content

Commit

Permalink
remove string option
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoniangreen committed Aug 18, 2024
1 parent 420d3ca commit cb5c5dc
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 33 deletions.
1 change: 0 additions & 1 deletion docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 2 additions & 8 deletions lib/compile/jtd/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
)
}
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?: "fast" | "string" | "null" // JTD only
specialNumbers?: "fast" | "null" // JTD only
$comment?:
| true
| ((comment: string, schemaPath?: string, rootSchema?: AnySchemaObject) => unknown)
Expand Down
23 changes: 0 additions & 23 deletions spec/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit cb5c5dc

Please sign in to comment.