#[derive(CandidType)] with serde_json

So I have a struct with a field that is of type serde_json::Value. I keep getting this error: error[E0277]: the trait bound sudograph::serde_json::Value: CandidType is not satisfied.

I’m not sure if there is an easy way to use a macro to satisfy the trait for serde_json::Value. Is there? If not, do I need to implement the CandidType for serde_json::Value by myself?

Any help is greatly appreciated.

If you cannot derive CandidType, then you need to implement it. There are some examples here: candid/impls.rs at master · dfinity/candid · GitHub


Perfect, this should help a lot, thank you

Wold you consider adding an implementation for CandidType for serde_json::Value in the candid library? Seems there are already some other serde implementations, and being able to send JSON structures between canisters seems like something people would want (I know I want it for Sudograph).

1 Like

I’m running into some issues because serde_json::Value and CandidType are both not defined in my crate, thus I get the error[E0117]: only traits defined in the current crate can be implemented for arbitrary types. I am using serde_json::Value as the types for some fields on structs that I am automatically generating in my own procedural macro. The async_graphql library seems to be built to handle serde_json::Value directly, thus I don’t want to have to create some kind of wrapper type to be able to implement CandidType.

Is there any way to provide the CandidType functionality now, without creating some kind of wrapper type over serde_json::Value in my crate?

1 Like

New type wrapper is a common pattern in Rust, e.g. struct JsonValue(serde_json::Value), then you can implement CandidType for JsonValue. Maybe you can start with this, and then see if there is a need to include it directly in the candid crate?

Also, I think there is a way to transform serde_json::Value to native Rust values, then you can use candid to send the values over the wire.