7 ms·
i dont think manual ser/de is common at all, and languages like dart where it was used is a massive pain point for people so much that they are adding macros to
by beanjuiceII 2y ago
i dont think manual ser/de is common at all, and languages like dart where it was used is a massive pain point for people so much that they are adding macros to the language and the first macro they add is for serialization. whats not explicit about saying hey i have a struct this is the data i expect, serialize/deseralize in this shape, validation is a another but separate concern. in javascript you are not doing anything manually so i'm not sure why thats an example?
- __MatrixMan__ 2y agoI'm a bit confused. How can you control how your data is serialized if not manually? Are there languages that use some kind of magically-figures-it-out layer that negotiates the appropriate serialization on the fly?
- googledocsftw 2y agoC# (or more precisely .NET libraries) does it using reflection. Attributes let you adjust the behaviour.
- neonsunset 2y agoOr with build-time source generation (because this specific pattern of reflection is AOT-unfriendly). It's not as convenient if you are using default serializer options, but if you don't - it ties together JsonTypeInfo<T> and JsonSerializerOptions, so it ends up being a slightly terser way to write it. I do prefer the Rust-style serde annotations however. record User(string Name, DateOnly DoB); [JsonSerializable(typeof(User))] partial class SerializerContext: JsonSerializerContext; ... var user = new User("John", new(1984, 1, 1)); var response = await http.PostAsJsonAsync( url, user, SerializerContext.Default.User);
- yawaramin 2y agoMany languages have some kind of macro or codegen system that allows serializing or deserializing based on type definitions. Eg (pseudocode): @deriving(json) class Person: id: int name: str Would give you something like: def parse(s: str): Person: ... def print(p: Person): str: ...
- __MatrixMan__ 2y agoI see, thanks. I thought maybe we were talking about the choice of json vs something else being automatic and chosen at runtime.
- steve_adams_86 2y agoSorry I wasn’t clear; I meant to use JavaScript as an example where it isn’t manual. Despite it being easy to use, I find I inevitably wind up requiring a lot of ceremony and effort to ensure it’s safe. I’m not a huge fan of automatic serialization in that it appears to work fine when sometimes it shouldn’t/won’t. I agree that it’s a lot of effort though. I guess the question is if you want the effort up front or later on. I prefer up front, I guess.