export function decode()

in src/common/providers/https.ts [514:551]


export function decode(data: any): any {
  if (data === null) {
    return data;
  }
  if (data['@type']) {
    switch (data['@type']) {
      case LONG_TYPE:
      // Fall through and handle this the same as unsigned.
      case UNSIGNED_LONG_TYPE: {
        // Technically, this could work return a valid number for malformed
        // data if there was a number followed by garbage. But it's just not
        // worth all the extra code to detect that case.
        const value = parseFloat(data.value);
        if (isNaN(value)) {
          logger.error('Data cannot be decoded from JSON.', data);
          throw new Error('Data cannot be decoded from JSON: ' + data);
        }
        return value;
      }
      default: {
        logger.error('Data cannot be decoded from JSON.', data);
        throw new Error('Data cannot be decoded from JSON: ' + data);
      }
    }
  }
  if (Array.isArray(data)) {
    return data.map(decode);
  }
  if (typeof data === 'object') {
    const obj: Record<string, any> = {};
    for (const [k, v] of Object.entries(data)) {
      obj[k] = decode(v);
    }
    return obj;
  }
  // Anything else is safe to return.
  return data;
}