Skip to content

Commit fb2edab

Browse files
idoqospetrunia
authored andcommitted
Extract json parser functions from class
Signed-off-by: Michael Okoko <okokomichaels@outlook.com>
1 parent 6bc2df5 commit fb2edab

File tree

3 files changed

+83
-72
lines changed

3 files changed

+83
-72
lines changed

include/json_lib.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ int json_key_matches(json_engine_t *je, json_string_t *k);
283283
int json_read_value(json_engine_t *j);
284284

285285
/*
286-
* smart_read_value() reads parses a scalar value and value length from the json engine,
286+
* json_smart_read_value() reads parses a scalar value and value length from the json engine,
287287
* and copies them into `value` and `value_length` respectively.
288288
* It should only be called when the json_engine state is JST_VALUE.
289289
* If it encounters a non-scalar value (say object or array) before getting to value_len,
290290
* such value is also read and copied into value.
291291
*/
292-
enum json_types smart_read_value(json_engine_t *je, const char **value, int *value_len);
292+
enum json_types json_smart_read_value(json_engine_t *je, const char **value, int *value_len);
293293

294294
/*
295295
json_skip_key() makes parser skip the content of the current

sql/sql_statistics.cc

Lines changed: 77 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,21 @@
6161
the collected statistics in the persistent statistical tables only
6262
when the value of the variable 'use_stat_tables' is not
6363
equal to "never".
64-
*/
65-
64+
*/
65+
66+
/*
67+
* json_get_array_items expects a JSON array as argument,
68+
* and pushes the elements of the array into the `container` vector.
69+
* It only works if all the elements in the original JSON array
70+
* are scalar values (i.e., strings, numbers, true or false), and returns JSV_BAD_JSON if:
71+
* the original JSON is not an array OR the JSON array contains non-scalar elements.
72+
*/
73+
bool json_get_array_items(const char *json, const char *json_end, int *value_type, std::vector<std::string> &container);
74+
75+
std::vector<std::string> parse_histogram_from_json(const char *json);
76+
77+
void test_parse_histogram_from_json();
78+
6679
/* Currently there are only 3 persistent statistical tables */
6780
static const uint STATISTICS_TABLES= 3;
6881

@@ -1651,83 +1664,81 @@ std::vector<std::string> bucket_bounds = {};
16511664

16521665
test_parse_histogram_from_json();
16531666
}
1667+
};
16541668

1655-
static std::vector<std::string> parse_histogram_from_json(const char *json)
1656-
{
1657-
std::vector<std::string> hist_buckets= {};
1658-
enum json_types vt = json_get_array_items(json, json + strlen(json), hist_buckets);
1659-
printf("%d", vt);
1660-
printf("%zu", hist_buckets.size());
1669+
void test_parse_histogram_from_json()
1670+
{
1671+
std::vector<std::string> bucket = {};
1672+
std::string json;
1673+
std::string tests[7] = {
1674+
R"(["aabbb", "ccccdd", "eeefff"])",
1675+
R"(["aabbb", "ccc{}dd", "eeefff"])",
1676+
R"(["aabbb", {"a": "b"}, "eeefff"])",
1677+
R"({})",
1678+
R"([1,2,3, null])",
1679+
R"([null])",
1680+
R"([])"
1681+
};
16611682

1662-
return hist_buckets;
1683+
for(const auto& test : tests) {
1684+
json = test;
1685+
bucket = parse_histogram_from_json(json.c_str());
16631686
}
1687+
}
16641688

1665-
static void test_parse_histogram_from_json()
1666-
{
1667-
std::vector<std::string> bucket = {};
1668-
std::string json;
1669-
std::string tests[7] = {
1670-
R"(["aabbb", "ccccdd", "eeefff"])",
1671-
R"(["aabbb", "ccc{}dd", "eeefff"])",
1672-
R"(["aabbb", {"a": "b"}, "eeefff"])",
1673-
R"({})",
1674-
R"([1,2,3, null])",
1675-
R"([null])",
1676-
R"([])"
1677-
};
1678-
1679-
for(const auto& test : tests) {
1680-
json = test;
1681-
bucket = parse_histogram_from_json(json.c_str());
1682-
printf("%zu", bucket.size());
1683-
}
1684-
}
1689+
std::vector<std::string> parse_histogram_from_json(const char *json)
1690+
{
1691+
std::vector<std::string> hist_buckets= {};
1692+
int vt;
1693+
bool result = json_get_array_items(json, json + strlen(json), &vt, hist_buckets);
1694+
fprintf(stderr,"==============\n");
1695+
fprintf(stderr,"histogram: %s\n", json);
1696+
fprintf(stderr, "json_get_array_items() returned %s\n", result ? "true" : "false");
1697+
fprintf(stderr, "value type after json_get_array_items() is %d\n", vt);
1698+
fprintf(stderr, " JSV_BAD_JSON=%d, JSON_VALUE_ARRAY=%d\n", (int)JSV_BAD_JSON, (int)JSON_VALUE_ARRAY);
1699+
fprintf(stderr, "hist_buckets.size()=%zu\n", hist_buckets.size());
1700+
1701+
return hist_buckets;
1702+
}
16851703

1686-
/*
1687-
* json_get_array_items expects a JSON array as argument,
1688-
* and pushes the elements of the array into the `container` vector.
1689-
* It only works if all the elements in the original JSON array
1690-
* are scalar values (i.e., strings, numbers, true or false), and returns JSV_BAD_JSON if:
1691-
* the original JSON is not an array OR the JSON array contains non-scalar elements.
1692-
*/
1693-
static json_types json_get_array_items(const char *json, const char *json_end, std::vector<std::string> &container) {
1694-
json_engine_t je;
1695-
enum json_types value_type;
1696-
int vl;
1697-
const char *v;
1704+
bool json_get_array_items(const char *json, const char *json_end, int *value_type, std::vector<std::string> &container) {
1705+
json_engine_t je;
1706+
int vl;
1707+
const char *v;
16981708

1699-
json_scan_start(&je, &my_charset_utf8mb4_bin, (const uchar *)json, (const uchar *)json_end);
1709+
json_scan_start(&je, &my_charset_utf8mb4_bin, (const uchar *)json, (const uchar *)json_end);
17001710

1701-
if (json_read_value(&je) || je.value_type != JSON_VALUE_ARRAY)
1702-
{
1703-
return JSV_BAD_JSON;
1704-
}
1705-
value_type = static_cast<json_types>(je.value_type);
1711+
if (json_read_value(&je) || je.value_type != JSON_VALUE_ARRAY)
1712+
{
1713+
*value_type = JSV_BAD_JSON;
1714+
return false;
1715+
}
1716+
*value_type = je.value_type;
17061717

1707-
std::string val;
1708-
while(!json_scan_next(&je))
1718+
std::string val;
1719+
while(!json_scan_next(&je))
1720+
{
1721+
switch(je.state)
17091722
{
1710-
switch(je.state)
1723+
case JST_VALUE:
1724+
*value_type = json_smart_read_value(&je, &v, &vl);
1725+
if (je.value_type != JSON_VALUE_STRING &&
1726+
je.value_type != JSON_VALUE_NUMBER &&
1727+
je.value_type != JSON_VALUE_TRUE &&
1728+
je.value_type != JSON_VALUE_FALSE)
17111729
{
1712-
case JST_VALUE:
1713-
if (je.value_type != JSON_VALUE_STRING &&
1714-
je.value_type != JSON_VALUE_NUMBER &&
1715-
je.value_type != JSON_VALUE_TRUE &&
1716-
je.value_type != JSON_VALUE_FALSE)
1717-
{
1718-
return JSV_BAD_JSON;
1719-
}
1720-
value_type = smart_read_value(&je, &v, &vl);
1721-
val = std::string(v, vl);
1722-
container.emplace_back(val);
1723-
case JST_ARRAY_END:
1724-
break;
1730+
*value_type = JSV_BAD_JSON;
1731+
return false;
17251732
}
1733+
val = std::string(v, vl);
1734+
container.emplace_back(val);
1735+
case JST_ARRAY_END:
1736+
break;
17261737
}
1727-
1728-
return value_type;
17291738
}
1730-
};
1739+
1740+
return true;
1741+
}
17311742

17321743
C_MODE_START
17331744

strings/json_lib.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ int json_path_compare(const json_path_t *a, const json_path_t *b,
18681868
}
18691869

18701870

1871-
enum json_types smart_read_value(json_engine_t *je,
1871+
enum json_types json_smart_read_value(json_engine_t *je,
18721872
const char **value, int *value_len)
18731873
{
18741874
if (json_read_value(je))
@@ -1909,7 +1909,7 @@ enum json_types json_type(const char *js, const char *js_end,
19091909
json_scan_start(&je, &my_charset_utf8mb4_bin,(const uchar *) js,
19101910
(const uchar *) js_end);
19111911

1912-
return smart_read_value(&je, value, value_len);
1912+
return json_smart_read_value(&je, value, value_len);
19131913
}
19141914

19151915

@@ -1933,7 +1933,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
19331933
{
19341934
case JST_VALUE:
19351935
if (c_item == n_item)
1936-
return smart_read_value(&je, value, value_len);
1936+
return json_smart_read_value(&je, value, value_len);
19371937

19381938
if (json_skip_key(&je))
19391939
goto err_return;
@@ -1997,7 +1997,7 @@ enum json_types json_get_object_key(const char *js, const char *js_end,
19971997
json_string_set_str(&key_name, (const uchar *) key,
19981998
(const uchar *) key_end);
19991999
if (json_key_matches(&je, &key_name))
2000-
return smart_read_value(&je, value, value_len);
2000+
return json_smart_read_value(&je, value, value_len);
20012001

20022002
if (json_skip_key(&je))
20032003
goto err_return;

0 commit comments

Comments
 (0)