Unexpected output from native-dialog (formerly PathBuf deserialise and serialise is inconsistent)

I need to store a directory in a config file. I'm currently using serde to serialise and deserialise the directory (which is a PathBuf). However, I have noticed that when serialising, a directory such as / > Users > user > Downloads becomes file:///Users/user/Downloads/. Then when deserialising, the directory becomes file/ > Users > user > Downloads. I guess serialising to JSON assumes its for web use and prepends a file://. So how can I mitigate this? Are there any alternatives?

If the serialization for PathBuf doesn't match its deserialization, then I would open a bug about that.

No. I don't think it would even be possible to do it. In Serde, the serialization format is decoupled from the data structure being (de)serialized. That is in fact the hole point of Serde. A data structure serializes itself into a tree of core primitives defined by Serde. For example, PathBuf serializes itself into a string. The serializer at the other end of the pipeline can't tell if a particular string came from a PathBuf or something else, and the data structure (the PathBuf in our case) can't tell which particular format it is being serialized into.

Accordingly, your claim does not reproduce in an MVCE. There must be something else wrong with another part of your program, and this does not appear to be a bug in Serde or Serde-JSON.

1 Like

It would not surprise me if this is windows-specific behavior.

1 Like

Yup I just checked using another program and it is consistant :frowning: . I'm receiving the path from native-dialog's file picker so that might be the issue. I should've checked before posting this

Yes it seems that native_dialog::FileDialog's show_open_single_dir() prepends file:// to the returned PathBuf even though it is supposed to be directory. Apologies to everyone! I've filed an issue in native-dialog's repository

1 Like

Awesome, the important thing is that you found the bug.

1 Like

Seems like native-dialog isn't really maintained anymore. The latest release was a year ago. (edit: the author did respond to the issue but I'm sticking with rfd) There is an alternative, rfd, which looks very similar. I'll try migrating to that and see what happens.

Update: Yes rfd works as expected!

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.