natlabs / serde Goto Github PK
View Code? Open in Web Editor NEWSerialization and Deserialization library for motoko
License: MIT License
Serialization and Deserialization library for motoko
License: MIT License
Greetings,
Thanks for this wonderful library. I'm using it to pull JSON in motoko from rosetta and in the response some of the named object properties are of the word "type" which happens to be a reserved keyword in motoko thus the compiler complains.
How do you go about handling this case? In other languages such as c# they use the "@" symbol infront of the property eg @type or @Class
Thanks again for your wonderful library..
I have a question, is this even possible? I'm trying to call an ETH RPC using HTTP outcalls. Everything works for simple requests, but when I need to send more complex parameters I'm hitting a blocker.
The required JSON to query for example, a chainlink feed to get the latest ETH/USD price is as such:
{"jsonrpc":"2.0","id":5423031679576389,"method":"eth_call","params":[{"data":"0xfeaf968c","to":"0x5f4ec3df9cbd43714fe2740f5e3616155c5b8419"},"latest"]}
However, this type I think is impossible to construct in Motoko? As the params field looks like a mixed type array of object + strings?
Can you think of a structure that can support this for serialization/deserialization using serde? I came up with this but doesn't work as the compiler throws a warning if you try to put mixed types in an array. I'm thinking this type of call is not possible using custom types as the compiler will never allow an array of [?{}, ?Text] correct?
In one debug screenshot you can see the resulting serde jsonRequest params field is an empty object array
Set jsonRequest: "{"id": 1, "jsonrpc": "2.0", "method": "eth_call", "params": [{}]}"
Greetings,
I'm seeing the JSON parser fail when encountering a field with a FLOAT type? For example, this works fine
const data = "{\"code\":\"EUR\",\"alphaCode\":\"EUR\",\"numericCode\":\"978\",\"name\":\"Euro\",\"rate\": \"0.92365973383771\"}";
but this does not:
const data = "{\"code\":\"EUR\",\"alphaCode\":\"EUR\",\"numericCode\":\"978\",\"name\":\"Euro\",\"rate\": 0.92365973383771}";
The 2nd example has a FLOAT type field for "rate" (the first example would be a string because its text-qualified)
When I run standard code like let t = JSON.fromText(data, null);
it throws an error for the 2nd case which is
#err("Failed to parse JSON text")
Do you think its a problem with the Float type? I could change my webservice to force text-qualify all fields as a work-around but I figured i'd report this potential issue to see if you were aware of it. Thank you.
when i search for json
on mops this library does not show up
serde 1.0.2
moc 0.9.8
let jsonText = "[{ \"a\": [] }, { \"a\": [ \"x\" ] }]";
type Res = [{ a : [Text] }];
let res : ?Res = from_candid(JSON.fromText(jsonText, null));
Failing with error: wasm trap: wasm `unreachable` instruction executed
If I add item to first empty array - parses successfully:
-let jsonText = "[{ \"a\": [] }, { \"a\": [ \"x\" ] }]";
+let jsonText = "[{ \"a\": [\"v\"] }, { \"a\": [ \"x\" ] }]";
type Res = [{ a : [Text] }];
let res : ?Res = from_candid(JSON.fromText(jsonText, null));
Getting an error on JSON.toText(blob, field_keys);
. Any thoughts? Thanks!
public func motokoToJson(user : User) : async Text {
let blob = to_candid (user);
let field_keys = ["name", "id"];
JSON.toText(blob, field_keys);
};
error:
expression of type
Result__6<Text, Text> = {#err : Text; #ok : Text}
cannot produce expected type
TextMotoko(M0096)
expression of type
(Blob, [Text])
cannot produce expected type
(Blob, [Text], ?Options)Motoko(M0096)
i cannot use serde
for my project, this dapp is not working locally with serde
as a mops dependency. it works with vessel
though. i reuploaded it to mops with this mops.toml
under the name motoko-serde
and now it works.
[package]
name = "motoko-serde"
version = "0.1.4"
description = "A serialisation and deserialisation library for Motoko."
repository = "https://github.com/NatLabs/serde"
[dependencies]
base = "https://github.com/dfinity/motoko-base#moc-0.7.4"
json = "https://github.com/aviate-labs/json.mo#v0.2.0"
itertools = "https://github.com/NatLabs/Itertools#master"
candid = "https://github.com/gekctek/motoko_candid#main"
"xtended-numbers" = "https://github.com/gekctek/motoko_numbers#v1.0.0"
"parser-combinators" = "https://github.com/aviate-labs/parser-combinators.mo#master"
please consider upgrading the dependencies
i'm trying to convert multidimensional array to JSON but it is not working. it's give me Failed to decode candid blob kind of error.
in single array it's working good but multidimensional not working.
How to solve this error?
Hi there, thanks a ton for this library! With my use case being a bit more complicated, I was wondering if you could update the sample to demonstrate how to convert a JSON payload of the following form to an array in motoko.
JSON payload -
{
"message":"Give data retrieved successfully",
"data":[
{
"parentNonprofits":[
"vLvKLwLxUe94aCvbVbQS"
],
"userId":"29zKJK2xytOQY3VR7JnbI9HIkPp2",
"giverEmailId":"[email protected]",
"giveId":"kPCeIOil0bN7zBnLE1hb",
"createdOn":"2023-09-12T11:26:35.311000+00:00",
"feathersAmount":3400,
"giverId":"29zKJKS6CF7IH5vLvKLw",
"projectId":"r6L8zEgEKFS2zsQB2Cc5"
},
{
"parentNonprofits":[
"vLvKLwLxUe94aCvbVbQS"
],
"userId":"29zKJK2xytOQY3VR7JnbI9HIkPp2",
"giverEmailId":"[email protected]",
"giveId":"5M6bFh6Fb1yTd8LuLJrz",
"createdOn":"2023-09-10T07:16:16.715000+00:00",
"feathersAmount":800,
"giverId":"29zKJKS6CF7IH5vLvKLw",
"projectId":"r6L8zEgEKFS2zsQB2Cc5"
}
]
}
I expect to be able to process it something like
records.add(jsonObj.data)
where records is a buffer or list data structure containing my custom type.
Essentially, how do I process a JSON array using this library and get an array/list/buffer object in motoko?
JSON.fromText
traps if unable to parse JSON text, and it is impossible to catch that.
What if change it to return ?Blob
instead of Blob
+trap?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.