mirror of
https://github.com/MariaDB/server.git
synced 2025-12-28 08:10:14 +00:00
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.
This commit is contained in:
parent
809e6f4195
commit
f4318f3f3b
@ -19,7 +19,7 @@
|
||||
#include "my_json_writer.h"
|
||||
#include "sql_statistics.h"
|
||||
#include "opt_histogram_json.h"
|
||||
|
||||
#include "sql_json_lib.h"
|
||||
|
||||
/*
|
||||
@brief
|
||||
@ -31,7 +31,7 @@
|
||||
succeeds.
|
||||
*/
|
||||
|
||||
static bool json_unescape_to_string(const char *val, int val_len, String* out)
|
||||
bool json_unescape_to_string(const char *val, int val_len, String* out)
|
||||
{
|
||||
// Make sure 'out' has some memory allocated.
|
||||
if (!out->alloced_length() && out->alloc(128))
|
||||
@ -416,64 +416,6 @@ void Histogram_json_hb::init_for_collection(MEM_ROOT *mem_root,
|
||||
size= (size_t)size_arg;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
A syntax sugar interface to json_string_t
|
||||
*/
|
||||
class Json_string
|
||||
{
|
||||
json_string_t str;
|
||||
public:
|
||||
explicit Json_string(const char *name)
|
||||
{
|
||||
json_string_set_str(&str, (const uchar*)name,
|
||||
(const uchar*)name + strlen(name));
|
||||
json_string_set_cs(&str, system_charset_info);
|
||||
}
|
||||
json_string_t *get() { return &str; }
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
This [partially] saves the JSON parser state and then can rollback the parser
|
||||
to it.
|
||||
|
||||
The goal of this is to be able to make multiple json_key_matches() calls:
|
||||
|
||||
Json_saved_parser_state save(je);
|
||||
if (json_key_matches(je, KEY_NAME_1)) {
|
||||
...
|
||||
return;
|
||||
}
|
||||
save.restore_to(je);
|
||||
if (json_key_matches(je, KEY_NAME_2)) {
|
||||
...
|
||||
}
|
||||
|
||||
This allows one to parse JSON objects where [optional] members come in any
|
||||
order.
|
||||
*/
|
||||
|
||||
class Json_saved_parser_state
|
||||
{
|
||||
const uchar *c_str;
|
||||
my_wc_t c_next;
|
||||
int state;
|
||||
public:
|
||||
explicit Json_saved_parser_state(const json_engine_t *je) :
|
||||
c_str(je->s.c_str),
|
||||
c_next(je->s.c_next),
|
||||
state(je->state)
|
||||
{}
|
||||
void restore_to(json_engine_t *je)
|
||||
{
|
||||
je->s.c_str= c_str;
|
||||
je->s.c_next= c_next;
|
||||
je->state= state;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
@brief
|
||||
Read a constant from JSON document and save it in *out.
|
||||
|
||||
76
sql/sql_json_lib.h
Normal file
76
sql/sql_json_lib.h
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
Copyright (c) 2025, MariaDB
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; version 2 of the License.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335
|
||||
USA */
|
||||
|
||||
#ifndef SQL_JSON_LIB
|
||||
#define SQL_JSON_LIB
|
||||
|
||||
/*
|
||||
A syntax sugar interface to json_string_t
|
||||
*/
|
||||
class Json_string
|
||||
{
|
||||
json_string_t str;
|
||||
|
||||
public:
|
||||
explicit Json_string(const char *name)
|
||||
{
|
||||
json_string_set_str(&str, (const uchar *) name,
|
||||
(const uchar *) name + strlen(name));
|
||||
json_string_set_cs(&str, system_charset_info);
|
||||
}
|
||||
json_string_t *get() { return &str; }
|
||||
};
|
||||
|
||||
/*
|
||||
This [partially] saves the JSON parser state and then can rollback the parser
|
||||
to it.
|
||||
The goal of this is to be able to make multiple json_key_matches() calls:
|
||||
Json_saved_parser_state save(je);
|
||||
if (json_key_matches(je, KEY_NAME_1)) {
|
||||
...
|
||||
return;
|
||||
}
|
||||
save.restore_to(je);
|
||||
if (json_key_matches(je, KEY_NAME_2)) {
|
||||
...
|
||||
}
|
||||
This allows one to parse JSON objects where [optional] members come in any
|
||||
order.
|
||||
*/
|
||||
class Json_saved_parser_state
|
||||
{
|
||||
const uchar *c_str;
|
||||
my_wc_t c_next;
|
||||
int state;
|
||||
|
||||
public:
|
||||
explicit Json_saved_parser_state(const json_engine_t *je)
|
||||
: c_str(je->s.c_str), c_next(je->s.c_next), state(je->state)
|
||||
{
|
||||
}
|
||||
void restore_to(json_engine_t *je)
|
||||
{
|
||||
je->s.c_str= c_str;
|
||||
je->s.c_next= c_next;
|
||||
je->state= state;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
@brief
|
||||
Un-escape a JSON string and save it into *out.
|
||||
*/
|
||||
bool json_unescape_to_string(const char *val, int val_len, String *out);
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue
Block a user