Skip to content

Commit

Permalink
workaround for serde_json 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Le Roux committed Oct 1, 2024
1 parent e3b1f58 commit 059e51d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mysqlclient-src = { version = "0.1.0", optional = true }
pq-sys = { version = ">=0.4.0, <0.7.0", optional = true }
pq-src = { version = "0.3", optional = true }
quickcheck = { version = "1.0.3", optional = true }
serde_json = { version = ">=0.9.0, <2.0", optional = true }
serde_json = { version = ">=0.8.0, <2.0", optional = true }
url = { version = "2.1.0", optional = true }
percent-encoding = { version = "2.1.0", optional = true }
uuid = { version = ">=0.7.0, <2.0.0", optional = true }
Expand Down
59 changes: 35 additions & 24 deletions diesel/src/sqlite/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,15 @@ mod jsonb {

// Read only the number of bytes specified by the payload size
let int_str = std::str::from_utf8(bytes).map_err(|_| "Invalid ASCII in JSONB integer")?;
// Parse the integer string into an i64
let int_value = int_str
.parse::<i64>()
.map_err(|_| "Failed to parse JSONB integer")?;
let int_value = serde_json::from_str(int_str)
.map_err(|_| "Failed to parse JSONB")
.and_then(|v: serde_json::Value| {
v.is_i64()
.then_some(v)
.ok_or("Failed to parse JSONB integer")
})?;

Ok(serde_json::Value::Number(serde_json::Number::from(
int_value,
)))
Ok(int_value)
}

// Read a JSON float in canonical format (FLOAT)
Expand All @@ -203,12 +204,15 @@ mod jsonb {
}

let float_str = std::str::from_utf8(bytes).map_err(|_| "Invalid UTF-8 in JSONB float")?;
let float_value = float_str
.parse::<f64>()
.map_err(|_| "Failed to parse JSONB float")?;
Ok(serde_json::Value::Number(
serde_json::Number::from_f64(float_value).ok_or("Invalid float value")?,
))
let float_value = serde_json::from_str(float_str)
.map_err(|_| "Failed to parse JSONB")
.and_then(|v: serde_json::Value| {
v.is_f64()
.then_some(v)
.ok_or("Failed to parse JSONB number")
})?;

Ok(float_value)
}

// Read a JSON string
Expand Down Expand Up @@ -363,13 +367,20 @@ mod jsonb {
value: &serde_json::Value,
buffer: &mut Vec<u8>,
) -> serialize::Result {
match value {
serde_json::Value::Null => write_jsonb_null(buffer),
serde_json::Value::Bool(b) => write_jsonb_bool(*b, buffer),
serde_json::Value::Number(n) => write_jsonb_number(n, buffer),
serde_json::Value::String(s) => write_jsonb_string(s, buffer),
serde_json::Value::Array(arr) => write_jsonb_array(arr, buffer),
serde_json::Value::Object(obj) => write_jsonb_object(obj, buffer),
if value.is_null() {
write_jsonb_null(buffer)
} else if value.is_boolean() {
write_jsonb_bool(value.as_bool().ok_or("Failed to read JSONB value")?, buffer)
} else if value.is_number() {
write_jsonb_number(value, buffer)
} else if value.is_string() {
write_jsonb_string(value.as_str().ok_or("Failed to read JSONB value")?, buffer)
} else if value.is_array() {
write_jsonb_array(value.as_array().ok_or("Failed to read JSONB value")?, buffer)
} else if value.is_object() {
write_jsonb_object(value.as_object().ok_or("Failed to read JSONB value")?, buffer)
} else {
Err("Unsupported JSONB value type".into())
}
}

Expand All @@ -388,7 +399,7 @@ mod jsonb {

// Write a JSON number (integers and floats)
pub(super) fn write_jsonb_number(
n: &serde_json::Number,
n: &serde_json::Value,
buffer: &mut Vec<u8>,
) -> serialize::Result {
if let Some(i) = n.as_i64() {
Expand All @@ -398,7 +409,7 @@ mod jsonb {
// Write a float (FLOAT type)
write_jsonb_float(f, buffer)
} else {
Err("Invalid number type".into())
Err("Invalid JSONB number type".into())
}
}

Expand Down Expand Up @@ -440,8 +451,8 @@ mod jsonb {

pub(super) fn write_jsonb_textj(s: &str, buffer: &mut Vec<u8>) -> serialize::Result {
// Escaping the string for JSON (e.g., \n, \uXXXX)
let escaped_string =
serde_json::to_string(s).map_err(|_| "Failed to serialize string for TEXTJ")?;
let escaped_string = serde_json::to_string(&String::from(s))
.map_err(|_| "Failed to serialize string for TEXTJ")?;

// Remove the surrounding quotes from serde_json::to_string result
let escaped_string = &escaped_string[1..escaped_string.len() - 1];
Expand Down

0 comments on commit 059e51d

Please sign in to comment.