# Deserialize to candid::Nat

**URL:** <https://forum.dfinity.org/t/deserialize-to-candid-nat/8192>\
**Category:** Developers\
**Created:** [October 28, 2021, 5:12am UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192 "2021-10-28T05:12:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 5:12am UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/1 "2021-10-28T05:12:47Z")

</div>

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.

Here’s a link to the issue with more information: [Deserialize to candid::Nat · Issue #289 · dfinity/candid · GitHub](https://github.com/dfinity/candid/issues/289)

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 28, 2021, 4:06pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/2 "2021-10-28T16:06:58Z")

</div>

Can you post the error message in the issue?

---

<div class="post-metadata">

**Author:** ![rckprtr](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rckprtr/32/809_2.png) [@rckprtr](https://forum.dfinity.org/u/rckprtr)\
**Post date:** [October 28, 2021, 4:25pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/3 "2021-10-28T16:25:18Z")

</div>

Without the error message, Im guessing Rust WASM doesn’t support arbitrary length integers? Nat8, Nat32, Nat64…

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 4:31pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/4 "2021-10-28T16:31:47Z")

</div>

```auto
Error("invalid type: integer `100`, expected Nat value", line: 1, column: 33)

```

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 4:37pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/5 "2021-10-28T16:37:12Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 28, 2021, 4:46pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/6 "2021-10-28T16:46:43Z")

</div>

I see. I can add the visitor for integer. `Nat` currently only takes a specific internal representation for deserialization.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 4:56pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/7 "2021-10-28T16:56:03Z")

</div>

It would be amazing to allow any JSON number to be deserialized to candid::Nat!

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 4:56pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/8 "2021-10-28T16:56:49Z")

</div>

Do you have an ETA on this? Just wondering if I should use something like [#[serde(deserialize\_with)]](https://users.rust-lang.org/t/need-help-with-serde-deserialize-with/18374) 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?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 28, 2021, 5:17pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/9 "2021-10-28T17:17:47Z")

</div>

Can you try this branch: [add more visitors for nat and int by chenyan-dfinity · Pull Request #290 · dfinity/candid · GitHub](https://github.com/dfinity/candid/pull/290)

I didn’t look at which integer type is used by serde\_json, but I think it should be enough.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 6:11pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/10 "2021-10-28T18:11:58Z")

</div>

Thank you! I will try this branch and let you know how it goes

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 7:14pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/11 "2021-10-28T19:14:17Z")

</div>

I’m trying to use that branch, but now my code won’t compile where it would compile before. Perhaps there are some issues with the ic-cdk version?

For this struct that worked with version `0.7.8` of candid:

```auto
#[derive(serde::Deserialize, candid::CandidType)]
        struct TransferRequest {
            from: String,
            to: String,
            amount: candid::Nat
        }

```

I get this error now: `the trait bound `TransferRequest: ic_cdk::export::candid::CandidType` is not satisfied`

If I change the struct to look like this:

```auto
#[derive(serde::Deserialize, ic_cdk::export::candid::CandidType)]
        struct TransferRequest {
            from: String,
            to: String,
            amount: candid::Nat
        }

```

Then I get the following error:

```auto
the trait `ic_cdk::export::candid::CandidType` is not implemented for `candid::Nat`

```

I’m using `ic-cdk = "0.3.2"`

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 28, 2021, 7:31pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/12 "2021-10-28T19:31:19Z")

</div>

Right, the second code is correct. You will need to patch Cargo.toml.

```auto
[patch.crates-io.candid]
git = "https://github.com/dfinity/candid.git"
branch = "nat-visitor"

```

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 8:10pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/13 "2021-10-28T20:10:17Z")

</div>

> [@chenyan](#):
>
> ```auto
> [patch.crates-io.candid]
> git = "https://github.com/dfinity/candid.git"
> branch = "nat-visitor"
> 
> ```

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?

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 28, 2021, 8:12pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/14 "2021-10-28T20:12:59Z")

</div>

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](https://github.com/lastmjs/azle), so if I run into any more issues maybe I’ll just post to this thread if the problems are similar

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 28, 2021, 8:30pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/15 "2021-10-28T20:30:41Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 29, 2021, 3:27pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/16 "2021-10-29T15:27:52Z")

</div>

@chenyan Now I need to get my Rust structs serialized into JSON.

Imagine I have this struct:

```auto
#[derive(serde::Serialize, serde::Deserialize, ic_cdk::export::candid::CandidType)]
struct TransferRequest {
    nat: ic_cdk::export::candid::Nat
}

```

I am trying to take a value of type `TransferRequest` and serialize it into JSON like this:

```auto
serde_json::to_string(&transferRequest);

```

But I get the following error:

```bash
^^^^^^ the trait `Serialize` is not implemented for `ic_cdk::export::candid::Nat`

```

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 29, 2021, 4:55pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/17 "2021-10-29T16:55:36Z")

</div>

You know, the documentation here says I can just use `u128` in Rust instead of `candid::Nat`…is that true? [Supported types :: Internet Computer](https://sdk.dfinity.org/docs/candid-guide/candid-types.html#type-nat)

Any best practices? Seems much easier to just use `u128`.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 29, 2021, 6:56pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/18 "2021-10-29T18:56:54Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [March 29, 2022, 5:49pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/19 "2022-03-29T17:49:07Z")

</div>

I am running into this issue again and would love for it to be addressed, not as easy to workaround now: [the trait bound `ic_cdk::export::candid::Nat: Serialize` is not satisfied · Issue #331 · dfinity/candid · GitHub](https://github.com/dfinity/candid/issues/331)

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [March 29, 2022, 5:49pm UTC](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192/20 "2022-03-29T17:49:50Z")

</div>

And yet it seems to be implemented for candid::Principal, as I don’t run into the same error when using a candid::Principal value.

[Next page](https://forum.dfinity.org/t/deserialize-to-candid-nat/8192.md?page=2)
