Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ describe(JsonShapeDeserializer.name, () => {
});
});

it("deserializes numeric members to number when the reviver is active", async () => {
expect(await deserializer.read(widget, `{ "scalar": 1.0 }`)).toEqual({ scalar: 1 });
expect(await deserializer.read(widget, `{ "scalar": 0.0 }`)).toEqual({ scalar: 0 });
expect(await deserializer.read(widget, `{ "scalar": 1e3 }`)).toEqual({ scalar: 1000 });

const data = (await deserializer.read(widget, `{ "scalar": 1.0 }`)) as { scalar: unknown };
expect(typeof data.scalar).toBe("number");
});

it("deserializes infinite and NaN numerics", async () => {
expect(await deserializer.read(widget, JSON.stringify({ scalar: "Infinity" }))).toEqual({ scalar: Infinity });
expect(await deserializer.read(widget, JSON.stringify({ scalar: "-Infinity" }))).toEqual({ scalar: -Infinity });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,28 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
return new NumericValue(String(value), "bigDecimal");
}

if (ns.isNumericSchema() && typeof value === "string") {
switch (value) {
case "Infinity":
return Infinity;
case "-Infinity":
return -Infinity;
case "NaN":
return NaN;
if (ns.isNumericSchema() && value != undefined) {
if (typeof value === "string") {
switch (value) {
case "Infinity":
return Infinity;
case "-Infinity":
return -Infinity;
case "NaN":
return NaN;
}
return value;
}
if (typeof value === "bigint") {
return Number(value);
}
if (value instanceof NumericValue) {
return Number(value.string);
}
const untyped = value as any;
if (untyped.type === "bigDecimal" && "string" in untyped) {
return Number(untyped.string);
}
return value;
}

if (ns.isDocumentSchema()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nv, NumericValue } from "@smithy/core/serde";
import { NumericValue } from "@smithy/core/serde";
import type { StaticStructureSchema, TimestampEpochSecondsSchema } from "@smithy/types";
import { describe, expect, test as it } from "vitest";

Expand Down Expand Up @@ -151,6 +151,15 @@ describe(JsonShapeDeserializer2.name, () => {
});
});

it("deserializes numeric members to number when the reviver is active", async () => {
expect(await deserializer.read(widget, `{ "scalar": 1.0 }`)).toEqual({ scalar: 1 });
expect(await deserializer.read(widget, `{ "scalar": 0.0 }`)).toEqual({ scalar: 0 });
expect(await deserializer.read(widget, `{ "scalar": 1e3 }`)).toEqual({ scalar: 1000 });

const data = (await deserializer.read(widget, `{ "scalar": 1.0 }`)) as { scalar: unknown };
expect(typeof data.scalar).toBe("number");
});

it("deserializes infinite and NaN numerics", async () => {
expect(await deserializer.read(widget, JSON.stringify({ scalar: "Infinity" }))).toEqual({ scalar: Infinity });
expect(await deserializer.read(widget, JSON.stringify({ scalar: "-Infinity" }))).toEqual({ scalar: -Infinity });
Expand Down Expand Up @@ -300,10 +309,10 @@ describe(JsonShapeDeserializer2.name, () => {
async () => {
const json = `{"scalar": 1.123456789012345678E16}`;
await expect(v1Deserializer.read(widget, json)).resolves.toEqual({
scalar: nv("1.123456789012345678E16"),
scalar: 11234567890123456,
});
await expect(deserializer.read(widget, json)).resolves.toEqual({
scalar: nv("1.123456789012345678E16"),
scalar: 11234567890123456,
});
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,28 @@ export class JsonShapeDeserializer2 extends SerdeContextConfig implements ShapeD
return new NumericValue(String(value), "bigDecimal");
}

if (ns.isNumericSchema() && typeof value === "string") {
switch (value) {
case "Infinity":
return Infinity;
case "-Infinity":
return -Infinity;
case "NaN":
return NaN;
if (ns.isNumericSchema() && value != undefined) {
if (typeof value === "string") {
switch (value) {
case "Infinity":
return Infinity;
case "-Infinity":
return -Infinity;
case "NaN":
return NaN;
}
return value;
}
if (typeof value === "bigint") {
return Number(value);
}
if (value instanceof NumericValue) {
return Number(value.string);
}
const untyped = value as any;
if (untyped.type === "bigDecimal" && "string" in untyped) {
return Number(untyped.string);
}
return value;
}

if (ns.isDocumentSchema()) {
Expand Down