Add toExtendedJson and fromExtendedJson helper functions
This commit is contained in:
@@ -74,3 +74,23 @@ export const extendedJsonReviver = (_propertyKey: string, value: unknown): unkno
|
|||||||
// If the value does not match either pattern, return the original value
|
// If the value does not match either pattern, return the original value
|
||||||
return 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);
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { expect, test } from 'vitest';
|
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.
|
* Tests that extendedJsonReviver reconstructs a positive bigint.
|
||||||
@@ -64,6 +64,34 @@ const testReviverPassesThroughPlainString = (): void => {
|
|||||||
expect(reconstructed).toBe('just a string');
|
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":"<uint8array: abcd>"}');
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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":"<uint8array: abcd>"}';
|
||||||
|
|
||||||
|
// 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.
|
* Tests that extendedJsonReviver passes through non-string values.
|
||||||
*/
|
*/
|
||||||
@@ -85,6 +113,8 @@ const runTests = async (): Promise<void> => {
|
|||||||
test('extendedJsonReviver: reconstructs an empty Uint8Array', testReviverReconstructsEmptyUint8Array);
|
test('extendedJsonReviver: reconstructs an empty Uint8Array', testReviverReconstructsEmptyUint8Array);
|
||||||
test('extendedJsonReviver: passes through plain strings', testReviverPassesThroughPlainString);
|
test('extendedJsonReviver: passes through plain strings', testReviverPassesThroughPlainString);
|
||||||
test('extendedJsonReviver: passes through non-string values', testReviverPassesThroughNonStringValues);
|
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();
|
await runTests();
|
||||||
|
|||||||
Reference in New Issue
Block a user