Adding new behavior when using a library


I’m working on serializing and deserializing the EDN format using serde.

I’m having a hard time figuring out how to use serde to deserialize EDN’s different sequence types.

Summarizing the problem as best I can: say JSON gains support for Sets, represented by ( ) in a JSON string. For (“foo”, “bar”, “baz”) I’d like to deserialize in the same way as serde_json does for its Value::Array when it detects a [ in the input. The only difference is I want to return a Value::Set when I see a ( in the input.

For an array, serde_json calls visit_seq on its visitor argument, a method defined by serde::de::Visitor. The visitor is defined and implemented here.

What seems important: visit_seq works wonderfully. It returns a Value::Array(Vec<Value>) by using serde’s traits and their implementations, handling recursion.

Here, we want the same basic thing. The only difference: Instead of a Value::Array we want to return a Value::Set.

What’s the best way to achieve this?

Any help is greatly appreciated!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.