3 ms·
I'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 t
by __MatrixMan__ 2y ago
I'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.