3 ms·
Serde [1] creator here. I think serde can do this out of the box. Is it missing something you need? [1]: https://github.com/serde-rs/serde https://github.com/s
by erickt 10y ago
Serde [1] creator here. I think serde can do this out of the box. Is it missing something you need?
[1]: https://github.com/serde-rs/serde https://github.com/serde-rs/serde
- rubber_duck 10y agoLooking at it - it seems like a serialization framework ala .NET ISerializable, while that's a part of what I need I also need a way to dump code metadata out to some file (like JSON) at compile time and then use that data to auto generate the client API to access the data (but that would be done outside of rust). And since I can constrain my models serialization part wouldn't benefit much from a framework - most of the complexity would be in extracting the metadata and validating model constraints. I'm not sure if Serde helps with those I can't tell from github ? (also the reference links in the readme are broken, eg. http://serde-rs.github.io/serde/serde/serde/ser/trait.Serializer.html http://serde-rs.github.io/serde/serde/serde/ser/trait.Serial... - seems like one too many /serde)
- erickt 10y agoThanks for finding that bug! In a way, Serde itself is all about extracting the metadata of a type and handling validation issues. However this is metadata is currently not exposed to end users. It's something I've thought about doing, but I haven't had a driving use case to help come up with a proper API for it. I don't think it'd be that hard to implement once we actually have a clear idea on what we want. The code Serde generates though typically optimizes down into nearly the same code as a hand written serializer and deserializer. On my laptop, serde_json [1] serializes a particular micro-benchmark [2] that serializes as fast as a hand rolled serializer (416MB/s vs 414MB/s). In comparison, rapidjson serializes the same structure in 432MB/s. I didn't write a hand written deserializer, but serde_json is comparable to rapidjson (193MB/s vs 182MB/s). So it may be fast enough that you might be able to just implement whatever you want by just using Serde directly without having to generate parsers. [1]: https://github.com/serde-rs/json https://github.com/serde-rs/json [2]: https://github.com/serde-rs/json/blob/master/json_tests/benches/bench_json.rs https://github.com/serde-rs/json/blob/master/json_tests/benc... [3]: https://github.com/erickt/rust-serialization-benchmarks/tree/master/c%2B%2B https://github.com/erickt/rust-serialization-benchmarks/tree...
- untothebreach 10y agoNot sure if this fits the bill, but you can do code generation at compile time with cargo using build scripts: http://doc.crates.io/build-script.html http://doc.crates.io/build-script.html I have an as-yet-unpublished crate that takes a JSON API spec and generates an API client for it (in rust) at compile time using this method.