Json Utilities
-
typedef gboolean (*WpRuleMatchCallback)(gpointer data, const gchar *action, WpSpaJson *value, GError **error)
A function to be called by wp_json_utils_match_rules() when a match is found.
- Param data:
(closure): the user data passed to wp_json_utils_match_rules()
- Param action:
the rule's action string
- Param value:
the value associated with this action
- Param error:
(out): return location for a GError
- Return:
FALSE if an error occurred and the match process should stop, TRUE otherwise
-
gboolean wp_json_utils_match_rules(WpSpaJson *json, WpProperties *match_props, WpRuleMatchCallback callback, gpointer data, GError **error)
Matches the given properties against a set of rules described in JSON and calls the given callback to perform actions on a successful match.
The given JSON should be an array of objects, where each object has a "matches" and an "actions" property. The "matches" value should also be an array of objects, where each object is a set of properties to match. Inside such an object, all properties must match to consider a successful match. However, if multiple objects are provided, only one object needs to match.
The "actions" value should be an object where the key is the action name and the value can be any valid JSON. Both the action name and the value are passed as-is on the callback.
* [ * { * matches = [ * # any of the items in matches needs to match, if one does, * # actions are emitted. * { * # all keys must match the value. ! negates. ~ starts regex. * <key> = <value> * ... * } * ... * ] * actions = { * <action> = <value> * ... * } * } * ] *
- Parameters:
json -- a JSON array containing rules in the described format
match_props -- (transfer none): the properties to match against the rules
callback -- (scope call)(closure data): a function to call for each action on a successful match
data -- data to be passed to callback
error -- (out)(optional): the error that occurred, if any
- Returns:
FALSE if an error occurred, TRUE otherwise
-
gint wp_json_utils_match_rules_update_properties(WpSpaJson *json, WpProperties *props)
Matches the given properties against a set of rules described in JSON and updates the properties if the rule actions include the "update-props" action.
- Parameters:
json -- a JSON array containing rules in the format accepted by wp_json_utils_match_rules()
props -- (transfer none): the properties to match against the rules and also update, acting on the "update-props" action
- Returns:
the number of properties that were updated
-
WpSpaJson *wp_json_utils_merge_containers(WpSpaJson *a, WpSpaJson *b)
Merges two JSON containers (objects or arrays) into one.
If both a and b are objects, the result will be a new object containing all properties from both a and b. If a property exists in both a and b, the values are recursively merged. If a property exists in both a and b and the property name starts with the "override." prefix in either of those, the value from the key with the prefix is used.
If both a and b are arrays, the result will be a new array containing all elements from both a and b.
If a and b are not of the same type, NULL is returned.
- Parameters:
a -- (transfer none): a JSON container
b -- (transfer none): a JSON container
- Returns:
a new JSON container containing the merged contents of a and b or NULL if a and b are not of the same type