Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fixtures and migrate to serde_with #563

Merged
merged 18 commits into from
Jul 31, 2024
Merged

Add fixtures and migrate to serde_with #563

merged 18 commits into from
Jul 31, 2024

Conversation

aumetra
Copy link
Member

@aumetra aumetra commented Jul 30, 2024

No description provided.

@aumetra
Copy link
Member Author

aumetra commented Jul 30, 2024

@tesaguri There are two failures right now and I'm not quite sure why.
Since you wrote the code, I thought you could maybe look over it. Because a lot of the migration from raw serde types to the serde_with traits worked flawless.

There is just some weird bug with FirstOk and with the Id deserializer..

@aumetra
Copy link
Member Author

aumetra commented Jul 30, 2024

Never mind, fixed one issue, and replaced the other with a different one :^)
Really fun!

@aumetra
Copy link
Member Author

aumetra commented Jul 31, 2024

@tesaguri just one little question, I can't quite wrap my head around that yet, why do we need this SeqAccess indirection here?
I get that it's required for it to work (I tried other things), but not quite sure why. Why do we need this indirection instead of doing something with seq.next_element() and some mapping operations?

struct SeqAccess<A>(A);
impl<'de, A> de::SeqAccess<'de> for SeqAccess<A>
where
A: de::SeqAccess<'de>,
{
type Error = A::Error;
fn next_element<T>(&mut self) -> Result<Option<T>, Self::Error>
where
T: Deserialize<'de>,
{
let value = self.0.next_element::<DeserializeAsWrap<_, Id>>()?;
Ok(value.map(DeserializeAsWrap::into_inner))
}
fn next_element_seed<T>(&mut self, _seed: T) -> Result<Option<T::Value>, Self::Error>
where
T: DeserializeSeed<'de>,
{
unreachable!();
}
fn size_hint(&self) -> Option<usize> {
self.0.size_hint()
}
}
T::deserialize(SeqAccessDeserializer::new(SeqAccess(seq)))

@aumetra aumetra force-pushed the aw/deserialize-tests branch from af8bde8 to 0a34984 Compare July 31, 2024 10:53
@aumetra aumetra merged commit f985632 into main Jul 31, 2024
18 of 19 checks passed
@aumetra aumetra deleted the aw/deserialize-tests branch July 31, 2024 17:33
@tesaguri
Copy link
Contributor

tesaguri commented Aug 1, 2024

@tesaguri just one little question, I can't quite wrap my head around that yet, why do we need this SeqAccess indirection here?
I get that it's required for it to work (I tried other things), but not quite sure why. Why do we need this indirection instead of doing something with seq.next_element() and some mapping operations?

I don't remember my thought at that time exactly so it's going to be a wild guess, but with seq.next_element::<T>(), I think <T as Deserialize>::deserialize would see map values like {"id": "https://example.com/", …} as-is. On the other hand, our goal here is to show the deserialize method a string value like "https://example.com/".

In addition to the map type, we also need to be able to deserialise verbatim string inputs like "https://example.com/". I think (unverified) the only way of handling both the types via seq.next_element() without the SeqAccess indirection would be to deserialise the next element with seq.next_element::<Value>() and matching on Value::String and Value::Object, which wouldn't be very efficient (if we don't need efficiency, we would be deserialising the entire input JSON into a DOM instead of bothering with the whole jsonld::serde megalith in the first place).

Also, Id::deserialize_as(SeqAccessDeserializer::new(seq)) would run into infinite recursion since the SeqAccessDeserializer::deserialize_any method would call Visitor::visit_seq.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants