I would like to parse about 5-10 different message types that share a common format (such as JSON, for example) but each have specific fields that need to be validated. Each message should eventually be parsed into a custom class/struct that has types that don't require any sort of casting (e.g. a field is an int instead of a variant/tuple). I see two approaches to the problem:
Write a grammar for each specific message that handles the validation for both the message format (JSON boilerplate, in this example) and validates the content of the fields, returning a truly custom struct
Write a grammar that only validates the structure (just the JSON rules) and returns a more generic object (with fields that are variants/tuples, etc.) and validate/translate at a higher level into a custom struct (casting and checking the various variant fields)
I see these as the pros and cons of each:
Pros for 1:
Cons for 1:
Pros for 2:
Cons for 2:
Which method is preferable? Is there a third way to parse into multiple types using one grammar?
Here's some example messages and the classes they should eventually reside in:
{"messageType": "messageTypeA", "numberParam": 1}
{"messageType": "messageTypeB", "stringParam": "Test"}
class MessageTypeA
{
public:
double numberParam;
};
class MessageTypeB
{
public:
std::string stringParam;
};
I think this question is really close to a recent answer, where I did precisely that: I answered with two answers:
My vote is with the first option, because
Even the OP in the linked question seemed to later need exactly the flexibility that my first answer already afforded:
Oh well. I'm pretty surprised that you accepted this answer then, since the other answer does exactly the same, except it does accept and ignore "other" JSON content. Did you miss the update that defined
extract_from
? It uses exactly the same data structure - the one you suggested in the question. ā sehe Jan 4 at 16:41