nim-serde/serde/json/parser.nim
Eric 10271bd494
feat: improve deserialization from string (#23)
Improves how types handle deserialization from string, including Option[T], seq[T], and Option[seq[T]]
Empty and null strings are no longer deserialized to 0, instead an error Result is returned
If an error occurs when parsing json, a JsonParseError is returned.
2024-05-16 17:57:42 +10:00

19 lines
504 B
Nim

import std/json as stdjson
import pkg/questionable/results
import ./errors
import ./types
{.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
try:
without val =? stdjson.parseJson(json).catch, error:
return failure error.mapErrTo(JsonParseError)
return success val
except Exception as e:
return failure newException(JsonParseError, e.msg, e)