英文:
Handling exception for invalid json format for `assemblyscript-json` in `JSON.parse`
问题
以下是翻译好的代码部分:
let jsonObj: JSON.Obj = <JSON.Obj>(JSON.parse('{"hello": "world", "value": 24}'));
但如果jsonString具有无效的JSON格式,它会破坏我的子图。
使用:
"@graphprotocol/graph-cli": "0.35.0",
"@graphprotocol/graph-ts": "0.29.0",
"assemblyscript-json": "1.1.0"
由于assemblyscript不支持错误处理(try/catch),是否有一种方法可以处理具有assemblyscript-json的无效json字符串?
英文:
The following works:
let jsonObj: JSON.Obj = <JSON.Obj>(JSON.parse('{"hello": "world", "value": 24}'));
but if jsonString has an invalid json format, it breaks my subgraph.
Using:
"@graphprotocol/graph-cli": "0.35.0",
"@graphprotocol/graph-ts": "0.29.0",
"assemblyscript-json": "1.1.0"
Since assemblyscript doesn't have support for error handling (try/catch) is there a way to handle an invalid json string with assemblyscript-json ?
答案1
得分: 2
Exception handling (and error handling in general) is currently hard to do in AssemblyScript, because the developers are waiting for the WebAssembly exception proposal to go through.
The assemblyscript-json
package is recommended by the documentation, but it seems, at least to me from afar, to be unmaintained actively. Maybe the json-as
package would prove to be useful for you. Still no exception handling, but it does not fail on invalid jsons, it merely returns an object with all nulls and zeros, so you can check it more easily.
import { JSON } from "json-as";
export function test(): Player {
// @ts-ignore
const data: Player = {
firstName: "Emmet",
lastName: "West",
lastActive: [8, 27, 2022],
age: 23,
pos: {
x: -3.4,
y: 1.2
},
isVerified: true
}
return JSON.parse<Player>("[1, 2, 3]"); // invalid json
}
This, for me, returns:
{
firstName: null,
lastName: null,
lastActive: null,
age: 0,
pos: null,
isVerified: false
}
In order to install the package, be sure to call:
npm install --save json-as
because that is the name on the npm, as opposed to the name on github. You can check the package documentation on github, to ensure that this is correct.
英文:
Exception handling (and error handling in general) is currently hard to do in AssemblyScript, because the developers are waiting for the WebAssembly exception proposal to go through.
The assemblyscript-json
package is recommended by the documentation, but it seems, at least to me from afar, to be unmaintained actively. Maybe the json-as
package would prove to be useful for you. Still no exception handling, but it does not fail on invalid jsons, it merely returns an object with all nulls and zeros, so you can check it more easily.
import { JSON } from "json-as";
export function test(): Player {
// @ts-ignore
const data: Player = {
firstName: "Emmet",
lastName: "West",
lastActive: [8, 27, 2022],
age: 23,
pos: {
x: -3.4,
y: 1.2
},
isVerified: true
}
return JSON.parse<Player>("[1, 2, 3]"); // invalid json
}
This, for me, returns:
{
firstName: null,
lastName: null,
lastActive: null,
age: 0,
pos: null,
isVerified: false
}
In order to install the package, be sure to call:
npm install --save json-as
because that is the name on the npm, as opposed to the name on github. You can check the package documentation on github, to ensure that this is correct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论