0

I have a JSON file with some repeating object structure and strings like below.

{
  "array": [
    {
      "data": [
            "blih",
            "blah",
            "bloh"
        ]
    },
    ...
  ]
}

My best understanding of Rust would be to deserialize the JSON into a set of structs and then copy the data into another set of structs that includes Rc:

// json structs
#[derive(Serialize, Deserialize)]
struct ArrayJson {
    array: Vec<DataJson>,
}

#[derive(Serialize, Deserialize)]
struct DataJson {
    data: Vec<String>,
}

// rc structs
struct ArrayRc {
    array: Vec<DataRc>,
}

struct DataRc {
    data: Vec<Rc<String>>,
}

Is there a way I can not create two sets of structs and just one?

Update: I believe the rc of serde is not what I want as it serializes and deserializes the actual Rc Arc struct.

Serializing a data structure containing reference-counted pointers will serialize a copy of the inner value of the pointer each time a pointer is referenced within the data structure. Serialization will not attempt to deduplicate these repeated data.

Deserializing a data structure containing reference-counted pointers will not attempt to deduplicate references to the same data. Every deserialized pointer will end up with a strong count of 1.

I only care about the Rc struct on the Rust side, hence why I believe I will need two structs.

NebulaFox
  • 7,813
  • 9
  • 47
  • 65
  • Please more clearly explain why the duplicate ([applied to your situation](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4490163cc52e79df8af4be0a7e3d94d6)) is not valid. – Shepmaster Mar 09 '20 at 15:28
  • Specifically, it's not clear what you mean by "as it serializes and deserializes the actual `Rc` `Arc` struct." – Shepmaster Mar 09 '20 at 15:28
  • That it serializes and deserializes the `Rc` struct, that is, including the reference count. I just want the memory to be managed by `Rc` struct. – NebulaFox Mar 09 '20 at 15:43
  • *including the reference count* — it does not. You can [trivially test that yourself](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dbfe6352f05c1e1437699a9f510d686b). – Shepmaster Mar 09 '20 at 15:46
  • To be clear then, is the rc feature enabled in the playground? Is it that by deafult the `Rc` struct is ignored by enabling rc then causes to seralize/deserialize the `Rc` struct? – NebulaFox Mar 09 '20 at 15:48
  • Yes, [the `rc` feature is enabled](https://github.com/integer32llc/rust-playground/blob/da69e7491da0aed6957956d7e2b165be1ce4a91b/compiler/base/Cargo.toml#L843-L846). – Shepmaster Mar 09 '20 at 15:50
  • 1
    I think the documentation has confused me and have asked a question about it https://stackoverflow.com/questions/60604346/why-does-serde-not-handle-rc-by-default, but I would agree that this question is indeed a duplicate. – NebulaFox Mar 09 '20 at 16:10

1 Answers1

0

Enable the rc feature of the serde crate, and serde will implement Serialize and Deserialize for Rc and Arc. You can then derive the two traits on your ArrayRc and DataRc structures.

For deserialize, it will create a new Rc with a strong count of 1 for every value it deserializes, which sounds like what you want.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85