Skip to content

Commit ab50b6d

Browse files
committed
Refactor: Implements serde::de::Deserializer and serde::ser::Serializer for RawJsonb
1 parent 7694b92 commit ab50b6d

File tree

17 files changed

+1893
-657
lines changed

17 files changed

+1893
-657
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ byteorder = "1.5.0"
3030
fast-float2 = "0.2.3"
3131
itoa = "1.0"
3232
nom = "7.1.3"
33+
num-traits = "0.2.19"
3334
ordered-float = { version = "4.5", default-features = false }
3435
rand = { version = "0.8.5", features = ["small_rng"] }
3536
ryu = "1.0"
37+
serde = "1.0"
3638
serde_json = { version = "1.0", default-features = false, features = ["std"] }
3739

3840
[dev-dependencies]
@@ -44,7 +46,9 @@ mockalloc = "0.1.2"
4446
criterion = "0.5.1"
4547

4648
[features]
47-
default = ["serde_json/preserve_order"]
49+
default = ["databend", "serde_json/preserve_order"]
50+
databend = []
51+
sqlite = []
4852

4953
[[bench]]
5054
name = "parser"

src/de.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use super::value::Value;
5353
/// Decode can be executed recursively.
5454
///
5555
/// Decode `JSONB` Value from binary bytes.
56-
pub fn from_slice(buf: &[u8]) -> Result<Value<'_>, Error> {
56+
pub fn from_slice(buf: &[u8]) -> Result<Value<'_>> {
5757
let mut decoder = Decoder::new(buf);
5858
match decoder.decode() {
5959
Ok(value) => Ok(value),
@@ -62,7 +62,7 @@ pub fn from_slice(buf: &[u8]) -> Result<Value<'_>, Error> {
6262
}
6363
}
6464

65-
pub fn parse_jsonb(buf: &[u8]) -> Result<Value<'_>, Error> {
65+
pub fn parse_jsonb(buf: &[u8]) -> Result<Value<'_>> {
6666
let mut decoder = Decoder::new(buf);
6767
decoder.decode()
6868
}
@@ -77,7 +77,7 @@ impl<'a> Decoder<'a> {
7777
Self { buf }
7878
}
7979

80-
pub fn decode(&mut self) -> Result<Value<'a>, Error> {
80+
pub fn decode(&mut self) -> Result<Value<'a>> {
8181
// Valid `JSONB` Value has at least one `Header`
8282
if self.buf.len() < 4 {
8383
return Err(Error::InvalidJsonb);
@@ -89,7 +89,7 @@ impl<'a> Decoder<'a> {
8989
// Read value type from the `Header`
9090
// `Scalar` has one `JEntry`
9191
// `Array` and `Object` store the numbers of elements
92-
fn decode_jsonb(&mut self) -> Result<Value<'a>, Error> {
92+
fn decode_jsonb(&mut self) -> Result<Value<'a>> {
9393
let container_header = self.buf.read_u32::<BigEndian>()?;
9494

9595
match container_header & CONTAINER_HEADER_TYPE_MASK {
@@ -109,7 +109,7 @@ impl<'a> Decoder<'a> {
109109
// `Number` and `String` `JEntry` stores the length or offset of the data,
110110
// read them and decode to the `Value`
111111
// `Array` and `Object` need to read nested data from the lower-level `Header`
112-
fn decode_scalar(&mut self, jentry: JEntry) -> Result<Value<'a>, Error> {
112+
fn decode_scalar(&mut self, jentry: JEntry) -> Result<Value<'a>> {
113113
match jentry.type_code {
114114
NULL_TAG => Ok(Value::Null),
115115
TRUE_TAG => Ok(Value::Bool(true)),
@@ -135,7 +135,7 @@ impl<'a> Decoder<'a> {
135135

136136
// Decode the numbers of values from the `Header`,
137137
// then read all `JEntries`, finally decode the `Value` by `JEntry`
138-
fn decode_array(&mut self, container_header: u32) -> Result<Value<'a>, Error> {
138+
fn decode_array(&mut self, container_header: u32) -> Result<Value<'a>> {
139139
let length = (container_header & CONTAINER_HEADER_LEN_MASK) as usize;
140140
let jentries = self.decode_jentries(length)?;
141141
let mut values: Vec<Value> = Vec::with_capacity(length);
@@ -151,7 +151,7 @@ impl<'a> Decoder<'a> {
151151

152152
// The basic process is the same as that of `Array`
153153
// but first decode the keys and then decode the values
154-
fn decode_object(&mut self, container_header: u32) -> Result<Value<'a>, Error> {
154+
fn decode_object(&mut self, container_header: u32) -> Result<Value<'a>> {
155155
let length = (container_header & CONTAINER_HEADER_LEN_MASK) as usize;
156156
let mut jentries = self.decode_jentries(length * 2)?;
157157

@@ -178,7 +178,7 @@ impl<'a> Decoder<'a> {
178178
}
179179

180180
// Decode `JEntries` for `Array` and `Object`
181-
fn decode_jentries(&mut self, length: usize) -> Result<VecDeque<JEntry>, Error> {
181+
fn decode_jentries(&mut self, length: usize) -> Result<VecDeque<JEntry>> {
182182
let mut jentries: VecDeque<JEntry> = VecDeque::with_capacity(length);
183183
for _ in 0..length {
184184
let encoded = self.buf.read_u32::<BigEndian>()?;

src/error.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use core::fmt::Display;
16+
use serde::{de, ser};
1617

1718
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1819
pub enum ParseErrorCode {
@@ -34,6 +35,8 @@ pub enum ParseErrorCode {
3435
UnexpectedEndOfHexEscape,
3536
}
3637

38+
pub type Result<T> = std::result::Result<T, Error>;
39+
3740
impl Display for ParseErrorCode {
3841
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
3942
match *self {
@@ -88,19 +91,47 @@ pub enum Error {
8891
InvalidJsonType,
8992
InvalidObject,
9093
ObjectDuplicateKey,
94+
UnexpectedType,
9195

96+
Message(String),
9297
Syntax(ParseErrorCode, usize),
9398
}
9499

100+
impl ser::Error for Error {
101+
fn custom<T: Display>(msg: T) -> Self {
102+
Error::Message(msg.to_string())
103+
}
104+
}
105+
106+
impl de::Error for Error {
107+
fn custom<T: Display>(msg: T) -> Self {
108+
Error::Message(msg.to_string())
109+
}
110+
}
111+
95112
impl Display for Error {
96113
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
97114
match self {
115+
Error::Message(m) => write!(f, "{}", m),
98116
Error::Syntax(code, pos) => write!(f, "{}, pos {}", code, pos),
99117
_ => write!(f, "{:?}", self),
100118
}
101119
}
102120
}
103121

122+
impl std::error::Error for Error {
123+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
124+
//match self {
125+
// Error::JsonError(e) => Some(e),
126+
// Error::Json5Error(e) => Some(e),
127+
// Error::Io(e) => Some(e),
128+
// Error::Utf8(e) => Some(e),
129+
// _ => None,
130+
//}
131+
None
132+
}
133+
}
134+
104135
impl From<std::io::Error> for Error {
105136
fn from(_error: std::io::Error) -> Self {
106137
Error::InvalidUtf8

src/functions/array.rs

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
// This file contains functions that specifically operate on JSONB array values.
1616

17-
use core::convert::TryInto;
1817
use std::collections::BTreeMap;
1918
use std::collections::BTreeSet;
2019
use std::collections::VecDeque;
@@ -27,8 +26,14 @@ use crate::functions::core::read_u32;
2726
use crate::iterator::iterate_array;
2827
use crate::jentry::JEntry;
2928

29+
use serde::ser::SerializeSeq;
30+
use serde::Serializer;
31+
3032
use crate::OwnedJsonb;
3133
use crate::RawJsonb;
34+
use crate::ValueType;
35+
36+
use crate::Serializer as JsonbSerializer;
3237

3338
impl OwnedJsonb {
3439
/// Builds a JSONB array from a collection of RawJsonb values.
@@ -72,38 +77,17 @@ impl OwnedJsonb {
7277
/// let result = OwnedJsonb::build_array([invalid_data.as_raw()].into_iter());
7378
/// assert!(result.is_err());
7479
/// ```
75-
pub fn build_array<'a>(
76-
items: impl IntoIterator<Item = RawJsonb<'a>>,
77-
) -> Result<OwnedJsonb, Error> {
78-
let mut jentries = Vec::new();
79-
let mut data = Vec::new();
80-
for value in items.into_iter() {
81-
let header = read_u32(value.data, 0)?;
82-
let encoded_jentry = match header & CONTAINER_HEADER_TYPE_MASK {
83-
SCALAR_CONTAINER_TAG => {
84-
let jentry = &value.data[4..8];
85-
data.extend_from_slice(&value.data[8..]);
86-
jentry.try_into().unwrap()
87-
}
88-
ARRAY_CONTAINER_TAG | OBJECT_CONTAINER_TAG => {
89-
data.extend_from_slice(value.data);
90-
(CONTAINER_TAG | value.data.len() as u32).to_be_bytes()
91-
}
92-
_ => return Err(Error::InvalidJsonbHeader),
93-
};
94-
jentries.push(encoded_jentry);
80+
pub fn build_array<'a>(items: impl IntoIterator<Item = RawJsonb<'a>>) -> Result<OwnedJsonb> {
81+
// TODO
82+
let len = 3;
83+
let mut serializer = JsonbSerializer::default();
84+
let mut seq = serializer.serialize_seq(Some(len))?;
85+
86+
for item in items.into_iter() {
87+
seq.serialize_element(&item)?;
9588
}
96-
let len = jentries.len();
97-
// reserve space for header, jentries and value data
98-
let mut buf = Vec::with_capacity(data.len() + len * 4 + 4);
99-
// write header
100-
let header = ARRAY_CONTAINER_TAG | (len as u32);
101-
buf.extend_from_slice(&header.to_be_bytes());
102-
// write jentries
103-
for jentry in jentries.into_iter() {
104-
buf.extend_from_slice(&jentry);
105-
}
106-
buf.extend_from_slice(&data);
89+
let _ = seq.end();
90+
let buf = serializer.buffer.clone();
10791
Ok(OwnedJsonb::new(buf))
10892
}
10993
}
@@ -131,19 +115,13 @@ impl RawJsonb<'_> {
131115
/// let len = raw_jsonb.array_length().unwrap();
132116
/// assert_eq!(len, None);
133117
/// ```
134-
pub fn array_length(&self) -> Result<Option<usize>, Error> {
135-
let header = read_u32(self.data, 0)?;
136-
let len = match header & CONTAINER_HEADER_TYPE_MASK {
137-
ARRAY_CONTAINER_TAG => {
138-
let length = (header & CONTAINER_HEADER_LEN_MASK) as usize;
139-
Some(length)
140-
}
141-
OBJECT_CONTAINER_TAG | SCALAR_CONTAINER_TAG => None,
142-
_ => {
143-
return Err(Error::InvalidJsonb);
144-
}
145-
};
146-
Ok(len)
118+
pub fn array_length(&self) -> Result<Option<usize>> {
119+
let value_type = self.value_type()?;
120+
if let ValueType::Array(len) = value_type {
121+
Ok(Some(len))
122+
} else {
123+
Ok(None)
124+
}
147125
}
148126

149127
/// Extracts the values from a JSONB array.
@@ -193,7 +171,7 @@ impl RawJsonb<'_> {
193171
/// assert!(values_result.is_ok());
194172
/// assert!(values_result.unwrap().is_none());
195173
/// ```
196-
pub fn array_values(&self) -> Result<Option<Vec<OwnedJsonb>>, Error> {
174+
pub fn array_values(&self) -> Result<Option<Vec<OwnedJsonb>>> {
197175
let header = read_u32(self.data, 0)?;
198176
match header & CONTAINER_HEADER_TYPE_MASK {
199177
ARRAY_CONTAINER_TAG => {
@@ -269,7 +247,7 @@ impl RawJsonb<'_> {
269247
/// let result = invalid_raw_jsonb.array_distinct();
270248
/// assert!(result.is_err());
271249
/// ```
272-
pub fn array_distinct(&self) -> Result<OwnedJsonb, Error> {
250+
pub fn array_distinct(&self) -> Result<OwnedJsonb> {
273251
let mut buf = Vec::new();
274252
let value = self.data;
275253
let header = read_u32(value, 0)?;
@@ -358,7 +336,7 @@ impl RawJsonb<'_> {
358336
/// let contained = scalar1.as_raw().array_intersection(scalar2.as_raw()).unwrap();
359337
/// assert_eq!(contained.to_string(), "[]"); // Not contained
360338
/// ```
361-
pub fn array_intersection(&self, other: RawJsonb) -> Result<OwnedJsonb, Error> {
339+
pub fn array_intersection(&self, other: RawJsonb) -> Result<OwnedJsonb> {
362340
let mut buf = Vec::new();
363341
let left = self.data;
364342
let right = other.data;
@@ -480,7 +458,7 @@ impl RawJsonb<'_> {
480458
/// let not_contained = scalar1.as_raw().array_except(scalar2.as_raw()).unwrap();
481459
/// assert_eq!(not_contained.to_string(), "[1]"); // Not contained
482460
/// ```
483-
pub fn array_except(&self, other: RawJsonb) -> Result<OwnedJsonb, Error> {
461+
pub fn array_except(&self, other: RawJsonb) -> Result<OwnedJsonb> {
484462
let mut buf = Vec::new();
485463
let left = self.data;
486464
let right = other.data;
@@ -610,7 +588,7 @@ impl RawJsonb<'_> {
610588
/// let result = invalid_raw_jsonb.array_overlap(arr1.as_raw());
611589
/// assert!(result.is_err()); // Returns an error
612590
/// ```
613-
pub fn array_overlap(&self, other: RawJsonb) -> Result<bool, Error> {
591+
pub fn array_overlap(&self, other: RawJsonb) -> Result<bool> {
614592
let left = self.data;
615593
let right = other.data;
616594

@@ -729,7 +707,7 @@ impl RawJsonb<'_> {
729707
/// let inserted = raw_jsonb.array_insert(0, new_raw_jsonb);
730708
/// assert_eq!(inserted.unwrap().to_string(), "[2,1]");
731709
/// ```
732-
pub fn array_insert(&self, pos: i32, new_val: RawJsonb) -> Result<OwnedJsonb, Error> {
710+
pub fn array_insert(&self, pos: i32, new_val: RawJsonb) -> Result<OwnedJsonb> {
733711
let mut buf = Vec::new();
734712
let value = self.data;
735713
let new_value = new_val.data;

0 commit comments

Comments
 (0)