Проверьте, не повреждены ли необработанные данные - PullRequest
0 голосов
/ 06 ноября 2018

Я сохраняю файлы .tmp, содержащие эти необработанные данные (буфер)

[{"start_bin_value":"000180000","end_bin_value":"000180999","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"1","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"021502670","end_bin_value":"021502909","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"221572830","end_bin_value":"221572839","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"000","funding_source":"D","restricted_use":"A"},{"start_bin_value":"308952000","end_bin_value":"308969999","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"}]

Теперь я хотел бы проверить (каждый раз, когда я читаю эти файлы), что содержимое / необработанные данные не повреждены / не повреждены, и я могу как-то их проанализировать

Как я могу это сделать практически?

Спасибо.

1 Ответ

0 голосов
/ 06 ноября 2018

Вы не указываете, что вы имеете в виду под поврежден / сломан , но если вы хотите только проверить, можете ли вы его проанализировать, просто прочитайте его с помощью fs.readFileSync и используйте try...catch блок вокруг JSON.parse:

/*

In Node.js you would do:

const fs = require('fs');
const rawData = fs.readFileSync("path/to/data.json");

*/

let rawData = `
[{"start_bin_value":"000180000","end_bin_value":"000180999","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"1","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"021502670","end_bin_value":"021502909","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"221572830","end_bin_value":"221572839","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"000","funding_source":"D","restricted_use":"A"},{"start_bin_value":"308952000","end_bin_value":"308969999","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"}]
`;

try {
  const data = JSON.parse(rawData);
  
  console.log('Data parsed OK!');
} catch(err) {
  console.error('Data could not be parsed!');
  console.error(err);
}

// Corrupt it so that it's no longer valid JSON:
rawData = rawData.slice(0, -10);

try {
  const data = JSON.parse(rawData);
  
  console.log('Data parsed OK!');
} catch(err) {
  console.error('Data could not be parsed!');
  console.error(err);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...