These are chat archives for rust-lang/rust

5th
Nov 2018
Tom Watson
@FridgeSeal
Nov 05 2018 07:00
Hi everyone! I’ve got some badly behaved JSON data that I’m trying deserialise with serde_json: basically there’s one field that should be Option<Vec<String>> but for whatever reason, the data source sometimes writes records as empty strings (“”), has a look at the string_or_struct section in serde, but no luck getting to work, anyone have any ideas on how to tackle this?
Isobel Redelmeier
@iredelmeier
Nov 05 2018 07:11
@FridgeSeal that sounds like you want either Vec<Option<String>> or Option<Vec<Option<String>>>
Tom Watson
@FridgeSeal
Nov 05 2018 07:17
Mmmm gave that a try, but it still fails with
Isobel Redelmeier
@iredelmeier
Nov 05 2018 07:18
What’s the failure?
Tom Watson
@FridgeSeal
Nov 05 2018 07:25
Sorry my phone sends things if I try to close the keyboard.
The error is “invalid type: string \”\”, expected a sequence”.
Isobel Redelmeier
@iredelmeier
Nov 05 2018 07:27
That sounds like the value was something like foo instead of [foo]. Is that accurate?
Tom Watson
@FridgeSeal
Nov 05 2018 07:28
Yep because my struct definition is (for example): struct TestCase {field1: Option<Vec<String>>}. Unfortunately sometimes the data resembles: {field1: “”}
The software producing this data is serialising null values as empty strings, rather than the JSON spec null, and I unfortunately can’t get it to change that behaviour. The serde_json docs have a section on using the serde macro “deserialise with”, had a go at implementing that myself, but I am a bit out of my depth implementing my own deserialiser like that, so I was hoping someone might have a clever idea on how I could get around it another way :P
Isobel Redelmeier
@iredelmeier
Nov 05 2018 07:32
Does the data ever actually contained arrays?
Tom Watson
@FridgeSeal
Nov 05 2018 07:33
Yeah, I’m parsing about 100k json objects, about 10% of them have these empty strings instead of arrays filled with data
Isobel Redelmeier
@iredelmeier
Nov 05 2018 07:34
How about using deserialize_with and a custom function?
Tom Watson
@FridgeSeal
Nov 05 2018 07:36
Phone pls.
Using the example in “either string or struct” section, but didn’t have much luck, think I might give that another go...
Tom Watson
@FridgeSeal
Nov 05 2018 08:10
@iredelmeier I got it working! Turns out the example in the serde docs was a bit overkill for what I actually needed to do, but I got there!
Isobel Redelmeier
@iredelmeier
Nov 05 2018 19:26
Awesome @FridgeSeal !
verilog15
@verilog15
Nov 05 2018 20:51
I was reading about modules, but didn't quite understand it fully. can someone provide an example of a module and a main so that main uses a function of module? should be something simple
AV Gaspard-Odelin
@Gaspard-Odelin
Nov 05 2018 21:06
@verilog15 https://medium.com/learning-rust/rust-lets-get-it-started-bdd8de58178d
As a beginner, I do like his articles. (A bit more general than modules, but should be fine)