Skip to content
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

question: how to best split parser into multiple specialized ones #150

Closed
markfink opened this issue Dec 24, 2018 · 5 comments

Comments

@markfink
Copy link

@markfink markfink commented Dec 24, 2018

please appologise if this is a "stupid" question... I am new to jsmn and so far I have only found simple examples on how to use it. I try to build a parser which is "assembled" from multiple specialized parsers.

For a data structure like this (always arrives as this big chunk of data) I like to split the parser into a house_parser() and car_parser().

{
"house": {"architecture": "wood_frame", "sqm": 100, ...},
"car": {"brand": "", "horsepower": 40} 
}

I hope you can share some pointers on how to achieve this. Just a pointer to a sample or codebase which is doing this would be great!
Thank you.

@pt300

This comment has been minimized.

Copy link
Collaborator

@pt300 pt300 commented Dec 27, 2018

https://gist.github.com/pt300/17441e85594428f7e2cb2a58cef2540d
Here's an example that hopefully helps you.

@markfink

This comment has been minimized.

Copy link
Author

@markfink markfink commented Dec 29, 2018

@pt300 thank you for this code. Your code implements one big parser which regretably is exactly what I try to avoid. Instead of using a monolithic parser I am looking for a way to decompose into simpler sub-parsers. In other words I try to build a parser which is "assembled" / composed from multiple specialized parsers.

@pt300

This comment has been minimized.

Copy link
Collaborator

@pt300 pt300 commented Dec 29, 2018

It's possible to split the parsing of house and car into separate parsing functions, but you still will need a parser for the root object which will call these "subparsers" when it encounters the appropriate keys.
I could try writing something like that if you still need it.

@markfink

This comment has been minimized.

Copy link
Author

@markfink markfink commented Dec 29, 2018

right, it is possible. I hope smb. will share how they usually go about it. you know, a JSON file usually gets pretty big (often with hundreds or even thousands of elements) - I am still convinced that a suitable parser would not be engineed as a monolith. Unless the parser code is automatically generated from - lets say json-schema - which I think would be ok, too.

@markfink

This comment has been minimized.

Copy link
Author

@markfink markfink commented Jan 20, 2019

@pt300 again thank you for your help to discuss this. the solution to structure the parser into multiple functions was actually pretty simple. Today I figured it out and wrote a blog post about it (containing a complete sample).

@markfink markfink closed this Jan 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.