2024-02-07 13:41:37 +11:00
|
|
|
import std/json as stdjson
|
2024-02-07 09:40:48 +11:00
|
|
|
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
2024-05-16 17:57:42 +10:00
|
|
|
import ./errors
|
2024-02-07 13:41:37 +11:00
|
|
|
import ./types
|
2024-02-07 09:40:48 +11:00
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
2024-02-09 11:08:14 +11:00
|
|
|
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:
|
2024-05-16 17:57:42 +10:00
|
|
|
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:
|
2024-02-08 17:01:38 +11:00
|
|
|
return failure newException(JsonParseError, e.msg, e)
|