Skip to content

Commit

Permalink
Remove workaround for serde_json's arbitrary_precision feature (ser…
Browse files Browse the repository at this point in the history
…enity-rs#1669)

In the past `serde_json` would call `Visitor::visit_map` for all numbers
when its `arbitrary_precision` feature is activated.
Since `serde_json` v1.0.75 that method is only called for 128bit numbers.
  • Loading branch information
nickelc authored and arqunis committed Apr 2, 2022
1 parent f54d918 commit 73f758f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -17,7 +17,7 @@ members = ["examples/*"]

[dependencies]
bitflags = "1.1"
serde_json = "1"
serde_json = "1.0.75"
async-trait = "0.1.9"

[dependencies.simd-json]
Expand Down
37 changes: 2 additions & 35 deletions src/model/id.rs
Expand Up @@ -254,8 +254,8 @@ pub(crate) mod snowflake {
use std::convert::TryFrom;
use std::fmt;

use serde::de::{Error, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serializer};
use serde::de::{Error, Visitor};
use serde::{Deserializer, Serializer};

pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u64, D::Error> {
deserializer.deserialize_any(SnowflakeVisitor)
Expand Down Expand Up @@ -287,39 +287,6 @@ pub(crate) mod snowflake {
fn visit_str<E: Error>(self, value: &str) -> Result<Self::Value, E> {
value.parse().map_err(Error::custom)
}

// This is called when serde_json's `arbitrary_precision` feature is enabled.
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
let id = map.next_value::<Snowflake>()?;
Ok(id.value)
}
}

struct Snowflake {
value: u64,
}

impl<'de> Deserialize<'de> for Snowflake {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct StrVisitor;

impl<'de> Visitor<'de> for StrVisitor {
type Value = Snowflake;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("string containing a number")
}

fn visit_str<E: Error>(self, s: &str) -> Result<Snowflake, E> {
let value = s.parse().map_err(Error::custom)?;
Ok(Snowflake {
value,
})
}
}

deserializer.deserialize_str(StrVisitor)
}
}
}

Expand Down

0 comments on commit 73f758f

Please sign in to comment.