Skip to content

Commit

Permalink
feat: array deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Dec 19, 2023
1 parent 08b72fb commit 9712987
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const buildBaseSerializeFunction: SerializeFunctionBuilder = (
functionName: string,
) => `des.${functionName}(`;

const buildSerializeFunction = (functionName: string) => `${buildBaseSerializeFunction(functionName)})`;
const buildSerializeFunction = (functionName: string) =>
`${buildBaseSerializeFunction(functionName)})`;

const arrayDeserializeFunction = buildBaseSerializeFunction("deserializeArray");

Expand Down
98 changes: 95 additions & 3 deletions test/deserializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe("deserializer", () => {
expect(result).toStrictEqual(toSerialize);
});

it("should serialize very deep nested object", () => {
it("should deserialize very deep nested object", () => {
const jsonSchema = {
type: "object",
properties: {
Expand Down Expand Up @@ -224,7 +224,7 @@ describe("deserializer", () => {
expect(result).toStrictEqual(toSerialize);
});

it("should serialize nested array", () => {
it("should deserialize nested array", () => {
const jsonSchema = {
type: "object",
properties: {
Expand Down Expand Up @@ -265,7 +265,99 @@ describe("deserializer", () => {
ser.serializeString(object.city);
});

const result = deserialize(jsonSchema)(createDes(ser.getBuffer()));
const result = deserialize(jsonSchema)(createDes(ser.getBuffer()));

expect(result).toStrictEqual(toSerialize);
});
});

describe("array deserialization", () => {
it("should deserialize array of objects", () => {
const jsonSchema = {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "integer",
},
},
},
} as const;

const toSerialize = [{ name: "test", age: 1 }];

const ser = createSer();
ser.serializeArray(toSerialize, (ser, object) => {
ser.serializeString(object.name);
ser.serializeNumber(object.age);
});

const result = deserialize(jsonSchema)(createDes(ser.getBuffer()));

expect(result).toStrictEqual(toSerialize);
});

it("should deserialize array of plain values", () => {
const jsonSchema = {
type: "array",
items: { type: "string" },
} as const;

const toSerialize = ["test", "test2"];

const ser = createSer();
ser.serializeArray(toSerialize, (ser, object) => {
ser.serializeString(object);
});

const result = deserialize(jsonSchema)(createDes(ser.getBuffer()));

expect(result).toStrictEqual(toSerialize);
});

it("should deserialize array nested inside an object", () => {
const jsonSchema = {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
},
},
},
},
},
},
} as const;

const toSerialize = [
{
name: [
{
name: "test",
},
],
},
];

const ser = createSer();
ser.serializeArray(toSerialize, (ser, object) => {
ser.serializeArray(object.name, (ser, object) => {
ser.serializeString(object.name);
});
});

const result = deserialize(jsonSchema)(createDes(ser.getBuffer()));

expect(result).toStrictEqual(toSerialize);
});
Expand Down

0 comments on commit 9712987

Please sign in to comment.