Commit c6480ea7 authored by Konrad Sztyber's avatar Konrad Sztyber Committed by Tomasz Zawadzki
Browse files

json: allow decoding arrays to a single buffer



Sometimes, it might be useful to decode a JSON array, but store the
result in a single place.  For instance, one might want to decode a set
of flags passed as an array of strings and store them in a single
uint64_t, e.g.:

static int
decode_flag(const struct spdk_json_val *val, void *out)
{
	uint64_t *flags = out;
	char *flag = NULL;
	int rc = 0;

	spdk_json_decode_string(val, &flag);
	if (strcmp(flag, "foo") == 0) {
		*flags |= FLAG_FOO;
	} else if (strcmp(flag, "bar") == 0) {
		*flags |= FLAG_BAR;
	} else {
		rc = -1;
	}

	free(flag);
	return rc;
}

uint64_t flags = 0;

spdk_json_decode_array(j, decode_flag, &flags, UINT32_MAX, &count, 0);

With this patch, it's possible to do this by passing stride=0 to
spdk_json_decode_array().

Signed-off-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I27b30dec85d3ccef5df1a307798d79b074768a99
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/22020


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
Community-CI: Mellanox Build Bot
parent dc485e01
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -122,6 +122,20 @@ int spdk_json_decode_object(const struct spdk_json_val *values,
			    const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out);
int spdk_json_decode_object_relaxed(const struct spdk_json_val *values,
				    const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out);

/**
 * Decode a JSON array.
 *
 * \param values List of values to decode.
 * \param decode_func Function to use to decode each individual value.
 * \param out Buffer to store decoded value(s).  If `stride` != 0, this buffer is advanced `stride`
 *            bytes for each decoded value.
 * \param out_size Number of decoded values.
 * \param max_size Maximum number of array elements to decode.
 * \param stride Number of bytes to advance `out`.
 *
 * \return 0 on success, -1 on failure.
 */
int spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn decode_func,
			   void *out, size_t max_size, size_t *out_size, size_t stride);

+1 −3
Original line number Diff line number Diff line
@@ -401,7 +401,6 @@ spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn d
{
	uint32_t i;
	char *field;
	char *out_end;

	if (values == NULL || values->type != SPDK_JSON_VAL_ARRAY_BEGIN) {
		return -1;
@@ -409,11 +408,10 @@ spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn d

	*out_size = 0;
	field = out;
	out_end = field + max_size * stride;
	for (i = 0; i < values->len;) {
		const struct spdk_json_val *v = &values[i + 1];

		if (field == out_end) {
		if (*out_size == max_size) {
			return -1;
		}