Skip to content

Commit

Permalink
fix: json deserialization from payload fails with i128 is not support…
Browse files Browse the repository at this point in the history
…ed (#712)
  • Loading branch information
paulobressan authored Oct 7, 2023
1 parent 27fb7e8 commit d25e265
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::fmt::Display;

use merge::Merge;

use serde::{Deserialize, Serialize};
use serde::{
de::{Error as DeError, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use serde_json::Value as JsonValue;

use strum_macros::Display;
Expand All @@ -29,6 +32,8 @@ pub enum Era {
pub enum MetadatumRendition {
MapJson(JsonValue),
ArrayJson(JsonValue),
#[serde(serialize_with = "serialize_int_scalar")]
#[serde(deserialize_with = "deserialize_int_scalar")]
IntScalar(i128),
TextScalar(String),
BytesHex(String),
Expand Down Expand Up @@ -374,3 +379,53 @@ pub struct Event {

pub fingerprint: Option<String>,
}

fn serialize_int_scalar<S>(value: &i128, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if *value > (i64::MAX as i128) || *value < (i64::MIN as i128) {
return value.to_string().serialize(serializer);
}

value.serialize(serializer)
}

fn deserialize_int_scalar<'de, D>(deserializer: D) -> Result<i128, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(IntScalarVisitor)
}

#[derive(Debug)]
struct IntScalarVisitor;
impl<'de> Visitor<'de> for IntScalarVisitor {
type Value = i128;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
dbg!(self);
formatter.write_str("expect to receive integer")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.parse().map_err(DeError::custom)?)
}

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v as i128)
}

fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v as i128)
}
}

0 comments on commit d25e265

Please sign in to comment.