File size: 489 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { isDefined } from '@/utils/validation';

export const parseJson = <T>(
  rawJson: string | boolean | null | number | undefined,
): T | null => {
  try {
    if (!isDefined(rawJson)) {
      return null;
    }

    if (rawJson === '') {
      return null;
    }

    // This is a hack to handle the case where the value is a scalar value which is part of JSON spec but not implemented before ES2019
    return JSON.parse('[' + rawJson + ']')[0];
  } catch {
    return null;
  }
};