How do I serialize or deserialize an Arc in Serde?

前端 未结 1 1307
有刺的猬
有刺的猬 2020-12-11 01:28

I have a struct that contains children of its own type. These children are wrapped in Arcs, and I\'m getting issues when calling serde_json::to_string

相关标签:
1条回答
  • 2020-12-11 02:07

    Serde provides implementations of Serialize and Deserialize for Arc<T> and Rc<T>, but only if the rc feature is enabled.

    There's a comment on Serde's reference website explaining why:

    Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types does not preserve identity and may result in multiple copies of the same data. Be sure that this is what you want before enabling this feature.

    To enable the rc feature, you need to ask for it in your own Cargo.toml:

    [dependencies]
    serde = { version = "1.0", features = ["rc"] }
    
    0 讨论(0)
提交回复
热议问题