static bool lgtd_jsonrpc_extract_and_validate_params_against_schema(void *output, const struct lgtd_jsonrpc_node *schema, int schema_size, const jsmntok_t *tokens, int ntokens, const char *json) { if (!ntokens) { // "params" were omitted, make sure no args were required or that they // are all optional: while (schema_size--) { if (!schema[schema_size].optional) { return false; } } return true; } switch (tokens[0].type) { case JSMN_OBJECT: return lgtd_jsonrpc_extract_values_from_schema_and_dict( output, schema, schema_size, tokens, ntokens, json ); case JSMN_ARRAY: return lgtd_jsonrpc_extract_values_from_schema_and_array( output, schema, schema_size, tokens, ntokens, json ); default: return false; } }
static bool lgtd_jsonrpc_extract_and_validate_params_against_schema(void *output, const struct lgtd_jsonrpc_node *schema, int schema_size, const jsmntok_t *tokens, int ntokens, const char *json) { switch (tokens[0].type) { case JSMN_OBJECT: return lgtd_jsonrpc_extract_values_from_schema_and_dict( output, schema, schema_size, tokens, ntokens, json ); case JSMN_ARRAY: return lgtd_jsonrpc_extract_values_from_schema_and_array( output, schema, schema_size, tokens, ntokens, json ); default: return false; } }
lgtd_jsonrpc_type_object_or_array, true ), LGTD_JSONRPC_NODE( "id", offsetof(struct lgtd_jsonrpc_request, id), -1, lgtd_jsonrpc_type_string_number_or_null, true ) }; bool ok = lgtd_jsonrpc_extract_values_from_schema_and_dict( request, request_schema, LGTD_ARRAY_SIZE(request_schema), tokens, ntokens, json ); if (!ok) { return false; } request->request_ntokens = 1 + 2 + 2; // dict itself + jsonrpc + method if (request->params) { request->request_ntokens += 1 + request->params_ntokens; } if (request->id) { request->request_ntokens += 2; }