json: teach json builder "conditional" object fields
Quite often when we generate objects some fields should only be generated in some conditions. Let's add high-level support for that. Matching the existing JSON_BUILD_PAIR() this adds JSON_BUILD_PAIR_CONDITIONAL() which is very similar, but takes an additional parameter: a boolean condition. If "true" this acts like JSON_BUILD_PAIR(), but if false then the whole pair is suppressed. This sounds simply, but requires a tiny bit of complexity: when complex sub-variants are used in fields, then we also need to suppress them.
This commit is contained in:
@@ -177,6 +177,7 @@ enum {
|
||||
_JSON_BUILD_OBJECT_BEGIN,
|
||||
_JSON_BUILD_OBJECT_END,
|
||||
_JSON_BUILD_PAIR,
|
||||
_JSON_BUILD_PAIR_CONDITION,
|
||||
_JSON_BUILD_NULL,
|
||||
_JSON_BUILD_VARIANT,
|
||||
_JSON_BUILD_LITERAL,
|
||||
@@ -192,6 +193,7 @@ enum {
|
||||
#define JSON_BUILD_ARRAY(...) _JSON_BUILD_ARRAY_BEGIN, __VA_ARGS__, _JSON_BUILD_ARRAY_END
|
||||
#define JSON_BUILD_OBJECT(...) _JSON_BUILD_OBJECT_BEGIN, __VA_ARGS__, _JSON_BUILD_OBJECT_END
|
||||
#define JSON_BUILD_PAIR(n, ...) _JSON_BUILD_PAIR, ({ const char *_x = n; _x; }), __VA_ARGS__
|
||||
#define JSON_BUILD_PAIR_CONDITION(c, n, ...) _JSON_BUILD_PAIR_CONDITION, ({ bool _x = c; _x; }), ({ const char *_x = n; _x; }), __VA_ARGS__
|
||||
#define JSON_BUILD_NULL _JSON_BUILD_NULL
|
||||
#define JSON_BUILD_VARIANT(v) _JSON_BUILD_VARIANT, ({ JsonVariant *_x = v; _x; })
|
||||
#define JSON_BUILD_LITERAL(l) _JSON_BUILD_LITERAL, ({ const char *_x = l; _x; })
|
||||
|
||||
Reference in New Issue
Block a user