nim-serde/serde/json/parser.nim

19 lines
504 B
Nim
Raw Normal View History

import std/json as stdjson
2024-02-07 09:40:48 +11:00
import pkg/questionable/results
import ./errors
import ./types
2024-02-07 09:40:48 +11:00
{.push raises: [].}
proc parse*(_: type JsonNode, json: string): ?!JsonNode =
# Used as a replacement for `std/json.parseJson`. Will not raise Exception like in the
# standard library
2024-02-07 09:40:48 +11:00
try:
without val =? stdjson.parseJson(json).catch, error:
return failure error.mapErrTo(JsonParseError)
return success val
2024-02-07 09:40:48 +11:00
except Exception as e:
return failure newException(JsonParseError, e.msg, e)