-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert BER result to JSON #42
Comments
Hi, it all depends on what type of JSON you expect to produce, there isn't a standard way to represent ASN.1 data as JSON. Example given: const ASN1 = require('./asn1');
const ber = Buffer.from('23800303000A3B230A0302005F030404291CD00000', 'hex');
console.log(ASN1.decode(ber).toPrettyString()); produces this output:
and you could imagine producing a JSON like this: {
"type": "BIT_STRING",
"length": 19,
"constructed": true,
"content": "00001010001110110101111100101001000111001101",
"parts": [
{
"type": "BIT_STRING",
"length": 5,
"content": "0000101000111011",
},
{
"type": "BIT_STRING",
"length": 17,
"constructed": true,
"content": "0101111100101001000111001101",
"parts": [
{
"type": "BIT_STRING",
"length": 11,
"content": "01011111"
},
{
"type": "BIT_STRING",
"length": 21,
"content": "00101001000111001101"
}
]
}
]
} But you could desire many other possible formats. Consider that ASN.1 value by them selves have no "meaning", only structure, and the meaning (i.e. names associated to values) are defined by ASN.1 specifications such as the one you're linking (and you could try to get some JSON with proper names such as |
Hi,
Is there a way to have this same result in json where it looks like:
|
In your example all the elements use a different custom application number but there could be in principle be more elements with the same application number and in that case the format you propose wouldn't be able to represent it. All in all, it seems a very custom format that you can derive yourself from the current library's output, perhaps something like this? (I added an array to allow having multiple tags with the same number, but of course the order of element appearance is still lost) const ASN1 = require('@lapo/asn1js');
const ber = Buffer.from('3082017F30820131A00302010202147C8E6449D70ED92D3E2E4A5D2F76F6554246D746300506032B65703035310B3009060355040613024954310F300D06035504070C064D696C616E6F3115301306035504030C0C546573742065643235353139301E170D3230303930323133323532365A170D3330303930323133323532365A3035310B3009060355040613024954310F300D06035504070C064D696C616E6F3115301306035504030C0C546573742065643235353139302A300506032B65700321003BA92FFDCB1766DE40A292F793DE30F80A23A831215DD007D863242EFF682185A3533051301D0603551D0E041604146BA5BDCF9DFA235978126417AE1E72D89A804AE8301F0603551D230418301680146BA5BDCF9DFA235978126417AE1E72D89A804AE8300F0603551D130101FF040530030101FF300506032B65700341006F7377BE28965A3336D7E534FD90F3FD407F1F02F90057F2160F166B04BF6584B698D2D0D2BF4CD66F0EB6E2E89D04A3E09950F9C26DDE73AD1D355785658606', 'hex');
function customJSON(asn1) {
if (asn1.sub == null)
return asn1.content();
const x = {};
for (const el of asn1.sub) {
const key = el.tag.tagNumber;
(x[key] = x[key] || []).push(customJSON(el));
}
return x;
}
console.log('Custom JSON:', JSON.stringify(customJSON(ASN1.decode(ber)), null, 2)); |
Thank you very much, this is working like a charm :-) I just had to modify Stream.prototype.parseOctetString so it won't return the byte length (len byte). |
BTW: I plan to add a new method similar to |
I think that'll be perfect. for example I'm storing the decoded data in MongoDB and I believe something like what you mentioned would help a lot. Thanks |
Hi,
I'm trying to use your library for converting TAP files and have the result as JSON.using the example below I can convert the file however I can't find anyway to convert the whole object into JSON. Is there anyway to convert the result to JSON?
Below is my sample code (nodeJS):
and below is what I see in the logs:
The text was updated successfully, but these errors were encountered: