use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;
use serde::de::DeserializeOwned;
use serde_json::value::RawValue;
use crate::validate::{Validate, ViolationKind};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum UnknownEnumValues {
#[default]
Reject,
Preserve,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum UnknownFields {
Reject,
#[default]
Ignore,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum DateTimeLeniency {
#[default]
Strict,
AllowNaive,
AllowSpace,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum NumericStrings {
#[default]
Reject,
Coerce,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum WholeFloats {
#[default]
Reject,
Coerce,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DecodeOptions {
pub unknown_enum_values: UnknownEnumValues,
pub unknown_fields: UnknownFields,
pub datetime: DateTimeLeniency,
pub numeric_strings: NumericStrings,
pub whole_floats: WholeFloats,
pub max_payload_size: usize,
pub max_repairs: usize,
}
impl Default for DecodeOptions {
fn default() -> Self {
Self::strict()
}
}
impl DecodeOptions {
#[must_use]
pub const fn strict() -> Self {
Self {
unknown_enum_values: UnknownEnumValues::Reject,
unknown_fields: UnknownFields::Ignore,
datetime: DateTimeLeniency::Strict,
numeric_strings: NumericStrings::Reject,
whole_floats: WholeFloats::Reject,
max_payload_size: 1024 * 1024,
max_repairs: 32,
}
}
#[must_use]
pub const fn pedantic() -> Self {
Self {
unknown_fields: UnknownFields::Reject,
..Self::strict()
}
}
#[must_use]
pub const fn lenient() -> Self {
Self {
unknown_enum_values: UnknownEnumValues::Preserve,
unknown_fields: UnknownFields::Ignore,
datetime: DateTimeLeniency::AllowSpace,
numeric_strings: NumericStrings::Coerce,
whole_floats: WholeFloats::Coerce,
max_payload_size: 1024 * 1024,
max_repairs: 32,
}
}
#[must_use]
pub const fn with_max_payload_size(mut self, bytes: usize) -> Self {
self.max_payload_size = bytes;
self
}
#[must_use]
pub const fn with_unknown_enum_values(mut self, policy: UnknownEnumValues) -> Self {
self.unknown_enum_values = policy;
self
}
#[must_use]
pub const fn with_unknown_fields(mut self, policy: UnknownFields) -> Self {
self.unknown_fields = policy;
self
}
#[must_use]
pub const fn with_datetime(mut self, policy: DateTimeLeniency) -> Self {
self.datetime = policy;
self
}
#[must_use]
pub const fn with_numeric_strings(mut self, policy: NumericStrings) -> Self {
self.numeric_strings = policy;
self
}
#[must_use]
pub const fn with_whole_floats(mut self, policy: WholeFloats) -> Self {
self.whole_floats = policy;
self
}
#[must_use]
pub const fn with_max_repairs(mut self, repairs: usize) -> Self {
self.max_repairs = repairs;
self
}
fn repairs_enabled(&self) -> bool {
self.datetime != DateTimeLeniency::Strict
|| self.numeric_strings == NumericStrings::Coerce
|| self.whole_floats == WholeFloats::Coerce
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DecodeErrorKind {
Format,
Occurrence,
Type,
Property,
UnknownField,
Protocol,
UnsupportedAction,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DecodeError {
pub kind: DecodeErrorKind,
pub path: String,
pub reason: String,
}
impl DecodeError {
#[must_use]
pub fn new(kind: DecodeErrorKind, path: impl Into<String>, reason: impl Into<String>) -> Self {
Self {
kind,
path: path.into(),
reason: reason.into(),
}
}
#[must_use]
pub fn unsupported_action(action: &str) -> Self {
Self::new(
DecodeErrorKind::UnsupportedAction,
"",
format!("action {action:?} is not valid in this direction"),
)
}
#[must_use]
fn from_violation(violation: &crate::validate::Violation) -> Self {
let kind = match violation.kind {
ViolationKind::Occurrence => DecodeErrorKind::Occurrence,
ViolationKind::Type => DecodeErrorKind::Type,
ViolationKind::Property | ViolationKind::UnknownEnumValue => DecodeErrorKind::Property,
ViolationKind::UnknownField => DecodeErrorKind::UnknownField,
ViolationKind::Protocol => DecodeErrorKind::Protocol,
};
Self::new(kind, violation.path.clone(), violation.reason.clone())
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.path.is_empty() {
write!(f, "{}", self.reason)
} else {
write!(f, "{}: {}", self.path, self.reason)
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DecodeError {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Repair {
pub pointer: String,
pub kind: RepairKind,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum RepairKind {
NumericString,
DateTimeWithoutOffset,
DateTimeWithSpace,
WholeFloat,
UnknownEnumValue,
}
impl RepairKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
RepairKind::NumericString => "numeric_string",
RepairKind::DateTimeWithoutOffset => "datetime_without_offset",
RepairKind::DateTimeWithSpace => "datetime_with_space",
RepairKind::WholeFloat => "whole_float",
RepairKind::UnknownEnumValue => "unknown_enum_value",
}
}
}
impl fmt::Display for RepairKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Decoded<T> {
pub value: T,
pub repairs: Vec<Repair>,
}
impl<T> Decoded<T> {
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Decoded<U> {
Decoded {
value: f(self.value),
repairs: self.repairs,
}
}
}
pub fn decode_payload<T: DeserializeOwned + serde::Serialize + Validate>(
payload: &RawValue,
options: &DecodeOptions,
) -> Result<T, DecodeError> {
decode_payload_reporting(payload, options).map(|decoded| decoded.value)
}
pub fn decode_payload_reporting<T: DeserializeOwned + serde::Serialize + Validate>(
payload: &RawValue,
options: &DecodeOptions,
) -> Result<Decoded<T>, DecodeError> {
let mut repairs = Vec::new();
let text = payload.get();
if text.len() > options.max_payload_size {
return Err(DecodeError::new(
DecodeErrorKind::Format,
"",
format!(
"payload is {} bytes, limit is {}",
text.len(),
options.max_payload_size
),
));
}
if !text.trim_start().starts_with('{') {
return Err(DecodeError::new(
DecodeErrorKind::Format,
"",
"payload is not a JSON object",
));
}
let value: T = match parse(text) {
Ok(value) => value,
Err(error) if options.repairs_enabled() => {
repair_and_parse(text, options, &error, &mut repairs)?
}
Err(error) => return Err(error),
};
if options.unknown_fields == UnknownFields::Reject {
if let Some(path) = first_unknown_field(text, &value)? {
return Err(DecodeError::new(
DecodeErrorKind::UnknownField,
path,
"member is not defined by the schema",
));
}
}
let mut violations = match value.validate() {
Ok(()) => return Ok(Decoded { value, repairs }),
Err(violations) => violations,
};
if options.unknown_enum_values == UnknownEnumValues::Preserve {
repairs.extend(
violations
.as_slice()
.iter()
.filter(|v| v.kind == ViolationKind::UnknownEnumValue)
.map(|v| Repair {
pointer: v.path.clone(),
kind: RepairKind::UnknownEnumValue,
}),
);
violations.retain_kinds(|kind| kind != ViolationKind::UnknownEnumValue);
}
match violations.first() {
Some(violation) => Err(DecodeError::from_violation(violation)),
None => Ok(Decoded { value, repairs }),
}
}
pub fn transcode<T: DeserializeOwned + serde::Serialize + Validate>(
payload: &RawValue,
options: &DecodeOptions,
) -> Result<alloc::boxed::Box<RawValue>, DecodeError> {
let value = decode_payload::<T>(payload, options)?;
serde_json::value::to_raw_value(&value)
.map_err(|error| DecodeError::new(DecodeErrorKind::Format, "", error.to_string()))
}
fn parse<T: DeserializeOwned>(text: &str) -> Result<T, DecodeError> {
let mut deserializer = serde_json::Deserializer::from_str(text);
match serde_path_to_error::deserialize(&mut deserializer) {
Ok(value) => Ok(value),
Err(error) => Err(classify(&error)),
}
}
fn classify(error: &serde_path_to_error::Error<serde_json::Error>) -> DecodeError {
let path = pointer_from(&error.path().to_string());
let inner = error.inner();
let message = inner.to_string();
let kind = match inner.classify() {
serde_json::error::Category::Syntax | serde_json::error::Category::Eof => {
DecodeErrorKind::Format
}
serde_json::error::Category::Io => DecodeErrorKind::Format,
serde_json::error::Category::Data => {
if message.starts_with("missing field") || message.starts_with("invalid length") {
DecodeErrorKind::Occurrence
} else if message.starts_with("invalid type") {
DecodeErrorKind::Type
} else if message.starts_with("unknown field") {
DecodeErrorKind::UnknownField
} else if message.starts_with("duplicate field") {
DecodeErrorKind::Format
} else {
DecodeErrorKind::Property
}
}
};
let reason = match message.rfind(" at line ") {
Some(cut) => message[..cut].to_owned(),
None => message,
};
DecodeError::new(kind, path, reason)
}
fn pointer_from(path: &str) -> String {
if path.is_empty() || path == "." {
return String::new();
}
let mut out = String::with_capacity(path.len() + 1);
for segment in path.split('.') {
out.push('/');
out.push_str(&segment.replace('~', "~0").replace('/', "~1"));
}
out
}
fn repair_and_parse<T: DeserializeOwned>(
text: &str,
options: &DecodeOptions,
first: &DecodeError,
repairs: &mut Vec<Repair>,
) -> Result<T, DecodeError> {
let mut json: serde_json::Value = serde_json::from_str(text)
.map_err(|error| DecodeError::new(DecodeErrorKind::Format, "", error.to_string()))?;
let mut error = first.clone();
for _ in 0..options.max_repairs {
let Some(kind) = apply_repair(&mut json, &error, options) else {
return Err(error);
};
repairs.push(Repair {
pointer: error.path.clone(),
kind,
});
match serde_path_to_error::deserialize::<_, T>(&json) {
Ok(value) => return Ok(value),
Err(next) => {
let next = classify(&next);
if next == error {
return Err(next);
}
error = next;
}
}
}
Err(error)
}
fn apply_repair(
json: &mut serde_json::Value,
error: &DecodeError,
options: &DecodeOptions,
) -> Option<RepairKind> {
let slot = pointer_mut(json, &error.path)?;
if options.whole_floats == WholeFloats::Coerce && error.kind == DecodeErrorKind::Type {
if let Some(whole) = slot
.as_f64()
.filter(|_| slot.is_f64())
.and_then(whole_number)
{
*slot = serde_json::Value::from(whole);
return Some(RepairKind::WholeFloat);
}
}
let text = slot.as_str().map(ToOwned::to_owned)?;
if options.numeric_strings == NumericStrings::Coerce && error.kind == DecodeErrorKind::Type {
if let Ok(number) = text.parse::<serde_json::Number>() {
*slot = serde_json::Value::Number(number);
return Some(RepairKind::NumericString);
}
}
if options.datetime != DateTimeLeniency::Strict && error.kind == DecodeErrorKind::Property {
let allow_space = options.datetime == DateTimeLeniency::AllowSpace;
if !allow_space && text.contains(' ') {
return None;
}
let normalized = crate::types::normalize_datetime(&text);
if normalized != text && crate::types::DateTime::parse(&normalized).is_ok() {
*slot = serde_json::Value::String(normalized);
return Some(if text.contains(' ') {
RepairKind::DateTimeWithSpace
} else {
RepairKind::DateTimeWithoutOffset
});
}
}
None
}
#[allow(
clippy::cast_possible_truncation,
clippy::float_cmp,
clippy::cast_precision_loss
)]
fn whole_number(v: f64) -> Option<i64> {
(v.is_finite() && v.abs() < 9.0e15 && (v as i64) as f64 == v).then_some(v as i64)
}
fn pointer_mut<'a>(
value: &'a mut serde_json::Value,
pointer: &str,
) -> Option<&'a mut serde_json::Value> {
if pointer.is_empty() {
return Some(value);
}
let mut current = value;
for raw in pointer.trim_start_matches('/').split('/') {
let key = raw.replace("~1", "/").replace("~0", "~");
current = match current {
serde_json::Value::Object(map) => map.get_mut(&key)?,
serde_json::Value::Array(items) => items.get_mut(key.parse::<usize>().ok()?)?,
_ => return None,
};
}
Some(current)
}
fn first_unknown_field<T: serde::Serialize>(
text: &str,
value: &T,
) -> Result<Option<String>, DecodeError> {
let wire: serde_json::Value = serde_json::from_str(text)
.map_err(|error| DecodeError::new(DecodeErrorKind::Format, "", error.to_string()))?;
let ours = serde_json::to_value(value)
.map_err(|error| DecodeError::new(DecodeErrorKind::Format, "", error.to_string()))?;
let mut path = Vec::new();
Ok(diff(&wire, &ours, &mut path))
}
fn diff(
wire: &serde_json::Value,
ours: &serde_json::Value,
path: &mut Vec<String>,
) -> Option<String> {
match (wire, ours) {
(serde_json::Value::Object(w), serde_json::Value::Object(o)) => {
for (key, value) in w {
if value.is_null() {
continue; }
path.push(key.replace('~', "~0").replace('/', "~1"));
let found = match o.get(key) {
None => Some(render(path)),
Some(mine) => diff(value, mine, path),
};
path.pop();
if found.is_some() {
return found;
}
}
None
}
(serde_json::Value::Array(w), serde_json::Value::Array(o)) => {
for (index, (value, mine)) in w.iter().zip(o).enumerate() {
path.push(index.to_string());
let found = diff(value, mine, path);
path.pop();
if found.is_some() {
return found;
}
}
None
}
_ => None,
}
}
fn render(path: &[String]) -> String {
let mut out = String::new();
for segment in path {
out.push('/');
out.push_str(segment);
}
out
}