Hi. I am learning to parse JSON data with serde lib. I tried to write code from example.
import { JSON; } "mo:serde";
type User = {
name: Text;
id: Nat;
email: ?Text;
};
public func parse() : async (?[User]) {
let jsonText = "[{\"name\": \"John\", \"id\": 123}, {\"name\": \"Jane\", \"id\": 456, \"email\": \"jane@gmail.com\"}]";
let #ok(blob) = JSON.fromText(jsonText, null); // you probably want to handle the error case here :)
let users : ?[User] = from_candid(blob);
return users;
};
I getting error when i run “parse” function.
baium@vm-baium:~/Desktop/test-dfx/serde$ dfx canister call serde_backend parse
Error: Failed update call.
Caused by: The replica returned a rejection error: reject code CanisterError, reject message Error from Canister napoe-hmaaa-aaaaa-qabmq-cai: Canister called `ic0.trap` with message: 'assertion failed at Encoder.mo:937.17-937.68'
Canister Backtrace:
encode_compound_type
encode_candid
encode_compound_type
encode_candid
one_shot_encode
one_shot
encode
encodeOne
fromText
$lambda
parse
.
Consider gracefully handling failures from this canister or altering the canister to handle exceptions. See documentation: https://internetcomputer.org/docs/current/references/execution-errors#trapped-explicitly, error code Some("IC0503")
Need some help. What i did wrong or may be i need additional libs?
My mops.toml:
[dependencies]
serde = "3.2.2"