I’ve also opened this up on the candid repo. Does anyone have experience using serde_json with candid::Nat with Rust? I can’t get a simple JSON string to deserialize to a Rust struct with a field that is of type candid::Nat.
It’s a deserialization error, that’s what the candid::Nat type is for, since there is no native Rust representation of a candid Nat. The problem is getting serde to deserialize a JSON number to a candid::Nat.
Do you have an ETA on this? Just wondering if I should use something like #[serde(deserialize_with)] in the mean time. I assume I would just manually implement a visitor for this field for now, and then when the candid library is updated I can just remove that code. And our code would essentially be doing the same thing?
Oh I see, should I not be installing candid directly? I’ve been installing candid and using candid::Nat and candid::CandidType. Should I be doing ic_cdk::export::candid:: always, generally speaking?
It all works!!! Thank you so much for getting this fixed and pointing me in the right direction. I will be doing a lot of deserialization in Rust for Azle GitHub - lastmjs/azle: JavaScript/TypeScript CDK for the Internet Computer, so if I run into any more issues maybe I’ll just post to this thread if the problems are similar
ic_cdk::export::candid is the current recommendation. This avoids candid version mismatch between ic-cdk and user’s import. Not sure if it’s the best way to handle version mismatch.
Serialize is not needed for Candid, so it’s not implemented for candid::Nat. You are right, you can use u128 for nat in Rust. The limitations are 1) u128 is always 128bit, while candid::Nat can take fewer space for smaller numbers; 2) You cannot decode numbers out of the 128bit range. Otherwise, they are identical.