Comments (5)
Sorry for the unclarity, you need to call useContextVars()
on the top-level parser. If you change
const parser = new Parser().array('data', {
to
const parser = new Parser().useContextVars().array('data', {
This works:
const Parser = require('binary-parser').Parser;
const buf = Buffer.from('a1012aa2027ab8a30460713d44ff03414243a401ff', 'hex');
const parser = new Parser().useContextVars().array('data', {
readUntil: 'eof',
type: new Parser()
.uint8('type')
.uint8('length')
.choice('value', {
tag: 'type',
choices: {
0xa1: new Parser().uint8('_a1'),
0xa2: new Parser().uint16be('_a2'),
0xa3: new Parser().buffer('_a3', { length: '$parent.length' }),
0xa4: new Parser().uint8('_a4'),
},
defaultChoice: new Parser().buffer('other', { length: '$parent.length' }),
}),
});
console.dir(parser.parse(buf), { depth: null });
Output:
{
data: [
{ type: 161, length: 1, value: { _a1: 42 } },
{ type: 162, length: 2, value: { _a2: 31416 } },
{
type: 163,
length: 4,
value: { _a3: Buffer(4) [Uint8Array] [ 96, 113, 61, 68 ] }
},
{
type: 255,
length: 3,
value: { other: Buffer(3) [Uint8Array] [ 65, 66, 67 ] }
},
{ type: 164, length: 1, value: { _a4: 255 } }
]
}
from binary-parser.
I see... your solution works. I'm curious though, does this mean useContextVars() have to declared at the very top level parser, and at that level only?
from binary-parser.
Yes, you have to declare it at the top level. If you call it at the lower levels it's simply ignored. This is required because the top-level parser has to know if the context vars is enabled as it sets$root
.
from binary-parser.
Closing since your initial problem seems resolved. Feel free to reopen if needed.
from binary-parser.
I see, thank you for the explanation.
from binary-parser.
Related Issues (20)
- Choice keyString returns entire string HOT 2
- Combining choice with array. HOT 4
- Reading array - unknown size in bytes HOT 2
- Directly embed parse result of wrapped object? HOT 3
- [HELP] looking for string tokens inside binary file - how to do this when length is unknown? HOT 3
- How to handle a field that is present depending on an earlier field HOT 2
- Maximum call stack size exceeded if parsing a lot of bit fields
- Allow async "wrapper" callbacks HOT 3
- Array containing arrays are not getting parsed HOT 2
- Building parsers programmatically (question) HOT 2
- Difference between bit8 and uint8 HOT 1
- Parse order is wrong with bit fields followed by nest
- Array with non 8bit alignment HOT 1
- Old array of strings work around no longer works due to typing HOT 1
- Referencing target buffer in parser
- Cleanup non issue
- Feature request
- How to use it in js HOT 3
- Turning Objects Back into Buffers? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from binary-parser.