Skip to content

Commit c77eaf2

Browse files
MDEV-38120: Move Json_string and Json_saved_parser_state into sql_json_lib.h
Move classes Json_string, and Json_saved_parser_state from opt_histogram_json.cc to the newly created file sql_json_lib.h Additionally, make the function json_unescape_to_string public earlier defined as static in opt_histogram_json.cc, by adding the declaration in sql_json_lib.h Needed this change so that, the classes and functions can be reused in other pieces of the code.
1 parent fd15fd2 commit c77eaf2

File tree

2 files changed

+78
-60
lines changed

2 files changed

+78
-60
lines changed

sql/opt_histogram_json.cc

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "my_json_writer.h"
2020
#include "sql_statistics.h"
2121
#include "opt_histogram_json.h"
22-
22+
#include "sql_json_lib.h"
2323

2424
/*
2525
@brief
@@ -31,7 +31,7 @@
3131
succeeds.
3232
*/
3333

34-
static bool json_unescape_to_string(const char *val, int val_len, String* out)
34+
bool json_unescape_to_string(const char *val, int val_len, String* out)
3535
{
3636
// Make sure 'out' has some memory allocated.
3737
if (!out->alloced_length() && out->alloc(128))
@@ -416,64 +416,6 @@ void Histogram_json_hb::init_for_collection(MEM_ROOT *mem_root,
416416
size= (size_t)size_arg;
417417
}
418418

419-
420-
/*
421-
A syntax sugar interface to json_string_t
422-
*/
423-
class Json_string
424-
{
425-
json_string_t str;
426-
public:
427-
explicit Json_string(const char *name)
428-
{
429-
json_string_set_str(&str, (const uchar*)name,
430-
(const uchar*)name + strlen(name));
431-
json_string_set_cs(&str, system_charset_info);
432-
}
433-
json_string_t *get() { return &str; }
434-
};
435-
436-
437-
/*
438-
This [partially] saves the JSON parser state and then can rollback the parser
439-
to it.
440-
441-
The goal of this is to be able to make multiple json_key_matches() calls:
442-
443-
Json_saved_parser_state save(je);
444-
if (json_key_matches(je, KEY_NAME_1)) {
445-
...
446-
return;
447-
}
448-
save.restore_to(je);
449-
if (json_key_matches(je, KEY_NAME_2)) {
450-
...
451-
}
452-
453-
This allows one to parse JSON objects where [optional] members come in any
454-
order.
455-
*/
456-
457-
class Json_saved_parser_state
458-
{
459-
const uchar *c_str;
460-
my_wc_t c_next;
461-
int state;
462-
public:
463-
explicit Json_saved_parser_state(const json_engine_t *je) :
464-
c_str(je->s.c_str),
465-
c_next(je->s.c_next),
466-
state(je->state)
467-
{}
468-
void restore_to(json_engine_t *je)
469-
{
470-
je->s.c_str= c_str;
471-
je->s.c_next= c_next;
472-
je->state= state;
473-
}
474-
};
475-
476-
477419
/*
478420
@brief
479421
Read a constant from JSON document and save it in *out.

sql/sql_json_lib.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright (c) 2025, MariaDB
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation; version 2 of the License.
6+
This program is distributed in the hope that it will be useful,
7+
but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9+
GNU General Public License for more details.
10+
You should have received a copy of the GNU General Public License
11+
along with this program; if not, write to the Free Software
12+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335
13+
USA */
14+
15+
#ifndef SQL_JSON_LIB
16+
#define SQL_JSON_LIB
17+
18+
/*
19+
A syntax sugar interface to json_string_t
20+
*/
21+
class Json_string
22+
{
23+
json_string_t str;
24+
25+
public:
26+
explicit Json_string(const char *name)
27+
{
28+
json_string_set_str(&str, (const uchar *) name,
29+
(const uchar *) name + strlen(name));
30+
json_string_set_cs(&str, system_charset_info);
31+
}
32+
json_string_t *get() { return &str; }
33+
};
34+
35+
/*
36+
This [partially] saves the JSON parser state and then can rollback the parser
37+
to it.
38+
The goal of this is to be able to make multiple json_key_matches() calls:
39+
Json_saved_parser_state save(je);
40+
if (json_key_matches(je, KEY_NAME_1)) {
41+
...
42+
return;
43+
}
44+
save.restore_to(je);
45+
if (json_key_matches(je, KEY_NAME_2)) {
46+
...
47+
}
48+
This allows one to parse JSON objects where [optional] members come in any
49+
order.
50+
*/
51+
class Json_saved_parser_state
52+
{
53+
const uchar *c_str;
54+
my_wc_t c_next;
55+
int state;
56+
57+
public:
58+
explicit Json_saved_parser_state(const json_engine_t *je)
59+
: c_str(je->s.c_str), c_next(je->s.c_next), state(je->state)
60+
{
61+
}
62+
void restore_to(json_engine_t *je)
63+
{
64+
je->s.c_str= c_str;
65+
je->s.c_next= c_next;
66+
je->state= state;
67+
}
68+
};
69+
70+
/*
71+
@brief
72+
Un-escape a JSON string and save it into *out.
73+
*/
74+
bool json_unescape_to_string(const char *val, int val_len, String *out);
75+
76+
#endif

0 commit comments

Comments
 (0)