serde is one of the best things about Rust in practice. It is more convenient to use serde to serialize/deserialize to some standard format like JSON or YAML than it is to write your own half-baked format and serialization/deserialization code --- even for the simplest tasks --- so you just stop doing the latter. This is a fundamental shift, and very good for your software. "Convenience" here actually covers a lot of…
I think in most ecosystems it's easier to use a prebuilt serializer/deserializer than your own format.
The Serde Rust Framework
91–100 of 159 posts
Re: The Serde Rust Framework
#92serde is one of the best things about Rust in practice. It is more convenient to use serde to serialize/deserialize to some standard format like JSON or YAML than it is to write your own half-baked format and serialization/deserialization code --- even for the simplest tasks --- so you just stop doing the latter. This is a fundamental shift, and very good for your software. "Convenience" here actually covers a lot of…
The only downside is compile time bloat. Serde generates heaps and heaps of generic code. This all gets optimized away to be very efficient, but only once it reaches LLVM. Ever tried working on a crate with hundreds or thousands of de/serializable types? Compile times shoot through the roof really quickly, and serde is often the culprit. The maintainer of serde also created `miniserde` [1] to tackle this problem, whi…
Re: The Serde Rust Framework
#93Earlier quoted context omitted.
I rarely have to think about deserialisation in c# or java either...
I wrote a lot of Java back in the day. I elaborated on why I disagree in the other comment. I do not know if the situation in C# is similar.
public record Point(int x, int y) {}
// [...]
var objectMapper = new ObjectMapper();
var jsonString = objectMapper.writeValueAsString(new Point(2,1));
It isn't obvious to me what advantage Serde offers that you couldn't get with Jackson or other similar libraries in Java (although I get that Java and Rust are different languages, and that there may not be something this ergonomic in the likes of C++)Re: The Serde Rust Framework
#94Earlier quoted context omitted.
I thought I'd share this project in case anyone wanted to quickly evaluate rust for web https://github.com/wulf/create-rust-app Although there's a lot of work left to be done, I'd love to hear feedback, painpoints and ideas you have :) Hoping to have documentation up at create-rust-app.dev soon~
Please consider giving warp as choice for the web server.
Re: The Serde Rust Framework
#95Earlier quoted context omitted.
The trick with Serde is that it decodes straight into a native Rust struct. You get most of validation for free (it also nicely takes advantage of Rust enums with data), and struct access is maximally fast. A generic JSON decoder would give you a dynamic structure that can contain anything, and then you'd have to pick it apart.
However, if you just want a very minimal subset of a JSON response (i.e. a 'status' string field out of say 20 other values in a map), is it not better to just look for one field, as opposed to have to create an entire struct in Rust to represent the full response? How does Serde work with optional extra fields? I know you can use Option , but that implies you know they exist - what happens if fields get added in the…
You can define a struct with just the status field.
> How does Serde work with optional extra fields? I know you can use Option, but that implies you know they exist - what happens if fields get added in the future silently? I guess the schema changes then, which isn't good, but that might happen?
Unknown fields are skipped by default.
Re: The Serde Rust Framework
#96Earlier quoted context omitted.
I've usually seen this handled by having everything be a pointer, the default value of the pointer is nil. You can also use the SQL values.
Isn’t it insane to change your types to accommodate this? You have to change all fields accesses, it behaves differently, you lose non-nullability…
Go is simple and "default case" which is simple and works for most cases... and then you have edge cases where you need to bend over backwards.
As an exercise, try to use comma (",") in JSON field name.
Re: The Serde Rust Framework
#97Because Serde is a common API for pretty much all serialisation formats supported in Rust, it's very easy to try multiple formats to choose the best size/speed you need. JSON too big? You can make it CBOR or Msgpack with a couple of lines. Want faster? Call bincode instead.
Yeah, the format independence is amazing. In Pernosco we use bincode for IPC payloads and when we want to log IPCs we just serialize the payloads to JSON with zero developer effort.
Re: The Serde Rust Framework
#98Earlier quoted context omitted.
I wrote a lot of Java back in the day. I elaborated on why I disagree in the other comment. I do not know if the situation in C# is similar.
For context, the example from the Serde wiki written in Java using Jackson public record Point(int x, int y) {} // [...] var objectMapper = new ObjectMapper(); var jsonString = objectMapper.writeValueAsString(new Point(2,1)); It isn't obvious to me what advantage Serde offers that you couldn't get with Jackson or other similar libraries in Java (although I get that Java and Rust are different languages, and that ther…
Re: The Serde Rust Framework
#99Earlier quoted context omitted.
These concerns are valid. Data serialization/deserialization is an extremely standard thing to need to be able to do. It's not the user's fault if they use the obvious tool for an obvious job and suddenly have bloated compile times.
Seems like there’s opportunity for tighter integration with the compiler where serde moves into the compiler or there are acceleration primitives rather than doing the AST macros thing. This sounds similar to the problems of c++ template bloat.
For example you can pre-instatiate the most common type parameters and just stuck those instantiations into a binary library.
Also if you have a nice C++ compiler environment like Visual C++, you can even do edit-and-continue for many kinds of changes, and VS 2022 will double down on that capability.
Re: The Serde Rust Framework
#100Earlier quoted context omitted.
I think in most ecosystems it's easier to use a prebuilt serializer/deserializer than your own format.
Nah, not like it is in Rust. For the vast majority of projects, serde makes parsing / serializing something they don't even have to think about , without having to sacrifice type safety to get there. Until you experience that in a large project with a lot of moving parts and developers, it's hard to appreciate just how many developer cycles you were wasting before on stuff that can be totally automated. It's format-a…