From b49cbbd3777ecab8f10adbc953f3ba397c700c2e Mon Sep 17 00:00:00 2001 From: Harvmaster Date: Mon, 20 Jul 2026 05:31:54 +0000 Subject: [PATCH] Add toExtendedJson and fromExtendedJson helper functions --- source/extended-json.ts | 20 ++++++++++++++++++++ test/extended-json.test.ts | 32 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/source/extended-json.ts b/source/extended-json.ts index ea77d41..9b4d06c 100644 --- a/source/extended-json.ts +++ b/source/extended-json.ts @@ -74,3 +74,23 @@ export const extendedJsonReviver = (_propertyKey: string, value: unknown): unkno // If the value does not match either pattern, return the original value return value; }; + +/** + * Serializes an object to a string using the {@link extendedJsonReplacer}. + * + * @param object The object to serialize. + * @returns The string representation of the object in Extended JSON format. + */ +export const toExtendedJson = (object: unknown): string => { + return JSON.stringify(object, extendedJsonReplacer); +}; + +/** + * Deserializes a string to an object using the {@link extendedJsonReviver}. + * + * @param serializedObject The string to deserialize. + * @returns The object reconstructed from the string. + */ +export const fromExtendedJson = (serializedObject: string): unknown => { + return JSON.parse(serializedObject, extendedJsonReviver); +}; diff --git a/test/extended-json.test.ts b/test/extended-json.test.ts index 22032c6..13705e9 100644 --- a/test/extended-json.test.ts +++ b/test/extended-json.test.ts @@ -1,5 +1,5 @@ import { expect, test } from 'vitest'; -import { extendedJsonReviver } from '../source/index.ts'; +import { extendedJsonReviver, toExtendedJson, fromExtendedJson } from '../source/index.ts'; /** * Tests that extendedJsonReviver reconstructs a positive bigint. @@ -64,6 +64,34 @@ const testReviverPassesThroughPlainString = (): void => { expect(reconstructed).toBe('just a string'); }; +/** + * Tests that toExtendedJson serializes an object to a string. + */ +const testToExtendedJsonSerializesObject = (): void => { + // Define an object with a Uint8Array + const extendedObject = { bytes: new Uint8Array([ 0xab, 0xcd ]) }; + + // Serialize the object to a string + const serialized = toExtendedJson(extendedObject); + + // The serialized string should contain the Uint8Array in Extended JSON format (Uint8Arrays are encoded as hex strings) + expect(serialized).toBe('{"bytes":""}'); +}; + +/** + * Tests that fromExtendedJson deserializes a string to an object. + */ +const testFromExtendedJsonDeserializesString = (): void => { + // Define a string in Extended JSON format that contains a Uint8Array + const serializedObject = '{"bytes":""}'; + + // Deserialize the string to an object + const deserialized = fromExtendedJson(serializedObject); + + // The deserialized object should contain the Uint8Array + expect(deserialized).toStrictEqual({ bytes: new Uint8Array([ 0xab, 0xcd ]) }); +}; + /** * Tests that extendedJsonReviver passes through non-string values. */ @@ -85,6 +113,8 @@ const runTests = async (): Promise => { test('extendedJsonReviver: reconstructs an empty Uint8Array', testReviverReconstructsEmptyUint8Array); test('extendedJsonReviver: passes through plain strings', testReviverPassesThroughPlainString); test('extendedJsonReviver: passes through non-string values', testReviverPassesThroughNonStringValues); + test('toExtendedJson: serializes an object to a string', testToExtendedJsonSerializesObject); + test('fromExtendedJson: deserializes a string to an object', testFromExtendedJsonDeserializesString); }; await runTests();