2020-04-22 22:39:56 +00:00
|
|
|
{.experimental: "notnil".}
|
|
|
|
|
2018-12-17 23:01:06 +00:00
|
|
|
import
|
2023-06-11 12:14:50 +00:00
|
|
|
std/[enumutils, tables, macros, strformat, typetraits],
|
|
|
|
stew/[enums, objects],
|
2021-03-19 02:13:55 +00:00
|
|
|
faststreams/inputs, serialization/[formats, object_serialization, errors],
|
2022-02-18 09:26:15 +00:00
|
|
|
"."/[format, types, lexer]
|
2019-01-21 17:40:14 +00:00
|
|
|
|
2020-07-24 19:49:30 +00:00
|
|
|
from json import JsonNode, JsonNodeKind
|
|
|
|
|
2019-01-21 17:40:14 +00:00
|
|
|
export
|
2023-05-27 09:42:08 +00:00
|
|
|
enumutils, inputs, format, types, errors
|
2018-12-17 23:01:06 +00:00
|
|
|
|
|
|
|
type
|
2021-03-18 11:01:06 +00:00
|
|
|
JsonReader*[Flavor = DefaultFlavor] = object
|
2019-07-18 18:20:06 +00:00
|
|
|
lexer*: JsonLexer
|
2020-07-17 20:58:02 +00:00
|
|
|
allowUnknownFields: bool
|
2021-08-11 17:00:34 +00:00
|
|
|
requireAllFields: bool
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2019-03-19 23:54:03 +00:00
|
|
|
JsonReaderError* = object of JsonError
|
2018-12-17 23:01:06 +00:00
|
|
|
line*, col*: int
|
|
|
|
|
|
|
|
UnexpectedField* = object of JsonReaderError
|
2022-06-07 09:35:20 +00:00
|
|
|
encounteredField*: string
|
2018-12-17 23:01:06 +00:00
|
|
|
deserializedType*: cstring
|
|
|
|
|
|
|
|
ExpectedTokenCategory* = enum
|
2020-06-30 19:11:56 +00:00
|
|
|
etValue = "value"
|
2018-12-17 23:01:06 +00:00
|
|
|
etBool = "bool literal"
|
|
|
|
etInt = "integer"
|
2023-05-27 09:42:08 +00:00
|
|
|
etEnumAny = "enum value (int / string)"
|
|
|
|
etEnumString = "enum value (string)"
|
2018-12-17 23:01:06 +00:00
|
|
|
etNumber = "number"
|
|
|
|
etString = "string"
|
|
|
|
etComma = "comma"
|
2020-08-01 16:28:36 +00:00
|
|
|
etColon = "colon"
|
2018-12-17 23:01:06 +00:00
|
|
|
etBracketLe = "array start bracket"
|
|
|
|
etBracketRi = "array end bracker"
|
|
|
|
etCurrlyLe = "object start bracket"
|
|
|
|
etCurrlyRi = "object end bracket"
|
|
|
|
|
2019-07-08 07:57:05 +00:00
|
|
|
GenericJsonReaderError* = object of JsonReaderError
|
|
|
|
deserializedField*: string
|
|
|
|
innerException*: ref CatchableError
|
|
|
|
|
2020-05-30 19:40:17 +00:00
|
|
|
UnexpectedTokenError* = object of JsonReaderError
|
2018-12-17 23:01:06 +00:00
|
|
|
encountedToken*: TokKind
|
|
|
|
expectedToken*: ExpectedTokenCategory
|
|
|
|
|
2020-05-30 19:40:17 +00:00
|
|
|
UnexpectedValueError* = object of JsonReaderError
|
|
|
|
|
2021-08-11 17:00:34 +00:00
|
|
|
IncompleteObjectError* = object of JsonReaderError
|
|
|
|
objectType: cstring
|
|
|
|
|
2020-03-24 18:08:35 +00:00
|
|
|
IntOverflowError* = object of JsonReaderError
|
|
|
|
isNegative: bool
|
2023-11-24 16:18:23 +00:00
|
|
|
absIntVal: BiggestUint
|
2020-03-24 18:08:35 +00:00
|
|
|
|
2021-03-19 02:13:55 +00:00
|
|
|
Json.setReader JsonReader
|
|
|
|
|
2023-06-05 08:23:36 +00:00
|
|
|
{.push gcsafe, raises: [].}
|
|
|
|
|
2020-03-24 18:08:35 +00:00
|
|
|
func valueStr(err: ref IntOverflowError): string =
|
|
|
|
if err.isNegative:
|
|
|
|
result.add '-'
|
|
|
|
result.add($err.absIntVal)
|
|
|
|
|
2020-06-15 22:03:00 +00:00
|
|
|
template tryFmt(expr: untyped): string =
|
|
|
|
try: expr
|
2020-06-22 14:42:50 +00:00
|
|
|
except CatchableError as err: err.msg
|
2020-06-15 22:03:00 +00:00
|
|
|
|
2021-11-18 14:24:59 +00:00
|
|
|
method formatMsg*(err: ref JsonReaderError, filename: string):
|
2023-06-05 08:23:36 +00:00
|
|
|
string {.gcsafe, raises: [].} =
|
2022-03-09 16:24:02 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) Error while reading json file: {err.msg}"
|
2019-03-19 23:54:03 +00:00
|
|
|
|
2021-11-18 14:24:59 +00:00
|
|
|
method formatMsg*(err: ref UnexpectedField, filename: string):
|
2023-06-05 08:23:36 +00:00
|
|
|
string {.gcsafe, raises: [].} =
|
2020-06-15 22:03:00 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) Unexpected field '{err.encounteredField}' while deserializing {err.deserializedType}"
|
2019-03-19 23:54:03 +00:00
|
|
|
|
2021-11-18 14:24:59 +00:00
|
|
|
method formatMsg*(err: ref UnexpectedTokenError, filename: string):
|
2023-06-05 08:23:36 +00:00
|
|
|
string {.gcsafe, raises: [].} =
|
2020-06-15 22:03:00 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) Unexpected token '{err.encountedToken}' in place of '{err.expectedToken}'"
|
2019-03-19 23:54:03 +00:00
|
|
|
|
2021-11-18 14:24:59 +00:00
|
|
|
method formatMsg*(err: ref GenericJsonReaderError, filename: string):
|
2023-06-05 08:23:36 +00:00
|
|
|
string {.gcsafe, raises: [].} =
|
2020-06-15 22:03:00 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) Exception encountered while deserializing '{err.deserializedField}': [{err.innerException.name}] {err.innerException.msg}"
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2021-11-18 14:24:59 +00:00
|
|
|
method formatMsg*(err: ref IntOverflowError, filename: string):
|
2023-06-05 08:23:36 +00:00
|
|
|
string {.gcsafe, raises: [].} =
|
2020-06-15 22:03:00 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) The value '{err.valueStr}' is outside of the allowed range"
|
2020-03-24 18:08:35 +00:00
|
|
|
|
2021-11-18 14:24:59 +00:00
|
|
|
method formatMsg*(err: ref UnexpectedValueError, filename: string):
|
2023-06-05 08:23:36 +00:00
|
|
|
string {.gcsafe, raises: [].} =
|
2020-06-15 22:03:00 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) {err.msg}"
|
2020-05-30 19:40:17 +00:00
|
|
|
|
2021-11-18 14:24:59 +00:00
|
|
|
method formatMsg*(err: ref IncompleteObjectError, filename: string):
|
2023-06-05 08:23:36 +00:00
|
|
|
string {.gcsafe, raises: [].} =
|
2021-08-11 17:00:34 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) Not all required fields were specified when reading '{err.objectType}'"
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
func assignLineNumber*(ex: ref JsonReaderError, lexer: JsonLexer) =
|
|
|
|
ex.line = lexer.line
|
|
|
|
ex.col = lexer.tokenStartCol
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
func raiseUnexpectedToken*(lexer: JsonLexer, expected: ExpectedTokenCategory)
|
2023-06-05 08:23:36 +00:00
|
|
|
{.noreturn, raises: [JsonReaderError].} =
|
2020-05-30 19:40:17 +00:00
|
|
|
var ex = new UnexpectedTokenError
|
2023-12-19 10:00:24 +00:00
|
|
|
ex.assignLineNumber(lexer)
|
|
|
|
ex.encountedToken = lexer.lazyTok
|
2018-12-17 23:01:06 +00:00
|
|
|
ex.expectedToken = expected
|
|
|
|
raise ex
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template raiseUnexpectedToken*(reader: JsonReader, expected: ExpectedTokenCategory) =
|
|
|
|
raiseUnexpectedToken(reader.lexer, expected)
|
|
|
|
|
|
|
|
func raiseUnexpectedValue*(
|
|
|
|
lexer: JsonLexer, msg: string) {.noreturn, raises: [JsonReaderError].} =
|
2020-05-30 19:40:17 +00:00
|
|
|
var ex = new UnexpectedValueError
|
2023-12-19 10:00:24 +00:00
|
|
|
ex.assignLineNumber(lexer)
|
2020-05-30 19:40:17 +00:00
|
|
|
ex.msg = msg
|
|
|
|
raise ex
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template raiseUnexpectedValue*(r: JsonReader, msg: string) =
|
|
|
|
raiseUnexpectedValue(r.lexer, msg)
|
|
|
|
|
|
|
|
func raiseIntOverflow*(
|
|
|
|
lexer: JsonLexer, absIntVal: BiggestUint, isNegative: bool)
|
|
|
|
{.noreturn, raises: [JsonReaderError].} =
|
2020-03-24 18:08:35 +00:00
|
|
|
var ex = new IntOverflowError
|
2023-12-19 10:00:24 +00:00
|
|
|
ex.assignLineNumber(lexer)
|
2020-03-24 18:08:35 +00:00
|
|
|
ex.absIntVal = absIntVal
|
|
|
|
ex.isNegative = isNegative
|
|
|
|
raise ex
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template raiseIntOverflow*(r: JsonReader, absIntVal: BiggestUint, isNegative: bool) =
|
|
|
|
raiseIntOverflow(r.lexer, absIntVal, isNegative)
|
|
|
|
|
|
|
|
func raiseUnexpectedField*(
|
|
|
|
lexer: JsonLexer, fieldName: string, deserializedType: cstring)
|
|
|
|
{.noreturn, raises: [JsonReaderError].} =
|
2018-12-17 23:01:06 +00:00
|
|
|
var ex = new UnexpectedField
|
2023-12-19 10:00:24 +00:00
|
|
|
ex.assignLineNumber(lexer)
|
2018-12-17 23:01:06 +00:00
|
|
|
ex.encounteredField = fieldName
|
|
|
|
ex.deserializedType = deserializedType
|
|
|
|
raise ex
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template raiseUnexpectedField*(r: JsonReader, fieldName: string, deserializedType: cstring) =
|
|
|
|
raiseUnexpectedField(r.lexer, fieldName, deserializedType)
|
|
|
|
|
|
|
|
func raiseIncompleteObject*(
|
|
|
|
lexer: JsonLexer, objectType: cstring)
|
|
|
|
{.noreturn, raises: [JsonReaderError].} =
|
2021-08-11 17:00:34 +00:00
|
|
|
var ex = new IncompleteObjectError
|
2023-12-19 10:00:24 +00:00
|
|
|
ex.assignLineNumber(lexer)
|
2021-08-11 17:00:34 +00:00
|
|
|
ex.objectType = objectType
|
|
|
|
raise ex
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template raiseIncompleteObject*(r: JsonReader, objectType: cstring) =
|
|
|
|
raiseIncompleteObject(r.lexer, objectType)
|
|
|
|
|
|
|
|
func handleReadException*(lexer: JsonLexer,
|
2019-07-08 07:57:05 +00:00
|
|
|
Record: type,
|
|
|
|
fieldName: string,
|
2019-08-01 14:12:31 +00:00
|
|
|
field: auto,
|
2023-06-05 08:23:36 +00:00
|
|
|
err: ref CatchableError) {.raises: [JsonReaderError].} =
|
2019-07-08 07:57:05 +00:00
|
|
|
var ex = new GenericJsonReaderError
|
2023-12-19 10:00:24 +00:00
|
|
|
ex.assignLineNumber(lexer)
|
2019-03-20 01:17:41 +00:00
|
|
|
ex.deserializedField = fieldName
|
|
|
|
ex.innerException = err
|
2019-03-19 23:54:03 +00:00
|
|
|
raise ex
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template handleReadException*(r: JsonReader,
|
|
|
|
Record: type,
|
|
|
|
fieldName: string,
|
|
|
|
field: auto,
|
|
|
|
err: ref CatchableError) =
|
|
|
|
handleReadException(r.lexer, Record, fieldName, field, err)
|
|
|
|
|
2020-06-30 19:11:56 +00:00
|
|
|
proc init*(T: type JsonReader,
|
|
|
|
stream: InputStream,
|
|
|
|
mode = defaultJsonMode,
|
2021-08-11 17:00:34 +00:00
|
|
|
allowUnknownFields = false,
|
2023-06-05 08:23:36 +00:00
|
|
|
requireAllFields = false): T {.raises: [IOError].} =
|
2023-12-19 10:00:24 +00:00
|
|
|
mixin flavorAllowsUnknownFields, flavorRequiresAllFields
|
|
|
|
type Flavor = T.Flavor
|
|
|
|
|
|
|
|
result.allowUnknownFields = allowUnknownFields or flavorAllowsUnknownFields(Flavor)
|
|
|
|
result.requireAllFields = requireAllFields or flavorRequiresAllFields(Flavor)
|
2019-03-19 23:54:03 +00:00
|
|
|
result.lexer = JsonLexer.init(stream, mode)
|
|
|
|
result.lexer.next()
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
proc requireToken*(lexer: var JsonLexer, tk: TokKind) {.raises: [IOError, JsonReaderError].} =
|
|
|
|
if lexer.tok != tk:
|
|
|
|
lexer.raiseUnexpectedToken case tk
|
2019-01-21 17:40:14 +00:00
|
|
|
of tkString: etString
|
2020-03-24 18:08:35 +00:00
|
|
|
of tkInt, tkNegativeInt: etInt
|
2019-01-21 17:40:14 +00:00
|
|
|
of tkComma: etComma
|
|
|
|
of tkBracketRi: etBracketRi
|
|
|
|
of tkBracketLe: etBracketLe
|
|
|
|
of tkCurlyRi: etCurrlyRi
|
|
|
|
of tkCurlyLe: etCurrlyLe
|
2020-08-01 16:28:36 +00:00
|
|
|
of tkColon: etColon
|
2019-03-13 23:39:10 +00:00
|
|
|
else: (doAssert false; etBool)
|
2019-01-21 17:40:14 +00:00
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
proc skipToken*(lexer: var JsonLexer, tk: TokKind) {.raises: [IOError, JsonReaderError].} =
|
|
|
|
lexer.requireToken tk
|
|
|
|
lexer.next()
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2020-07-24 19:49:30 +00:00
|
|
|
proc parseJsonNode(r: var JsonReader): JsonNode
|
2023-06-05 08:23:36 +00:00
|
|
|
{.gcsafe, raises: [IOError, JsonReaderError].}
|
2020-07-24 19:49:30 +00:00
|
|
|
|
2023-04-17 01:06:09 +00:00
|
|
|
proc readJsonNodeField(r: var JsonReader, field: var JsonNode)
|
2023-06-05 08:23:36 +00:00
|
|
|
{.gcsafe, raises: [IOError, JsonReaderError].} =
|
2023-04-17 01:06:09 +00:00
|
|
|
if field.isNil.not:
|
2020-07-24 19:49:30 +00:00
|
|
|
r.raiseUnexpectedValue("Unexpected duplicated field name")
|
|
|
|
|
|
|
|
r.lexer.next()
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkColon
|
2020-07-24 19:49:30 +00:00
|
|
|
|
|
|
|
field = r.parseJsonNode()
|
|
|
|
|
|
|
|
proc parseJsonNode(r: var JsonReader): JsonNode =
|
2023-11-24 16:18:23 +00:00
|
|
|
const maxIntValue: BiggestUint = BiggestInt.high.BiggestUint + 1
|
2020-07-24 19:49:30 +00:00
|
|
|
|
|
|
|
case r.lexer.tok
|
|
|
|
of tkCurlyLe:
|
|
|
|
result = JsonNode(kind: JObject)
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkCurlyRi:
|
|
|
|
while r.lexer.tok == tkString:
|
2021-03-19 02:13:55 +00:00
|
|
|
try:
|
|
|
|
r.readJsonNodeField(result.fields.mgetOrPut(r.lexer.strVal, nil))
|
|
|
|
except KeyError:
|
|
|
|
raiseAssert "mgetOrPut should never raise a KeyError"
|
2020-07-24 19:49:30 +00:00
|
|
|
if r.lexer.tok == tkComma:
|
|
|
|
r.lexer.next()
|
|
|
|
else:
|
|
|
|
break
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkCurlyRi
|
2020-07-24 19:49:30 +00:00
|
|
|
|
|
|
|
of tkBracketLe:
|
|
|
|
result = JsonNode(kind: JArray)
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkBracketRi:
|
|
|
|
while true:
|
|
|
|
result.elems.add r.parseJsonNode()
|
|
|
|
if r.lexer.tok == tkBracketRi:
|
|
|
|
break
|
|
|
|
else:
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkComma
|
2020-07-24 19:49:30 +00:00
|
|
|
# Skip over the last tkBracketRi
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkColon, tkComma, tkEof, tkError, tkBracketRi, tkCurlyRi:
|
|
|
|
r.raiseUnexpectedToken etValue
|
|
|
|
|
|
|
|
of tkString:
|
|
|
|
result = JsonNode(kind: JString, str: r.lexer.strVal)
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkInt:
|
|
|
|
if r.lexer.absIntVal > maxIntValue:
|
|
|
|
r.raiseIntOverflow(r.lexer.absIntVal, false)
|
|
|
|
else:
|
|
|
|
result = JsonNode(kind: JInt, num: BiggestInt r.lexer.absIntVal)
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkNegativeInt:
|
|
|
|
if r.lexer.absIntVal > maxIntValue + 1:
|
|
|
|
r.raiseIntOverflow(r.lexer.absIntVal, true)
|
|
|
|
else:
|
|
|
|
# `0 - x` is a magical trick that turns the unsigned
|
|
|
|
# value into its negative signed counterpart:
|
2023-11-24 16:18:23 +00:00
|
|
|
result = JsonNode(kind: JInt, num: cast[BiggestInt](BiggestUint(0) - r.lexer.absIntVal))
|
2020-07-24 19:49:30 +00:00
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkFloat:
|
|
|
|
result = JsonNode(kind: JFloat, fnum: r.lexer.floatVal)
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkTrue:
|
|
|
|
result = JsonNode(kind: JBool, bval: true)
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkFalse:
|
|
|
|
result = JsonNode(kind: JBool, bval: false)
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkNull:
|
|
|
|
result = JsonNode(kind: JNull)
|
|
|
|
r.lexer.next()
|
|
|
|
|
2022-05-05 16:33:40 +00:00
|
|
|
of tkQuoted, tkExBlob, tkNumeric, tkExInt, tkExNegInt:
|
|
|
|
raiseAssert "generic type " & $r.lexer.lazyTok & " is not applicable"
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
proc skipSingleJsValue*(lexer: var JsonLexer) {.raises: [IOError, JsonReaderError].} =
|
|
|
|
case lexer.tok
|
2020-06-30 19:11:56 +00:00
|
|
|
of tkCurlyLe:
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.next()
|
|
|
|
if lexer.tok != tkCurlyRi:
|
2020-06-30 19:11:56 +00:00
|
|
|
while true:
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.skipToken tkString
|
|
|
|
lexer.skipToken tkColon
|
|
|
|
lexer.skipSingleJsValue()
|
|
|
|
if lexer.tok == tkCurlyRi:
|
2020-06-30 19:11:56 +00:00
|
|
|
break
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.skipToken tkComma
|
2020-06-30 19:11:56 +00:00
|
|
|
# Skip over the last tkCurlyRi
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.next()
|
2020-06-30 19:11:56 +00:00
|
|
|
|
|
|
|
of tkBracketLe:
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.next()
|
|
|
|
if lexer.tok != tkBracketRi:
|
2020-06-30 19:11:56 +00:00
|
|
|
while true:
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.skipSingleJsValue()
|
|
|
|
if lexer.tok == tkBracketRi:
|
2020-06-30 19:11:56 +00:00
|
|
|
break
|
|
|
|
else:
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.skipToken tkComma
|
2020-06-30 19:11:56 +00:00
|
|
|
# Skip over the last tkBracketRi
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.next()
|
2020-06-30 19:11:56 +00:00
|
|
|
|
|
|
|
of tkColon, tkComma, tkEof, tkError, tkBracketRi, tkCurlyRi:
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.raiseUnexpectedToken etValue
|
2020-06-30 19:11:56 +00:00
|
|
|
|
2022-05-05 16:33:40 +00:00
|
|
|
of tkString, tkQuoted, tkExBlob,
|
|
|
|
tkInt, tkNegativeInt, tkFloat, tkNumeric, tkExInt, tkExNegInt,
|
|
|
|
tkTrue, tkFalse, tkNull:
|
2023-12-19 10:00:24 +00:00
|
|
|
lexer.next()
|
2020-06-30 19:11:56 +00:00
|
|
|
|
2023-06-05 08:23:36 +00:00
|
|
|
proc captureSingleJsValue(r: var JsonReader, output: var string) {.raises: [IOError, SerializationError].} =
|
2020-07-21 18:59:16 +00:00
|
|
|
r.lexer.renderTok output
|
|
|
|
case r.lexer.tok
|
|
|
|
of tkCurlyLe:
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkCurlyRi:
|
|
|
|
while true:
|
|
|
|
r.lexer.renderTok output
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkString
|
2020-07-21 18:59:16 +00:00
|
|
|
r.lexer.renderTok output
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkColon
|
2020-07-21 18:59:16 +00:00
|
|
|
r.captureSingleJsValue(output)
|
|
|
|
r.lexer.renderTok output
|
|
|
|
if r.lexer.tok == tkCurlyRi:
|
|
|
|
break
|
|
|
|
else:
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkComma
|
2020-07-21 18:59:16 +00:00
|
|
|
else:
|
|
|
|
output.add '}'
|
|
|
|
# Skip over the last tkCurlyRi
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkBracketLe:
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkBracketRi:
|
|
|
|
while true:
|
|
|
|
r.captureSingleJsValue(output)
|
|
|
|
r.lexer.renderTok output
|
|
|
|
if r.lexer.tok == tkBracketRi:
|
|
|
|
break
|
|
|
|
else:
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkComma
|
2020-07-21 18:59:16 +00:00
|
|
|
else:
|
|
|
|
output.add ']'
|
|
|
|
# Skip over the last tkBracketRi
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkColon, tkComma, tkEof, tkError, tkBracketRi, tkCurlyRi:
|
|
|
|
r.raiseUnexpectedToken etValue
|
|
|
|
|
2022-05-05 16:33:40 +00:00
|
|
|
of tkString, tkQuoted, tkExBlob,
|
|
|
|
tkInt, tkNegativeInt, tkFloat, tkNumeric, tkExInt, tkExNegInt,
|
|
|
|
tkTrue, tkFalse, tkNull:
|
2020-07-21 18:59:16 +00:00
|
|
|
r.lexer.next()
|
2020-07-17 20:58:02 +00:00
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
func allocPtr[T](p: var ptr T) =
|
2019-06-24 14:38:37 +00:00
|
|
|
p = create(T)
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
func allocPtr[T](p: var ref T) =
|
2019-06-24 14:38:37 +00:00
|
|
|
p = new(T)
|
|
|
|
|
2023-06-05 08:23:36 +00:00
|
|
|
iterator readArray*(r: var JsonReader, ElemType: typedesc): ElemType {.raises: [IOError, SerializationError].} =
|
2019-07-16 10:20:05 +00:00
|
|
|
mixin readValue
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkBracketLe
|
2022-05-05 16:33:40 +00:00
|
|
|
if r.lexer.lazyTok != tkBracketRi:
|
2019-07-16 10:20:05 +00:00
|
|
|
while true:
|
|
|
|
var res: ElemType
|
|
|
|
readValue(r, res)
|
|
|
|
yield res
|
|
|
|
if r.lexer.tok != tkComma: break
|
|
|
|
r.lexer.next()
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkBracketRi
|
2019-07-16 10:20:05 +00:00
|
|
|
|
2020-08-02 10:22:24 +00:00
|
|
|
iterator readObjectFields*(r: var JsonReader,
|
2023-06-05 08:23:36 +00:00
|
|
|
KeyType: type): KeyType {.raises: [IOError, SerializationError].} =
|
2019-08-02 10:25:02 +00:00
|
|
|
mixin readValue
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkCurlyLe
|
2022-05-05 16:33:40 +00:00
|
|
|
if r.lexer.lazyTok != tkCurlyRi:
|
2019-08-02 10:25:02 +00:00
|
|
|
while true:
|
|
|
|
var key: KeyType
|
|
|
|
readValue(r, key)
|
2022-05-05 16:33:40 +00:00
|
|
|
if r.lexer.lazyTok != tkColon: break
|
2019-08-02 10:25:02 +00:00
|
|
|
r.lexer.next()
|
2020-08-02 10:22:24 +00:00
|
|
|
yield key
|
2022-05-05 16:33:40 +00:00
|
|
|
if r.lexer.lazyTok != tkComma: break
|
2019-08-02 10:25:02 +00:00
|
|
|
r.lexer.next()
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkCurlyRi
|
2019-08-02 10:25:02 +00:00
|
|
|
|
2020-08-02 10:22:24 +00:00
|
|
|
iterator readObject*(r: var JsonReader,
|
|
|
|
KeyType: type,
|
2023-06-05 08:23:36 +00:00
|
|
|
ValueType: type): (KeyType, ValueType) {.raises: [IOError, SerializationError].} =
|
2020-08-02 10:22:24 +00:00
|
|
|
mixin readValue
|
|
|
|
|
|
|
|
for fieldName in readObjectFields(r, KeyType):
|
|
|
|
var value: ValueType
|
|
|
|
readValue(r, value)
|
|
|
|
yield (fieldName, value)
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
func isNotNilCheck[T](x: ref T not nil) {.compileTime.} = discard
|
|
|
|
func isNotNilCheck[T](x: ptr T not nil) {.compileTime.} = discard
|
2020-04-22 22:39:56 +00:00
|
|
|
|
2022-07-14 12:14:10 +00:00
|
|
|
func isFieldExpected*(T: type): bool {.compileTime.} =
|
|
|
|
T isnot Option
|
|
|
|
|
|
|
|
func totalExpectedFields*(T: type): int {.compileTime.} =
|
|
|
|
mixin isFieldExpected,
|
|
|
|
enumAllSerializedFields
|
|
|
|
|
|
|
|
enumAllSerializedFields(T):
|
|
|
|
if isFieldExpected(FieldType):
|
|
|
|
inc result
|
|
|
|
|
2022-07-14 13:05:55 +00:00
|
|
|
func setBitInWord(x: var uint, bit: int) {.inline.} =
|
2022-07-14 12:14:10 +00:00
|
|
|
let mask = uint(1) shl bit
|
|
|
|
x = x or mask
|
|
|
|
|
|
|
|
const bitsPerWord = sizeof(uint) * 8
|
|
|
|
|
|
|
|
func expectedFieldsBitmask*(TT: type): auto {.compileTime.} =
|
|
|
|
type T = TT
|
|
|
|
|
|
|
|
mixin isFieldExpected,
|
|
|
|
enumAllSerializedFields
|
|
|
|
|
|
|
|
const requiredWords =
|
2022-07-15 10:23:35 +00:00
|
|
|
(totalSerializedFields(T) + bitsPerWord - 1) div bitsPerWord
|
2022-07-14 12:14:10 +00:00
|
|
|
|
|
|
|
var res: array[requiredWords, uint]
|
|
|
|
|
|
|
|
var i = 0
|
|
|
|
enumAllSerializedFields(T):
|
|
|
|
if isFieldExpected(FieldType):
|
2022-07-14 13:05:55 +00:00
|
|
|
res[i div bitsPerWord].setBitInWord(i mod bitsPerWord)
|
2022-07-14 12:14:10 +00:00
|
|
|
inc i
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
res
|
2022-07-14 12:14:10 +00:00
|
|
|
|
2022-07-14 13:05:55 +00:00
|
|
|
template setBitInArray[N](data: var array[N, uint], bitIdx: int) =
|
|
|
|
when data.len > 1:
|
|
|
|
setBitInWord(data[bitIdx div bitsPerWord], bitIdx mod bitsPerWord)
|
2022-07-14 12:14:10 +00:00
|
|
|
else:
|
2022-07-14 13:05:55 +00:00
|
|
|
setBitInWord(data[0], bitIdx)
|
2022-07-14 12:14:10 +00:00
|
|
|
|
|
|
|
func isBitwiseSubsetOf[N](lhs, rhs: array[N, uint]): bool =
|
|
|
|
for i in low(lhs) .. high(lhs):
|
|
|
|
if (lhs[i] and rhs[i]) != lhs[i]:
|
|
|
|
return false
|
|
|
|
|
|
|
|
true
|
|
|
|
|
2020-04-29 08:21:20 +00:00
|
|
|
# this construct catches `array[N, char]` which otherwise won't decompose into
|
|
|
|
# openArray[char] - we treat any array-like thing-of-characters as a string in
|
|
|
|
# the output
|
|
|
|
template isCharArray[N](v: array[N, char]): bool = true
|
|
|
|
template isCharArray(v: auto): bool = false
|
|
|
|
|
2023-05-27 09:42:08 +00:00
|
|
|
func parseStringEnum[T](
|
|
|
|
r: var JsonReader, value: var T,
|
2023-06-05 08:23:36 +00:00
|
|
|
stringNormalizer: static[proc(s: string): string]) {.raises: [JsonReaderError].} =
|
2023-05-27 09:42:08 +00:00
|
|
|
try:
|
|
|
|
value = genEnumCaseStmt(
|
|
|
|
T, r.lexer.strVal,
|
|
|
|
default = nil, ord(T.low), ord(T.high), stringNormalizer)
|
2023-06-05 08:23:36 +00:00
|
|
|
except ValueError:
|
2023-05-27 09:42:08 +00:00
|
|
|
const typeName = typetraits.name(T)
|
|
|
|
r.raiseUnexpectedValue("Invalid value for '" & typeName & "'")
|
|
|
|
|
|
|
|
func strictNormalize(s: string): string = # Match enum value exactly
|
|
|
|
s
|
|
|
|
|
|
|
|
proc parseEnum[T](
|
|
|
|
r: var JsonReader, value: var T, allowNumericRepr: static[bool] = false,
|
2023-06-05 08:23:36 +00:00
|
|
|
stringNormalizer: static[proc(s: string): string] = strictNormalize) {.raises: [IOError, JsonReaderError].} =
|
2023-05-27 09:42:08 +00:00
|
|
|
const style = T.enumStyle
|
|
|
|
let tok = r.lexer.tok
|
|
|
|
case tok
|
|
|
|
of tkString:
|
|
|
|
r.parseStringEnum(value, stringNormalizer)
|
|
|
|
of tkInt:
|
|
|
|
when allowNumericRepr:
|
|
|
|
case style
|
|
|
|
of EnumStyle.Numeric:
|
|
|
|
if not value.checkedEnumAssign(r.lexer.absIntVal):
|
|
|
|
const typeName = typetraits.name(T)
|
|
|
|
r.raiseUnexpectedValue("Out of range for '" & typeName & "'")
|
|
|
|
of EnumStyle.AssociatedStrings:
|
|
|
|
r.raiseUnexpectedToken etEnumString
|
|
|
|
else:
|
|
|
|
r.raiseUnexpectedToken etEnumString
|
|
|
|
else:
|
|
|
|
case style
|
|
|
|
of EnumStyle.Numeric:
|
|
|
|
when allowNumericRepr:
|
|
|
|
r.raiseUnexpectedToken etEnumAny
|
|
|
|
else:
|
|
|
|
r.raiseUnexpectedToken etEnumString
|
|
|
|
of EnumStyle.AssociatedStrings:
|
|
|
|
r.raiseUnexpectedToken etEnumString
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
proc readRecordValue*[T](r: var JsonReader, value: var T)
|
|
|
|
{.raises: [SerializationError, IOError].} =
|
|
|
|
type
|
|
|
|
ReaderType {.used.} = type r
|
|
|
|
T = type value
|
|
|
|
|
|
|
|
r.lexer.skipToken tkCurlyLe
|
|
|
|
|
|
|
|
when T.totalSerializedFields > 0:
|
|
|
|
let
|
|
|
|
fieldsTable = T.fieldReadersTable(ReaderType)
|
|
|
|
|
|
|
|
const
|
|
|
|
expectedFields = T.expectedFieldsBitmask
|
|
|
|
|
|
|
|
var
|
|
|
|
encounteredFields: typeof(expectedFields)
|
|
|
|
mostLikelyNextField = 0
|
|
|
|
|
|
|
|
while true:
|
|
|
|
# Have the assignment parsed of the AVP
|
|
|
|
if r.lexer.lazyTok == tkQuoted:
|
|
|
|
r.lexer.accept
|
|
|
|
if r.lexer.lazyTok != tkString:
|
|
|
|
break
|
|
|
|
|
|
|
|
when T is tuple:
|
|
|
|
let fieldIdx = mostLikelyNextField
|
|
|
|
mostLikelyNextField += 1
|
|
|
|
else:
|
|
|
|
let fieldIdx = findFieldIdx(fieldsTable[],
|
|
|
|
r.lexer.strVal,
|
|
|
|
mostLikelyNextField)
|
|
|
|
if fieldIdx != -1:
|
|
|
|
let reader = fieldsTable[][fieldIdx].reader
|
|
|
|
r.lexer.next()
|
|
|
|
r.lexer.skipToken tkColon
|
|
|
|
reader(value, r)
|
|
|
|
encounteredFields.setBitInArray(fieldIdx)
|
|
|
|
elif r.allowUnknownFields:
|
|
|
|
r.lexer.next()
|
|
|
|
r.lexer.skipToken tkColon
|
|
|
|
r.lexer.skipSingleJsValue()
|
|
|
|
else:
|
|
|
|
const typeName = typetraits.name(T)
|
|
|
|
r.raiseUnexpectedField(r.lexer.strVal, cstring typeName)
|
|
|
|
|
|
|
|
if r.lexer.lazyTok == tkComma:
|
|
|
|
r.lexer.next()
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
if r.requireAllFields and
|
|
|
|
not expectedFields.isBitwiseSubsetOf(encounteredFields):
|
|
|
|
const typeName = typetraits.name(T)
|
|
|
|
r.raiseIncompleteObject(typeName)
|
|
|
|
|
|
|
|
r.lexer.accept
|
|
|
|
r.lexer.skipToken tkCurlyRi
|
|
|
|
|
2020-06-22 14:42:50 +00:00
|
|
|
proc readValue*[T](r: var JsonReader, value: var T)
|
2023-06-05 08:23:36 +00:00
|
|
|
{.gcsafe, raises: [SerializationError, IOError].} =
|
2022-05-05 16:33:40 +00:00
|
|
|
## Master filed/object parser. This function relies on customised sub-mixins for particular
|
|
|
|
## object types.
|
|
|
|
##
|
|
|
|
## Customised readValue() examples:
|
|
|
|
## ::
|
|
|
|
## type
|
|
|
|
## FancyInt = distinct int
|
|
|
|
## FancyUInt = distinct uint
|
|
|
|
##
|
|
|
|
## proc readValue(reader: var JsonReader, value: var FancyInt) =
|
|
|
|
## ## Refer to another readValue() instance
|
|
|
|
## value = reader.readValue(int).FancyInt
|
|
|
|
##
|
|
|
|
## proc readValue(reader: var JsonReader, value: var FancyUInt) =
|
|
|
|
## ## Provide a full custum version of a readValue() instance
|
|
|
|
## if reader.lexer.lazyTok == tkNumeric:
|
|
|
|
## # lazyTok: Check token before the value is available
|
|
|
|
## var accu: FancyUInt
|
|
|
|
## # custom parser (the directive `customIntValueIt()` is a
|
|
|
|
## # convenience wrapper around `customIntHandler()`.)
|
|
|
|
## reader.lexer.customIntValueIt:
|
|
|
|
## accu = accu * 10 + it.u256
|
|
|
|
## value = accu
|
|
|
|
## elif reader.lexer.lazyTok == tkQuoted:
|
|
|
|
## var accu = string
|
|
|
|
## # The following is really for demo only (inefficient,
|
|
|
|
## # lacks hex encoding)
|
|
|
|
## reader.lexer.customTextValueIt:
|
|
|
|
## accu &= it
|
|
|
|
## value = accu.parseUInt.FancyUInt
|
|
|
|
## ...
|
|
|
|
## # prepare next parser cycle
|
|
|
|
## reader.lexer.next
|
|
|
|
##
|
2018-12-17 23:01:06 +00:00
|
|
|
mixin readValue
|
|
|
|
|
2022-05-05 16:33:40 +00:00
|
|
|
when value is (object or tuple):
|
|
|
|
let tok {.used.} = r.lexer.lazyTok
|
|
|
|
else:
|
|
|
|
let tok {.used.} = r.lexer.tok # resove lazy token
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2020-07-24 19:49:30 +00:00
|
|
|
when value is JsonString:
|
|
|
|
r.captureSingleJsValue(string value)
|
|
|
|
|
|
|
|
elif value is JsonNode:
|
|
|
|
value = r.parseJsonNode()
|
|
|
|
|
|
|
|
elif value is string:
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.requireToken tkString
|
2018-12-17 23:01:06 +00:00
|
|
|
value = r.lexer.strVal
|
|
|
|
r.lexer.next()
|
2020-07-21 18:59:16 +00:00
|
|
|
|
2020-04-29 08:21:20 +00:00
|
|
|
elif value is seq[char]:
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.requireToken tkString
|
2020-04-29 08:21:20 +00:00
|
|
|
value.setLen(r.lexer.strVal.len)
|
|
|
|
for i in 0..<r.lexer.strVal.len:
|
|
|
|
value[i] = r.lexer.strVal[i]
|
|
|
|
r.lexer.next()
|
2020-07-21 18:59:16 +00:00
|
|
|
|
2020-04-29 08:21:20 +00:00
|
|
|
elif isCharArray(value):
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.requireToken tkString
|
2020-04-29 08:21:20 +00:00
|
|
|
if r.lexer.strVal.len != value.len:
|
|
|
|
# Raise tkString because we expected a `"` earlier
|
|
|
|
r.raiseUnexpectedToken(etString)
|
|
|
|
for i in 0..<value.len:
|
|
|
|
value[i] = r.lexer.strVal[i]
|
|
|
|
r.lexer.next()
|
2020-07-21 18:59:16 +00:00
|
|
|
|
2018-12-17 23:01:06 +00:00
|
|
|
elif value is bool:
|
|
|
|
case tok
|
|
|
|
of tkTrue: value = true
|
|
|
|
of tkFalse: value = false
|
2019-01-21 17:40:14 +00:00
|
|
|
else: r.raiseUnexpectedToken etBool
|
2018-12-17 23:01:06 +00:00
|
|
|
r.lexer.next()
|
|
|
|
|
2019-06-24 14:38:37 +00:00
|
|
|
elif value is ref|ptr:
|
2020-04-22 22:39:56 +00:00
|
|
|
when compiles(isNotNilCheck(value)):
|
2019-06-24 14:38:37 +00:00
|
|
|
allocPtr value
|
|
|
|
value[] = readValue(r, type(value[]))
|
2020-04-22 22:39:56 +00:00
|
|
|
else:
|
|
|
|
if tok == tkNull:
|
|
|
|
value = nil
|
|
|
|
r.lexer.next()
|
|
|
|
else:
|
|
|
|
allocPtr value
|
|
|
|
value[] = readValue(r, type(value[]))
|
2019-06-24 14:38:37 +00:00
|
|
|
|
2018-12-17 23:01:06 +00:00
|
|
|
elif value is enum:
|
2023-05-27 09:42:08 +00:00
|
|
|
r.parseEnum(value)
|
2018-12-17 23:01:06 +00:00
|
|
|
r.lexer.next()
|
|
|
|
|
2023-11-24 16:18:23 +00:00
|
|
|
elif value is SomeSignedInt:
|
2018-12-17 23:01:06 +00:00
|
|
|
type TargetType = type(value)
|
2023-11-24 16:18:23 +00:00
|
|
|
let
|
|
|
|
isNegative = tok == tkNegativeInt
|
|
|
|
maxValidAbsValue: BiggestUint =
|
|
|
|
if isNegative:
|
|
|
|
TargetType.high.BiggestUint + 1
|
|
|
|
else:
|
|
|
|
TargetType.high.BiggestUint
|
2020-03-24 18:08:35 +00:00
|
|
|
|
2023-11-24 16:18:23 +00:00
|
|
|
if r.lexer.absIntVal > maxValidAbsValue:
|
|
|
|
r.raiseIntOverflow(r.lexer.absIntVal, isNegative)
|
2020-03-24 18:08:35 +00:00
|
|
|
|
|
|
|
case tok
|
|
|
|
of tkInt:
|
|
|
|
value = TargetType(r.lexer.absIntVal)
|
|
|
|
of tkNegativeInt:
|
2023-11-24 16:18:23 +00:00
|
|
|
if r.lexer.absIntVal == maxValidAbsValue:
|
|
|
|
# We must handle this as a special case because it would be illegal
|
|
|
|
# to convert a value like 128 to int8 before negating it. The max
|
|
|
|
# int8 value is 127 (while the minimum is -128).
|
|
|
|
value = low(TargetType)
|
2020-03-24 18:08:35 +00:00
|
|
|
else:
|
2023-11-24 16:18:23 +00:00
|
|
|
value = -TargetType(r.lexer.absIntVal)
|
|
|
|
else:
|
|
|
|
r.raiseUnexpectedToken etInt
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
elif value is SomeUnsignedInt:
|
|
|
|
type TargetType = type(value)
|
|
|
|
|
|
|
|
if r.lexer.absIntVal > TargetType.high.BiggestUint:
|
|
|
|
r.raiseIntOverflow(r.lexer.absIntVal, isNegative = false)
|
|
|
|
|
|
|
|
case tok
|
|
|
|
of tkInt:
|
|
|
|
value = TargetType(r.lexer.absIntVal)
|
2020-03-24 18:08:35 +00:00
|
|
|
else:
|
|
|
|
r.raiseUnexpectedToken etInt
|
2018-12-17 23:01:06 +00:00
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
elif value is SomeFloat:
|
|
|
|
case tok
|
2020-03-24 18:08:35 +00:00
|
|
|
of tkInt: value = float(r.lexer.absIntVal)
|
2018-12-17 23:01:06 +00:00
|
|
|
of tkFloat: value = r.lexer.floatVal
|
2019-01-21 17:40:14 +00:00
|
|
|
else:
|
|
|
|
r.raiseUnexpectedToken etNumber
|
2018-12-17 23:01:06 +00:00
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
elif value is seq:
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkBracketLe
|
2018-12-17 23:01:06 +00:00
|
|
|
if r.lexer.tok != tkBracketRi:
|
|
|
|
while true:
|
|
|
|
let lastPos = value.len
|
|
|
|
value.setLen(lastPos + 1)
|
2019-03-20 01:17:41 +00:00
|
|
|
readValue(r, value[lastPos])
|
2018-12-17 23:01:06 +00:00
|
|
|
if r.lexer.tok != tkComma: break
|
|
|
|
r.lexer.next()
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkBracketRi
|
2018-12-17 23:01:06 +00:00
|
|
|
|
|
|
|
elif value is array:
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkBracketLe
|
2018-12-17 23:01:06 +00:00
|
|
|
for i in low(value) ..< high(value):
|
|
|
|
# TODO: dont's ask. this makes the code compile
|
|
|
|
if false: value[i] = value[i]
|
2019-03-20 01:17:41 +00:00
|
|
|
readValue(r, value[i])
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkComma
|
2019-03-20 01:17:41 +00:00
|
|
|
readValue(r, value[high(value)])
|
2023-12-19 10:00:24 +00:00
|
|
|
r.lexer.skipToken tkBracketRi
|
2018-12-17 23:01:06 +00:00
|
|
|
|
|
|
|
elif value is (object or tuple):
|
2023-12-19 10:00:24 +00:00
|
|
|
mixin flavorUsesAutomaticObjectSerialization
|
2022-07-14 12:14:10 +00:00
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
type Flavor = JsonReader.Flavor
|
|
|
|
const isAutomatic =
|
|
|
|
flavorUsesAutomaticObjectSerialization(Flavor)
|
2021-08-11 17:00:34 +00:00
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
when not isAutomatic:
|
|
|
|
const typeName = typetraits.name(T)
|
|
|
|
{.error: "Please override readValue for the " & typeName & " type (or import the module where the override is provided)".}
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
readRecordValue(r, value)
|
2018-12-17 23:01:06 +00:00
|
|
|
else:
|
2020-06-22 14:42:50 +00:00
|
|
|
const typeName = typetraits.name(T)
|
2018-12-17 23:01:06 +00:00
|
|
|
{.error: "Failed to convert to JSON an unsupported type: " & typeName.}
|
2020-08-02 17:15:44 +00:00
|
|
|
|
2023-07-06 07:13:29 +00:00
|
|
|
iterator readObjectFields*(r: var JsonReader): string {.
|
|
|
|
raises: [IOError, SerializationError].} =
|
2020-08-02 17:15:44 +00:00
|
|
|
for key in readObjectFields(r, string):
|
|
|
|
yield key
|
|
|
|
|
2023-05-27 09:42:08 +00:00
|
|
|
template configureJsonDeserialization*(
|
|
|
|
T: type[enum], allowNumericRepr: static[bool] = false,
|
|
|
|
stringNormalizer: static[proc(s: string): string] = strictNormalize) =
|
|
|
|
proc readValue*(r: var JsonReader, value: var T) {.
|
2023-06-05 08:23:36 +00:00
|
|
|
raises: [IOError, SerializationError].} =
|
2023-05-27 09:42:08 +00:00
|
|
|
static: doAssert not allowNumericRepr or enumStyle(T) == EnumStyle.Numeric
|
|
|
|
r.parseEnum(value, allowNumericRepr, stringNormalizer)
|
2023-06-05 08:23:36 +00:00
|
|
|
|
|
|
|
{.pop.}
|