SafeJSON.ts 605 B

12345678910111213141516171819202122
  1. /**
  2. * @en call JSON mthods safely.
  3. * @zh 用于安全操作JSON相关函数
  4. * */
  5. export class SafeJSON {
  6. public static parse(text: string, reciver?: (key: any, value: any) => any): any {
  7. try {
  8. return JSON.parse(text, reciver);
  9. } catch (error) {
  10. console.log(error);
  11. return null;
  12. }
  13. }
  14. public static stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number) {
  15. try {
  16. return JSON.stringify(value, replacer, space);
  17. } catch (error) {
  18. return '';
  19. }
  20. }
  21. }