json: new relaxed decode object function

With new funtion it is allowed to successfully parse json values even
if doceder for given key is not found.

Signed-off-by: Jacek Kalwas <jacek.kalwas@intel.com>
Change-Id: I036f263e9050bd2b96aaa3ff61a9542c98365892
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/4340
Community-CI: Broadcom CI
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Jacek Kalwas 2020-09-22 20:27:43 +02:00 committed by Tomasz Zawadzki
parent de016f2756
commit 90472b8663
4 changed files with 22 additions and 5 deletions

View File

@ -144,6 +144,8 @@ struct spdk_json_object_decoder {
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);
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);

View File

@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
SO_VER := 2
SO_MINOR := 0
SO_MINOR := 1
C_SRCS = json_parse.c json_util.c json_write.c
LIBNAME = json

View File

@ -298,9 +298,9 @@ spdk_json_number_to_uint64(const struct spdk_json_val *val, uint64_t *num)
return 0;
}
int
spdk_json_decode_object(const struct spdk_json_val *values,
const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out)
static int
_json_decode_object(const struct spdk_json_val *values,
const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out, bool relaxed)
{
uint32_t i;
bool invalid = false;
@ -344,7 +344,7 @@ spdk_json_decode_object(const struct spdk_json_val *values,
}
}
if (!found) {
if (!relaxed && !found) {
invalid = true;
SPDK_JSON_DEBUG("Decoder not found for key '%.*s'\n", name->len, (char *)name->start);
}
@ -364,6 +364,20 @@ spdk_json_decode_object(const struct spdk_json_val *values,
return invalid ? -1 : 0;
}
int
spdk_json_decode_object(const struct spdk_json_val *values,
const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out)
{
return _json_decode_object(values, decoders, num_decoders, out, false);
}
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)
{
return _json_decode_object(values, decoders, num_decoders, out, true);
}
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)

View File

@ -4,6 +4,7 @@
# public functions
spdk_json_parse;
spdk_json_decode_object;
spdk_json_decode_object_relaxed;
spdk_json_decode_array;
spdk_json_decode_bool;
spdk_json_decode_uint16;