mehcode / config-rs

⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
Apache License 2.0
2.43k stars 206 forks source link

Implement serde::Serialize for Config #508

Open nectariner opened 6 months ago

nectariner commented 6 months ago

I want to be able to serialize my config to json so that when I log it Cloudwatch formats it correctly. I'm not aware of another simple way of doing this, it's unfortunate since the regular debug output is so close to what I need but Cloudwatch formats it horribly since it's not actual json

matthiasbeyer commented 6 months ago

You can use Config::try_deserialize() to deserialize config to your own type and have that implement Serialize!

nectariner commented 6 months ago

Apologies if this is a stupid question (I'm not familiar with the codebase whatsoever) - is there a reason why it couldn't implement Serialize itself?

matthiasbeyer commented 6 months ago

Hm, I don't know to be honest. One thing I could imagine is that serializing does more than just "dumping the data to some format", but actually includes logic that is a bit more on the heavy side of things? Like... it actually has to compute stuff to be even able to provide the data that is needed for the serialization, and because of that implementing Serialize directly would give a user a false sense of "cost" for this functionality?

But to be honest, I don't really know.