Things I wish I had known about serde_json

489 points richardanaya

I've been knee deep in parsing lot's of json lately with rust and here's some details I thought i'd share:

  • untagged unions are extremely slow to parse compared to tagged unions. If you care about performance, it's always better to have a property you can discriminate union variants by.
  • you can avoid allocations of Strings by parsing to a data structure that uses \&str\\ fields
  • There's a type \serde\\_json::Number\\ that avoids converting to number formats until you actually want to spend the processing time
  • if you are using a tagged union to disriminate based off a json property value, you don't have to use the name of your enum variant , you can use

​

#[serde(tag="animal_type")]
enum Animal {
   #[serde(alias="crab")]
   Crab(MyCrabStruct)
   #[serde(alias="gopher")]
   Gopher(MyGopherStruct)
}

Anyone else find any interesting details?