Skip to content

Commit

Permalink
Merge pull request #79 from nobrayner/fix-78
Browse files Browse the repository at this point in the history
Fix 78: Serializer.encode throws for null and undefined
  • Loading branch information
bloodyowl authored Aug 29, 2024
2 parents 328f9d3 + d9e3483 commit 2129b74
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const encode = (value: any, indent?: number | undefined) => {
return JSON.stringify(
value,
(key, value) => {
if (value == null) {
return value;
}
if (typeof value[BOXED_TYPE] === "string") {
return { ...value, __boxed_type__: value[BOXED_TYPE] };
}
Expand Down
9 changes: 9 additions & 0 deletions test/Serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ test("Serializer", () => {
nothing: Option.None(),
loading: AsyncData.Loading(),
notAsked: AsyncData.NotAsked(),
nullable: null,
undefinable: undefined,
string: "Hello",
number: 1209,
boolean: true,
array: [1, 2, 3],
object: { a: 1, b: "two" },
}),
),
),
Expand All @@ -22,4 +29,6 @@ test("Serializer", () => {
expect(decode(encode(start)).value.map(() => "Hello")).toEqual(
AsyncData.Done("Hello"),
);
expect(encode(null)).toEqual("null");
expect(encode(undefined)).toEqual(undefined);
});

0 comments on commit 2129b74

Please sign in to comment.