Skip to content

Commit

Permalink
feat: add byteLength to JSON serialization of byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Nov 4, 2024
1 parent cc8ca91 commit 2615a07
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
13 changes: 13 additions & 0 deletions json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ Deno.test({
const actual = valueToJSON(ab);
assertEquals(actual, {
type: "ArrayBuffer",
byteLength: 3,
value: "AQID",
});
},
Expand Down Expand Up @@ -482,6 +483,7 @@ Deno.test({
const actual = valueToJSON(dataView);
assertEquals(actual, {
type: "DataView",
byteLength: 3,
value: "AQID",
});
},
Expand Down Expand Up @@ -684,6 +686,7 @@ Deno.test({
const actual = valueToJSON(new Int8Array([1, 2, 3]));
assertEquals(actual, {
type: "Int8Array",
byteLength: 3,
value: "AQID",
});
},
Expand All @@ -695,6 +698,7 @@ Deno.test({
const actual = valueToJSON(new Uint8Array([1, 2, 3]));
assertEquals(actual, {
type: "Uint8Array",
byteLength: 3,
value: "AQID",
});
},
Expand All @@ -706,6 +710,7 @@ Deno.test({
const actual = valueToJSON(new Uint8ClampedArray([1, 2, 3]));
assertEquals(actual, {
type: "Uint8ClampedArray",
byteLength: 3,
value: "AQID",
});
},
Expand All @@ -717,6 +722,7 @@ Deno.test({
const actual = valueToJSON(new Int16Array([1, 2, 3, 4]));
assertEquals(actual, {
type: "Int16Array",
byteLength: 8,
value: "AQACAAMABAA",
});
},
Expand All @@ -728,6 +734,7 @@ Deno.test({
const actual = valueToJSON(new Uint16Array([1, 2, 3, 4]));
assertEquals(actual, {
type: "Uint16Array",
byteLength: 8,
value: "AQACAAMABAA",
});
},
Expand All @@ -739,6 +746,7 @@ Deno.test({
const actual = valueToJSON(new Int32Array([1, 2, 3, 4]));
assertEquals(actual, {
type: "Int32Array",
byteLength: 16,
value: "AQAAAAIAAAADAAAABAAAAA",
});
},
Expand All @@ -750,6 +758,7 @@ Deno.test({
const actual = valueToJSON(new Uint32Array([1, 2, 3, 4]));
assertEquals(actual, {
type: "Uint32Array",
byteLength: 16,
value: "AQAAAAIAAAADAAAABAAAAA",
});
},
Expand All @@ -761,6 +770,7 @@ Deno.test({
const actual = valueToJSON(new Float32Array([1.2, 3.4, 5.6]));
assertEquals(actual, {
type: "Float32Array",
byteLength: 12,
value: "mpmZP5qZWUAzM7NA",
});
},
Expand All @@ -772,6 +782,7 @@ Deno.test({
const actual = valueToJSON(new Float64Array([1.2, 3.4, 5.6]));
assertEquals(actual, {
type: "Float64Array",
byteLength: 24,
value: "MzMzMzMz8z8zMzMzMzMLQGZmZmZmZhZA",
});
},
Expand All @@ -783,6 +794,7 @@ Deno.test({
const actual = valueToJSON(new BigInt64Array([1n, 2n, 3n]));
assertEquals(actual, {
type: "BigInt64Array",
byteLength: 24,
value: "AQAAAAAAAAACAAAAAAAAAAMAAAAAAAAA",
});
},
Expand All @@ -794,6 +806,7 @@ Deno.test({
const actual = valueToJSON(new BigUint64Array([1n, 2n, 3n]));
assertEquals(actual, {
type: "BigUint64Array",
byteLength: 24,
value: "AQAAAAAAAAACAAAAAAAAAAMAAAAAAAAA",
});
},
Expand Down
38 changes: 25 additions & 13 deletions json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export type KvKeyJSON = KvKeyPartJSON[];
*/
export interface KvArrayBufferJSON {
type: "ArrayBuffer";
byteLength?: number;
value: string;
}

Expand Down Expand Up @@ -176,6 +177,7 @@ export interface KvArrayJSON {
*/
export interface KvDataViewJSON {
type: "DataView";
byteLength?: number;
value: string;
}

Expand Down Expand Up @@ -358,6 +360,7 @@ export interface KvTypedArrayJSON<
ArrayType extends TypedArrayTypes = TypedArrayTypes,
> {
type: ArrayType;
byteLength?: number | undefined;
value: string;
}

Expand Down Expand Up @@ -460,38 +463,39 @@ function errorToJSON(error: Error): KvErrorJSON {
/** Internal function to serialize various typed arrays to JSON. */
function typedArrayToJSON(typedArray: ArrayBufferView): KvTypedArrayJSON {
const value = encodeBase64Url(typedArray.buffer);
const byteLength = typedArray.byteLength;
if (typedArray instanceof Int8Array) {
return { type: "Int8Array", value };
return { type: "Int8Array", byteLength, value };
}
if (typedArray instanceof Uint8Array) {
return { type: "Uint8Array", value };
return { type: "Uint8Array", byteLength, value };
}
if (typedArray instanceof Uint8ClampedArray) {
return { type: "Uint8ClampedArray", value };
return { type: "Uint8ClampedArray", byteLength, value };
}
if (typedArray instanceof Int16Array) {
return { type: "Int16Array", value };
return { type: "Int16Array", byteLength, value };
}
if (typedArray instanceof Uint16Array) {
return { type: "Uint16Array", value };
return { type: "Uint16Array", byteLength, value };
}
if (typedArray instanceof Int32Array) {
return { type: "Int32Array", value };
return { type: "Int32Array", byteLength, value };
}
if (typedArray instanceof Uint32Array) {
return { type: "Uint32Array", value };
return { type: "Uint32Array", byteLength, value };
}
if (typedArray instanceof Float32Array) {
return { type: "Float32Array", value };
return { type: "Float32Array", byteLength, value };
}
if (typedArray instanceof Float64Array) {
return { type: "Float64Array", value };
return { type: "Float64Array", byteLength, value };
}
if (typedArray instanceof BigInt64Array) {
return { type: "BigInt64Array", value };
return { type: "BigInt64Array", byteLength, value };
}
if (typedArray instanceof BigUint64Array) {
return { type: "BigUint64Array", value };
return { type: "BigUint64Array", byteLength, value };
}
throw TypeError("Unexpected typed array type, could not serialize.");
}
Expand Down Expand Up @@ -600,13 +604,21 @@ export function valueToJSON(value: unknown): KvValueJSON {
return { type: "json_array", value: value.map(valueToJSON) };
}
if (value instanceof DataView) {
return { type: "DataView", value: encodeBase64Url(value.buffer) };
return {
type: "DataView",
byteLength: value.byteLength,
value: encodeBase64Url(value.buffer),
};
}
if (ArrayBuffer.isView(value)) {
return typedArrayToJSON(value);
}
if (value instanceof ArrayBuffer) {
return { type: "ArrayBuffer", value: encodeBase64Url(value) };
return {
type: "ArrayBuffer",
byteLength: value.byteLength,
value: encodeBase64Url(value),
};
}
if (value instanceof Date) {
return { type: "Date", value: value.toJSON() };
Expand Down

0 comments on commit 2615a07

Please sign in to comment.