nim-serde/serde/json/parser.nim

19 lines
518 B
Nim
Raw Normal View History

import std/json as stdjson
2024-02-07 09:40:48 +11:00
import pkg/questionable/results
2025-05-16 18:24:50 +05:30
import ../utils/errors
import ../utils/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)