zserge / jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
MIT License
3.67k stars 781 forks source link

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

Closed markfink closed 5 years ago

markfink commented 5 years ago

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 commented 5 years ago

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

markfink commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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).