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

add helpers to make BigInt compatible with JSON.stringify #402

Closed
hoangdv2429 opened this issue Jun 28, 2023 · 5 comments
Closed

add helpers to make BigInt compatible with JSON.stringify #402

hoangdv2429 opened this issue Jun 28, 2023 · 5 comments
Labels
good first issue Good for newcomers

Comments

@hoangdv2429
Copy link
Contributor

hoangdv2429 commented Jun 28, 2023

Currently we can have the option to encode/decode int64 as bigInt instead of Long. However, we're having a problem with display the response because JSON.stringify by default doesn't have the option to parse bigInt, as described in this issue. We should add helper to bigInt so that stringify can work with bigInt.

@hoangdv2429 hoangdv2429 added the good first issue Good for newcomers label Jun 28, 2023
@Zetazzz
Copy link
Collaborator

Zetazzz commented Jul 6, 2023

const test= Proposal.fromPartial({
id: BigInt(1),
status: -1
})
// using toJSON can get string values of bigint inside
const testJson = Proposal.toJSON(test);
console.log(JSON.stringify(testJson));
// and then using fromJSON to get original Proposal type with bigint inside
const origTest = Proposal.fromJSON(testJson);
console.log(origTest)

I have to say it takes one more step... but this's currently the best we can do...

@Zetazzz
Copy link
Collaborator

Zetazzz commented Jul 7, 2023

I believe generating like this can solve the problem, and then we can make all functions that returns the interface(Proposal) using this creation:

function createBaseProposal(): Proposal {
return {
id: BigInt(0),
messages: [],
status: 0,
finalTallyResult: TallyResult.fromPartial({}),
submitTime: undefined,
depositEndTime: undefined,
totalDeposit: [],
votingStartTime: undefined,
votingEndTime: undefined,
metadata: "",
toJSON: function(){
return Proposal.toJSON(this)
}
} as Proposal;
}

I've tested and it's working:

const test= Proposal.fromPartial({
id: BigInt(1),
status: -1
})

// it will call toJSON inside and work
let testString = JSON.stringify(test);

console.log(testString);

let testJson = JSON.parse(testString)
// get back with the string id.
console.log(testJson)

// and then using fromJSON to get original Proposal type with bigint inside
const origTest = Proposal.fromJSON(testJson);
console.log(origTest)

Hope this make sense. If you like the solution, I can make the change.

@hoangdv2429
Copy link
Contributor Author

Lgtm! We can add toJSON to createBase function that have bigInt fields and it should be good!

@hoangdv2429
Copy link
Contributor Author

@Zetazzz can you review the PR above

@hoangdv2429
Copy link
Contributor Author

let ppl define they own way of parsing object with bigInt

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

No branches or pull requests

2 participants