处理 `assemblyscript-json` 中对无效 JSON 格式的异常,使用 `JSON.parse`。

huangapple go评论55阅读模式
英文:

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 = &lt;JSON.Obj&gt;(JSON.parse(&#39;{&quot;hello&quot;: &quot;world&quot;, &quot;value&quot;: 24}&#39;));

but if jsonString has an invalid json format, it breaks my subgraph.

Using:

&quot;@graphprotocol/graph-cli&quot;: &quot;0.35.0&quot;,
&quot;@graphprotocol/graph-ts&quot;: &quot;0.29.0&quot;,
&quot;assemblyscript-json&quot;: &quot;1.1.0&quot;

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 &quot;json-as&quot;;

export function test(): Player {
  // @ts-ignore
  const data: Player = {
    firstName: &quot;Emmet&quot;,
    lastName: &quot;West&quot;,
    lastActive: [8, 27, 2022],
    age: 23,
    pos: {
      x: -3.4,
      y: 1.2
    },
    isVerified: true
  }

  return JSON.parse&lt;Player&gt;(&quot;[1, 2, 3]&quot;); // 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.

huangapple
  • 本文由 发表于 2023年2月6日 17:30:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定