File size: 879 Bytes
cf47645 9104321 cf47645 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
export function encodeObject(obj: unknown): string {
/**
* Encodes an object to a string using JSON serialization and Base64 encoding.
*
* Args:
* obj: The object to encode.
*
* Returns:
* A string representation of the object.
*/
const jsonString: string = JSON.stringify(obj);
const encodedString: string = btoa(unescape(encodeURIComponent(jsonString))); // btoa expects only ASCII chars
return encodedString;
}
export function decodeString(encodedString: string): unknown {
/**
* Decodes a string to an object using Base64 decoding and JSON deserialization.
*
* Args:
* encodedString: The string to decode.
*
* Returns:
* The decoded object.
*/
try {
const jsonString: string = decodeURIComponent(escape(atob(encodedString)));
const obj: unknown = JSON.parse(jsonString);
return obj;
} catch {
return null;
}
}
|