Skip to content

Commit ddda2cd

Browse files
committed
fix: parse json path with double quote
1 parent 873844b commit ddda2cd

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/common/jsonb/src/functions.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use super::value::Value;
3131
/// Build `JSONB` array from items.
3232
/// Assuming that the input values is valid JSONB data.
3333
pub fn build_array<'a>(
34-
items: impl IntoIterator<Item = &'a [u8]>,
34+
items: impl IntoIterator<Item=&'a [u8]>,
3535
buf: &mut Vec<u8>,
3636
) -> Result<(), Error> {
3737
let start = buf.len();
@@ -69,7 +69,7 @@ pub fn build_array<'a>(
6969
/// Build `JSONB` object from items.
7070
/// Assuming that the input values is valid JSONB data.
7171
pub fn build_object<'a, K: AsRef<str>>(
72-
items: impl IntoIterator<Item = (K, &'a [u8])>,
72+
items: impl IntoIterator<Item=(K, &'a [u8])>,
7373
buf: &mut Vec<u8>,
7474
) -> Result<(), Error> {
7575
let start = buf.len();
@@ -836,6 +836,25 @@ pub fn parse_json_path(path: &[u8]) -> Result<Vec<JsonPath>, Error> {
836836
return Err(Error::InvalidToken);
837837
}
838838
}
839+
} else if c == b'"' {
840+
prev_idx = idx;
841+
loop {
842+
let c = read_char(path, &mut idx)?;
843+
if c == b'\\' {
844+
idx += 1;
845+
let c = read_char(path, &mut idx)?;
846+
if c == b'"' {
847+
idx += 1;
848+
}
849+
} else if c != b'"' {
850+
idx += 1;
851+
} else {
852+
break;
853+
}
854+
}
855+
let s = std::str::from_utf8(&path[prev_idx..idx-1])?;
856+
let json_path = JsonPath::String(Cow::Borrowed(s));
857+
json_paths.push(json_path);
839858
} else {
840859
if c == b':' || c == b'.' {
841860
if idx == 1 {

src/common/jsonb/tests/it/functions.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ fn test_parse_json_path() {
361361
JsonPath::String(Cow::from("k2")),
362362
JsonPath::String(Cow::from("k3")),
363363
]),
364+
("\"k1\"", vec![JsonPath::String(Cow::from("k1")),
365+
]),
366+
("\"k1k2\"", vec![JsonPath::String(Cow::from("k1k2")),
367+
]),
364368
(r#"k1["k2"][1]"#, vec![
365369
JsonPath::String(Cow::from("k1")),
366370
JsonPath::String(Cow::from("k2")),
@@ -413,7 +417,7 @@ fn test_as_type() {
413417

414418
let mut buf: Vec<u8> = Vec::new();
415419
for (s, expect_null, expect_bool, expect_number, expect_str, expect_array, expect_object) in
416-
sources
420+
sources
417421
{
418422
let res = as_null(s.as_bytes());
419423
match expect_null {

0 commit comments

Comments
 (0)