From 0791b3aa8a4765bd3037ab8761d53b9a61567d5d Mon Sep 17 00:00:00 2001 From: Bart Blast Date: Thu, 17 Oct 2024 15:11:58 +0200 Subject: [PATCH] Serialize boxed maps --- assets/js/serializer.mjs | 4 ++++ test/javascript/serializer_test.mjs | 31 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/assets/js/serializer.mjs b/assets/js/serializer.mjs index ce17d2416..53de70f4b 100644 --- a/assets/js/serializer.mjs +++ b/assets/js/serializer.mjs @@ -26,6 +26,10 @@ export default class Serializer { return `__integer__:${value.value.toString()}`; } + if (value?.type === "map") { + return {...value, data: Object.values(value.data)}; + } + if (typeof value === "bigint") { return `__bigint__:${value.toString()}`; } diff --git a/test/javascript/serializer_test.mjs b/test/javascript/serializer_test.mjs index 0d1d1d30b..447696a97 100644 --- a/test/javascript/serializer_test.mjs +++ b/test/javascript/serializer_test.mjs @@ -10,7 +10,7 @@ import Type from "../../assets/js/type.mjs"; defineGlobalErlangAndElixirModules(); -describe.only("Serializer", () => { +describe("Serializer", () => { describe("serialize()", () => { const serialize = Serializer.serialize; @@ -120,6 +120,35 @@ describe.only("Serializer", () => { assert.equal(serialize(term), expected); }); }); + + describe("map", () => { + it("top-level", () => { + const term = Type.map([ + [Type.atom("x"), Type.integer(1)], + [Type.bitstring("y"), Type.float(1.23)], + ]); + + const expected = + '[1,{"type":"map","data":[["__atom__:x","__integer__:1"],["__binary__:y","__float__:1.23"]]}]'; + + assert.equal(serialize(term), expected); + }); + + it("nested", () => { + const term = { + a: Type.map([ + [Type.atom("x"), Type.integer(1)], + [Type.bitstring("y"), Type.float(1.23)], + ]), + b: 2, + }; + + const expected = + '[1,{"a":{"type":"map","data":[["__atom__:x","__integer__:1"],["__binary__:y","__float__:1.23"]]},"b":2}]'; + + assert.equal(serialize(term), expected); + }); + }); }); describe("JS terms", () => {