Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs are outdated. How to call JSON.stringify()? #320

Open
ryancwalsh opened this issue Nov 30, 2021 · 8 comments
Open

Docs are outdated. How to call JSON.stringify()? #320

ryancwalsh opened this issue Nov 30, 2021 · 8 comments

Comments

@ryancwalsh
Copy link

I'm looking for a replacement of JSON.stringify() that I can call in my NEAR AssemblyScript contract.

I found this repo.

1edab43 commit message "Made JSON.stringify comptible with javascript JSON" (sic) was encouraging.

But

namespace _JSON {
doesn't seem to expose stringify(), from what I can tell.

And even encoder.stringify() mentioned at https://github.com/near/assemblyscript-json/blob/1edab439e33acd787671618d33c1ec4250f523c9/README.md#encoding-json doesn't seem to be available.

What am I misunderstanding? Thanks!

@willemneal
Copy link
Contributor

I'd use @serial-as/json. See https://github.com/gagdiez/serial-as/tree/main/json

@ryancwalsh
Copy link
Author

ryancwalsh commented Nov 30, 2021

@willemneal Thanks. I also had found https://www.npmjs.com/package/json-as (which I guess I will use) but felt inclined to rely on a NEAR repo instead (this one).

@mariusa
Copy link

mariusa commented Nov 15, 2022

So this entire module can't be used for JSON.stringify(obj) ?

@willemneal
Copy link
Contributor

See the link above serial-as/json:

import * as JSON from '@serial-as/json'

@serializable
class Pair{
  x: i32 = 0,
  y: i32 = 0
}

let pair: Pair = {x:1, y:2}

// `serialized` is the string "{"x":1,"y":2}"
let serialized: string = JSON.stringify(object)  

@mariusa
Copy link

mariusa commented Nov 15, 2022

Thanks! So there are 3 npm modules for JSON support in assemblyscript (rather than being built-in)

https://www.npmjs.com/package/assemblyscript-json
https://www.npmjs.com/package/json-as
https://www.npmjs.com/package/@serial-as/json

What a mess :(

@willemneal
Copy link
Contributor

The serial-as still uses this library for the JSON types, but rather is part of a serializing framework. It should be part of the language, to match with JS, but any of these libraries do work relatively well.

@mariusa
Copy link

mariusa commented Nov 15, 2022

Thanks again! Saw that json-as requires a class defined for each object property. Does serial-as too?
(feel free to answer in gagdiez/serial-as#38 )

@JairusSW
Copy link

JairusSW commented Dec 7, 2022

@mariusa, json-as used to generate its own classes so that you could create a Object Literal which would then be replaced by the generated class, but that broke as AS updated. Working on re-implementing that feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants