Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MariaDB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
d68d7e50
Commit
d68d7e50
authored
Nov 28, 2018
by
Sergei Golubchik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
json helpers
parent
a76aadf7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
8 deletions
+87
-8
include/json_lib.h
include/json_lib.h
+5
-1
sql/item_jsonfunc.cc
sql/item_jsonfunc.cc
+1
-7
strings/json_lib.c
strings/json_lib.c
+81
-0
No files found.
include/json_lib.h
View file @
d68d7e50
...
@@ -423,10 +423,14 @@ int json_path_parts_compare(
...
@@ -423,10 +423,14 @@ int json_path_parts_compare(
int
json_path_compare
(
const
json_path_t
*
a
,
const
json_path_t
*
b
,
int
json_path_compare
(
const
json_path_t
*
a
,
const
json_path_t
*
b
,
enum
json_value_types
vt
);
enum
json_value_types
vt
);
int
json_valid
(
const
char
*
js
,
size_t
js_len
,
CHARSET_INFO
*
cs
);
int
json_get_object_by_key
(
const
char
*
js
,
size_t
js_len
,
const
char
*
key
,
size_t
key_len
,
enum
json_value_types
*
value_type
,
const
char
**
value_start
,
size_t
*
value_len
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
#endif
/* JSON_LIB_INCLUDED */
#endif
/* JSON_LIB_INCLUDED */
sql/item_jsonfunc.cc
View file @
d68d7e50
...
@@ -374,17 +374,11 @@ static int path_setup_nwc(json_path_t *p, CHARSET_INFO *i_cs,
...
@@ -374,17 +374,11 @@ static int path_setup_nwc(json_path_t *p, CHARSET_INFO *i_cs,
longlong
Item_func_json_valid
::
val_int
()
longlong
Item_func_json_valid
::
val_int
()
{
{
String
*
js
=
args
[
0
]
->
val_json
(
&
tmp_value
);
String
*
js
=
args
[
0
]
->
val_json
(
&
tmp_value
);
json_engine_t
je
;
if
((
null_value
=
args
[
0
]
->
null_value
))
if
((
null_value
=
args
[
0
]
->
null_value
))
return
0
;
return
0
;
json_scan_start
(
&
je
,
js
->
charset
(),
(
const
uchar
*
)
js
->
ptr
(),
return
json_valid
(
js
->
ptr
(),
js
->
length
(),
js
->
charset
());
(
const
uchar
*
)
js
->
ptr
()
+
js
->
length
());
while
(
json_scan_next
(
&
je
)
==
0
)
{}
return
je
.
s
.
error
==
0
;
}
}
...
...
strings/json_lib.c
View file @
d68d7e50
...
@@ -1877,4 +1877,85 @@ enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey
...
@@ -1877,4 +1877,85 @@ enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey
return
JSV_NOTHING
;
return
JSV_NOTHING
;
}
}
/** Simple json lookup for a value by the key.
Only supports flat json objects.
Does not look inside nested objects.
Does not process complex path expressions.
@param js [in] json to search in
@param js_len [in] - " -
@param key [in] key to search for
@param key_len [in] - " -
@param value_type [out] type of the value found or 0 if not found
@param value_start [out] pointer into js (value or closing })
@param value_len [out] length of the value found or number of keys
@retval 0 - success
@retval 1 - error (invalid json)
*/
int
json_get_object_by_key
(
const
char
*
js
,
size_t
js_len
,
const
char
*
key
,
size_t
key_len
,
enum
json_value_types
*
value_type
,
const
char
**
value_start
,
size_t
*
value_len
)
{
json_engine_t
je
;
json_string_t
key_name
;
int
n_keys
=
0
;
json_string_set_cs
(
&
key_name
,
&
my_charset_utf8mb4_bin
);
json_scan_start
(
&
je
,
&
my_charset_utf8mb4_bin
,(
const
uchar
*
)
js
,
(
const
uchar
*
)
js
+
js_len
);
if
(
json_read_value
(
&
je
)
||
je
.
value_type
!=
JSON_VALUE_OBJECT
)
goto
err_return
;
while
(
!
json_scan_next
(
&
je
))
{
switch
(
je
.
state
)
{
case
JST_KEY
:
n_keys
++
;
json_string_set_str
(
&
key_name
,
(
const
uchar
*
)
key
,
(
const
uchar
*
)
key
+
key_len
);
if
(
!
json_key_matches
(
&
je
,
&
key_name
))
{
if
(
json_skip_key
(
&
je
))
goto
err_return
;
}
else
{
if
(
json_read_value
(
&
je
))
goto
err_return
;
*
value_type
=
je
.
value_type
;
*
value_start
=
(
const
char
*
)
je
.
value
;
*
value_len
=
je
.
value_len
;
return
0
;
}
break
;
case
JST_OBJ_END
:
*
value_type
=
(
enum
json_value_types
)
0
;
*
value_start
=
(
const
char
*
)
(
je
.
s
.
c_str
-
je
.
sav_c_len
);
*
value_len
=
n_keys
;
return
0
;
}
}
err_return:
return
1
;
}
/** Check if json is valid (well-formed)
@retval 0 - success, json is well-formed
@retval 1 - error, json is invalid
*/
int
json_valid
(
const
char
*
js
,
size_t
js_len
,
CHARSET_INFO
*
cs
)
{
json_engine_t
je
;
json_scan_start
(
&
je
,
cs
,
(
const
uchar
*
)
js
,
(
const
uchar
*
)
js
+
js_len
);
while
(
json_scan_next
(
&
je
)
==
0
)
/* no-op */
;
return
je
.
s
.
error
==
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment