Thomas G. Lopes
add copy project option
cf47645
raw
history blame
840 Bytes
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.
*/
const jsonString: string = decodeURIComponent(escape(atob(encodedString)));
const obj: unknown = JSON.parse(jsonString);
return obj;
}