Commit fb2edab3 authored by Michael Okoko's avatar Michael Okoko Committed by Sergei Petrunia

Extract json parser functions from class

Signed-off-by: default avatarMichael Okoko <okokomichaels@outlook.com>
parent 6bc2df5f
...@@ -283,13 +283,13 @@ int json_key_matches(json_engine_t *je, json_string_t *k); ...@@ -283,13 +283,13 @@ int json_key_matches(json_engine_t *je, json_string_t *k);
int json_read_value(json_engine_t *j); int json_read_value(json_engine_t *j);
/* /*
* smart_read_value() reads parses a scalar value and value length from the json engine, * json_smart_read_value() reads parses a scalar value and value length from the json engine,
* and copies them into `value` and `value_length` respectively. * and copies them into `value` and `value_length` respectively.
* It should only be called when the json_engine state is JST_VALUE. * It should only be called when the json_engine state is JST_VALUE.
* If it encounters a non-scalar value (say object or array) before getting to value_len, * If it encounters a non-scalar value (say object or array) before getting to value_len,
* such value is also read and copied into value. * such value is also read and copied into value.
*/ */
enum json_types smart_read_value(json_engine_t *je, const char **value, int *value_len); enum json_types json_smart_read_value(json_engine_t *je, const char **value, int *value_len);
/* /*
json_skip_key() makes parser skip the content of the current json_skip_key() makes parser skip the content of the current
......
...@@ -63,6 +63,19 @@ ...@@ -63,6 +63,19 @@
equal to "never". equal to "never".
*/ */
/*
* json_get_array_items expects a JSON array as argument,
* and pushes the elements of the array into the `container` vector.
* It only works if all the elements in the original JSON array
* are scalar values (i.e., strings, numbers, true or false), and returns JSV_BAD_JSON if:
* the original JSON is not an array OR the JSON array contains non-scalar elements.
*/
bool json_get_array_items(const char *json, const char *json_end, int *value_type, std::vector<std::string> &container);
std::vector<std::string> parse_histogram_from_json(const char *json);
void test_parse_histogram_from_json();
/* Currently there are only 3 persistent statistical tables */ /* Currently there are only 3 persistent statistical tables */
static const uint STATISTICS_TABLES= 3; static const uint STATISTICS_TABLES= 3;
...@@ -1651,19 +1664,10 @@ std::vector<std::string> bucket_bounds = {}; ...@@ -1651,19 +1664,10 @@ std::vector<std::string> bucket_bounds = {};
test_parse_histogram_from_json(); test_parse_histogram_from_json();
} }
};
static std::vector<std::string> parse_histogram_from_json(const char *json) void test_parse_histogram_from_json()
{ {
std::vector<std::string> hist_buckets= {};
enum json_types vt = json_get_array_items(json, json + strlen(json), hist_buckets);
printf("%d", vt);
printf("%zu", hist_buckets.size());
return hist_buckets;
}
static void test_parse_histogram_from_json()
{
std::vector<std::string> bucket = {}; std::vector<std::string> bucket = {};
std::string json; std::string json;
std::string tests[7] = { std::string tests[7] = {
...@@ -1679,20 +1683,26 @@ std::vector<std::string> bucket_bounds = {}; ...@@ -1679,20 +1683,26 @@ std::vector<std::string> bucket_bounds = {};
for(const auto& test : tests) { for(const auto& test : tests) {
json = test; json = test;
bucket = parse_histogram_from_json(json.c_str()); bucket = parse_histogram_from_json(json.c_str());
printf("%zu", bucket.size());
}
} }
}
/* std::vector<std::string> parse_histogram_from_json(const char *json)
* json_get_array_items expects a JSON array as argument, {
* and pushes the elements of the array into the `container` vector. std::vector<std::string> hist_buckets= {};
* It only works if all the elements in the original JSON array int vt;
* are scalar values (i.e., strings, numbers, true or false), and returns JSV_BAD_JSON if: bool result = json_get_array_items(json, json + strlen(json), &vt, hist_buckets);
* the original JSON is not an array OR the JSON array contains non-scalar elements. fprintf(stderr,"==============\n");
*/ fprintf(stderr,"histogram: %s\n", json);
static json_types json_get_array_items(const char *json, const char *json_end, std::vector<std::string> &container) { fprintf(stderr, "json_get_array_items() returned %s\n", result ? "true" : "false");
fprintf(stderr, "value type after json_get_array_items() is %d\n", vt);
fprintf(stderr, " JSV_BAD_JSON=%d, JSON_VALUE_ARRAY=%d\n", (int)JSV_BAD_JSON, (int)JSON_VALUE_ARRAY);
fprintf(stderr, "hist_buckets.size()=%zu\n", hist_buckets.size());
return hist_buckets;
}
bool json_get_array_items(const char *json, const char *json_end, int *value_type, std::vector<std::string> &container) {
json_engine_t je; json_engine_t je;
enum json_types value_type;
int vl; int vl;
const char *v; const char *v;
...@@ -1700,9 +1710,10 @@ std::vector<std::string> bucket_bounds = {}; ...@@ -1700,9 +1710,10 @@ std::vector<std::string> bucket_bounds = {};
if (json_read_value(&je) || je.value_type != JSON_VALUE_ARRAY) if (json_read_value(&je) || je.value_type != JSON_VALUE_ARRAY)
{ {
return JSV_BAD_JSON; *value_type = JSV_BAD_JSON;
return false;
} }
value_type = static_cast<json_types>(je.value_type); *value_type = je.value_type;
std::string val; std::string val;
while(!json_scan_next(&je)) while(!json_scan_next(&je))
...@@ -1710,14 +1721,15 @@ std::vector<std::string> bucket_bounds = {}; ...@@ -1710,14 +1721,15 @@ std::vector<std::string> bucket_bounds = {};
switch(je.state) switch(je.state)
{ {
case JST_VALUE: case JST_VALUE:
*value_type = json_smart_read_value(&je, &v, &vl);
if (je.value_type != JSON_VALUE_STRING && if (je.value_type != JSON_VALUE_STRING &&
je.value_type != JSON_VALUE_NUMBER && je.value_type != JSON_VALUE_NUMBER &&
je.value_type != JSON_VALUE_TRUE && je.value_type != JSON_VALUE_TRUE &&
je.value_type != JSON_VALUE_FALSE) je.value_type != JSON_VALUE_FALSE)
{ {
return JSV_BAD_JSON; *value_type = JSV_BAD_JSON;
return false;
} }
value_type = smart_read_value(&je, &v, &vl);
val = std::string(v, vl); val = std::string(v, vl);
container.emplace_back(val); container.emplace_back(val);
case JST_ARRAY_END: case JST_ARRAY_END:
...@@ -1725,9 +1737,8 @@ std::vector<std::string> bucket_bounds = {}; ...@@ -1725,9 +1737,8 @@ std::vector<std::string> bucket_bounds = {};
} }
} }
return value_type; return true;
} }
};
C_MODE_START C_MODE_START
......
...@@ -1868,7 +1868,7 @@ int json_path_compare(const json_path_t *a, const json_path_t *b, ...@@ -1868,7 +1868,7 @@ int json_path_compare(const json_path_t *a, const json_path_t *b,
} }
enum json_types smart_read_value(json_engine_t *je, enum json_types json_smart_read_value(json_engine_t *je,
const char **value, int *value_len) const char **value, int *value_len)
{ {
if (json_read_value(je)) if (json_read_value(je))
...@@ -1909,7 +1909,7 @@ enum json_types json_type(const char *js, const char *js_end, ...@@ -1909,7 +1909,7 @@ enum json_types json_type(const char *js, const char *js_end,
json_scan_start(&je, &my_charset_utf8mb4_bin,(const uchar *) js, json_scan_start(&je, &my_charset_utf8mb4_bin,(const uchar *) js,
(const uchar *) js_end); (const uchar *) js_end);
return smart_read_value(&je, value, value_len); return json_smart_read_value(&je, value, value_len);
} }
...@@ -1933,7 +1933,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -1933,7 +1933,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
{ {
case JST_VALUE: case JST_VALUE:
if (c_item == n_item) if (c_item == n_item)
return smart_read_value(&je, value, value_len); return json_smart_read_value(&je, value, value_len);
if (json_skip_key(&je)) if (json_skip_key(&je))
goto err_return; goto err_return;
...@@ -1997,7 +1997,7 @@ enum json_types json_get_object_key(const char *js, const char *js_end, ...@@ -1997,7 +1997,7 @@ enum json_types json_get_object_key(const char *js, const char *js_end,
json_string_set_str(&key_name, (const uchar *) key, json_string_set_str(&key_name, (const uchar *) key,
(const uchar *) key_end); (const uchar *) key_end);
if (json_key_matches(&je, &key_name)) if (json_key_matches(&je, &key_name))
return smart_read_value(&je, value, value_len); return json_smart_read_value(&je, value, value_len);
if (json_skip_key(&je)) if (json_skip_key(&je))
goto err_return; goto err_return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment