Code Monkey home page Code Monkey logo

serde's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

serde's Issues

Deserialization of types with reserved keywords?

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

capture2
/100089505/a16b6bf6-b6f0-4bbe-97d2-7160efc1efa6)

Capture

Serializing a motoko object with an array parameter of mixed types... is this even possible?

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?

error
ethjsonrequest
chainlink latestround
ethjson

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": [{}]}"

Json.FromText fails with float types?

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")
float serialization

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.

Failing to parse specific json

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));

JSON.toText() error

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)

dependencies for mops faulty

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

Motoko to JSON Multidimensional Aarray

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?

JSON array to Motoko array

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.