Skip to content

Commit

Permalink
allow Entity to be deserialized with serde_json (#3873)
Browse files Browse the repository at this point in the history
# Objective

- `serde_json` assumes that numbers being deserialized are either u64 or i64.
- `Entity` serializes and deserializes as a u32.
- Deserializing an `Entity` with `serde_json` fails with: `Error("invalid type: integer 10947, expected expected Entity"`

## Solution

- Implemented a visitor for u64 that allows an `Entity` to be deserialized in this case.
- While I was here, also fixed the redundant "expected expected Entity" in the error message
- Tested the change in a local project which now correctly deserializes `Entity` structs with `serde_json` when it couldn't before
  • Loading branch information
EpsilonDelphine committed Feb 6, 2022
1 parent 75286b8 commit b13f238
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/bevy_ecs/src/entity/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'de> Visitor<'de> for EntityVisitor {
type Value = Entity;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("expected Entity")
formatter.write_str("Entity")
}

fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
Expand All @@ -34,4 +34,11 @@ impl<'de> Visitor<'de> for EntityVisitor {
{
Ok(Entity::from_raw(v))
}

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

0 comments on commit b13f238

Please sign in to comment.