PeculiarVentures / ASN1.js

ASN1js is a pure JavaScript library implementing a full ASN.1 BER decoder and encoder.
https://asn1js.org
Other
267 stars 57 forks source link

Convert decoded BER to JSON #54

Closed ashkank83 closed 4 years ago

ashkank83 commented 4 years ago

Hi, Thanks for this great library. I'm trying to use your library for converting TAP files and have the result as JSON. I did read all the documentation and put a small example together which is converting my files, however can't find anyway to convert the whole object into JSON so it can be stored in Mongo (Or any other database). Is there anyway to convert the result to JSON? Below is my sample code (nodeJS):

var asn1 = require("asn1js");
var fs = require("fs");
var data = fs.readFileSync("/myTAPFile");
//console.log(data);
var obj = asn1.fromBER(data.buffer);
console.log(obj); // this is successfully logging the converted structure 
//console.log(obj.result);

result of console.log above

➜  JS-ASN.1 node src/index2.js
{
  offset: 78313,
  result: Constructed {
    blockLength: 78313,
    error: '',
    warnings: [],
    valueBeforeDecode: ArrayBuffer {
      [Uint8Contents]: <61 83 01 31 e4 64 81 8b 5f 81 44 05 44 4e 4b 54 44 5f 81 36 05 32 53 50 35 30 5f 6d 05 32 38 39 33 30 7f 6c 19 50 0e 32 30 32 30 30 35 31 35 32 33 35 35 32 39 5f 81 67 05 2b 30 32 30 30 7f 81 63 19 50 0e 32 30 32 30 30 35 31 35 32 33 35 35 32 39 5f 81 67 05 2b 30 32 30 30 7f 6b 19 50 0e 32 30 32 30 ... 78213 more bytes>,
      byteLength: 78313
    },
    idBlock: LocalIdentificationBlock {
      blockLength: 1,
      error: '',
      warnings: [],
      valueBeforeDecode: [ArrayBuffer],
      isHexOnly: false,
      valueHex: [ArrayBuffer],
      tagClass: 2,
      tagNumber: 1,
      isConstructed: true
    },
    lenBlock: LocalLengthBlock {
      blockLength: 4,
      error: '',
      warnings: [],
      valueBeforeDecode: [ArrayBuffer],
      isIndefiniteForm: false,
      longFormUsed: true,
      length: 78308
    },
    valueBlock: LocalConstructedValueBlock {
      blockLength: 78308,
      error: '',
      warnings: [],
      valueBeforeDecode: [ArrayBuffer],
      value: [Array],
      isIndefiniteForm: false
    }
  }
}
YuryStrozhevsky commented 4 years ago

@ashkank83 Common way JSON.stringify on asn1.result object. Read JS documents on JSON object and apply additional filters in order to make output smaller.