2020-04-22 22:39:56 +00:00
|
|
|
{.experimental: "notnil".}
|
|
|
|
|
2018-12-17 23:01:06 +00:00
|
|
|
import
|
2020-07-24 19:49:30 +00:00
|
|
|
tables, strutils, typetraits, macros, strformat,
|
2020-05-05 17:28:44 +00:00
|
|
|
faststreams/inputs, serialization/[object_serialization, errors],
|
2019-01-21 17:40:14 +00:00
|
|
|
types, lexer
|
|
|
|
|
2020-07-24 19:49:30 +00:00
|
|
|
from json import JsonNode, JsonNodeKind
|
|
|
|
|
2019-01-21 17:40:14 +00:00
|
|
|
export
|
2019-03-19 23:54:03 +00:00
|
|
|
types, errors
|
2018-12-17 23:01:06 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
JsonReader* = object
|
2019-07-18 18:20:06 +00:00
|
|
|
lexer*: JsonLexer
|
2020-07-17 20:58:02 +00:00
|
|
|
allowUnknownFields: 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
|
|
|
|
encounteredField*: cstring
|
|
|
|
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"
|
|
|
|
etEnum = "enum value (int or string)"
|
|
|
|
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
|
|
|
|
|
2020-03-24 18:08:35 +00:00
|
|
|
IntOverflowError* = object of JsonReaderError
|
|
|
|
isNegative: bool
|
|
|
|
absIntVal: uint64
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-19 23:54:03 +00:00
|
|
|
method formatMsg*(err: ref JsonReaderError, filename: string): string =
|
2020-06-15 22:03:00 +00:00
|
|
|
tryFmt: fmt"{filename}({err.line}, {err.col}) Error while reading json file"
|
2019-03-19 23:54:03 +00:00
|
|
|
|
|
|
|
method formatMsg*(err: ref UnexpectedField, filename: string): string =
|
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
|
|
|
|
2020-05-30 19:40:17 +00:00
|
|
|
method formatMsg*(err: ref UnexpectedTokenError, filename: string): string =
|
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
|
|
|
|
2019-07-08 07:57:05 +00:00
|
|
|
method formatMsg*(err: ref GenericJsonReaderError, filename: string): string =
|
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
|
|
|
|
2020-03-24 18:08:35 +00:00
|
|
|
method formatMsg*(err: ref IntOverflowError, filename: string): string =
|
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
|
|
|
|
2020-05-30 19:40:17 +00:00
|
|
|
method formatMsg*(err: ref UnexpectedValueError, filename: string): string =
|
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
|
|
|
|
2019-07-21 14:12:48 +00:00
|
|
|
proc assignLineNumber*(ex: ref JsonReaderError, r: JsonReader) =
|
2018-12-17 23:01:06 +00:00
|
|
|
ex.line = r.lexer.line
|
2019-07-21 14:12:48 +00:00
|
|
|
ex.col = r.lexer.tokenStartCol
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2020-05-30 19:40:17 +00:00
|
|
|
proc raiseUnexpectedToken*(r: JsonReader, expected: ExpectedTokenCategory)
|
|
|
|
{.noreturn.} =
|
|
|
|
var ex = new UnexpectedTokenError
|
2018-12-17 23:01:06 +00:00
|
|
|
ex.assignLineNumber(r)
|
|
|
|
ex.encountedToken = r.lexer.tok
|
|
|
|
ex.expectedToken = expected
|
|
|
|
raise ex
|
|
|
|
|
2020-05-30 19:40:17 +00:00
|
|
|
proc raiseUnexpectedValue*(r: JsonReader, msg: string) {.noreturn.} =
|
|
|
|
var ex = new UnexpectedValueError
|
|
|
|
ex.assignLineNumber(r)
|
|
|
|
ex.msg = msg
|
|
|
|
raise ex
|
|
|
|
|
2020-03-24 18:08:35 +00:00
|
|
|
proc raiseIntOverflow*(r: JsonReader, absIntVal: uint64, isNegative: bool) {.noreturn.} =
|
|
|
|
var ex = new IntOverflowError
|
|
|
|
ex.assignLineNumber(r)
|
|
|
|
ex.absIntVal = absIntVal
|
|
|
|
ex.isNegative = isNegative
|
|
|
|
raise ex
|
|
|
|
|
|
|
|
proc raiseUnexpectedField*(r: JsonReader, fieldName, deserializedType: cstring) {.noreturn.} =
|
2018-12-17 23:01:06 +00:00
|
|
|
var ex = new UnexpectedField
|
|
|
|
ex.assignLineNumber(r)
|
|
|
|
ex.encounteredField = fieldName
|
|
|
|
ex.deserializedType = deserializedType
|
|
|
|
raise ex
|
|
|
|
|
2019-07-08 07:57:05 +00:00
|
|
|
proc handleReadException*(r: JsonReader,
|
|
|
|
Record: type,
|
|
|
|
fieldName: string,
|
2019-08-01 14:12:31 +00:00
|
|
|
field: auto,
|
2019-07-08 07:57:05 +00:00
|
|
|
err: ref CatchableError) =
|
|
|
|
var ex = new GenericJsonReaderError
|
2019-03-19 23:54:03 +00:00
|
|
|
ex.assignLineNumber(r)
|
2019-03-20 01:17:41 +00:00
|
|
|
ex.deserializedField = fieldName
|
|
|
|
ex.innerException = err
|
2019-03-19 23:54:03 +00:00
|
|
|
raise ex
|
|
|
|
|
2020-06-30 19:11:56 +00:00
|
|
|
proc init*(T: type JsonReader,
|
|
|
|
stream: InputStream,
|
|
|
|
mode = defaultJsonMode,
|
2020-07-17 20:58:02 +00:00
|
|
|
allowUnknownFields = false): T =
|
|
|
|
result.allowUnknownFields = allowUnknownFields
|
2019-03-19 23:54:03 +00:00
|
|
|
result.lexer = JsonLexer.init(stream, mode)
|
|
|
|
result.lexer.next()
|
|
|
|
|
|
|
|
proc setParsed[T: enum](e: var T, s: string) =
|
|
|
|
e = parseEnum[T](s)
|
|
|
|
|
2019-07-18 18:20:06 +00:00
|
|
|
proc requireToken*(r: JsonReader, tk: TokKind) =
|
2019-01-21 17:40:14 +00:00
|
|
|
if r.lexer.tok != tk:
|
|
|
|
r.raiseUnexpectedToken case tk
|
|
|
|
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
|
|
|
|
2019-07-18 18:20:06 +00:00
|
|
|
proc skipToken*(r: var JsonReader, tk: TokKind) =
|
2018-12-17 23:01:06 +00:00
|
|
|
r.requireToken tk
|
|
|
|
r.lexer.next()
|
|
|
|
|
2020-07-24 19:49:30 +00:00
|
|
|
func maxAbsValue(T: type[SomeInteger]): uint64 {.compileTime.} =
|
|
|
|
when T is int8 : 128'u64
|
|
|
|
elif T is int16: 32768'u64
|
|
|
|
elif T is int32: 2147483648'u64
|
|
|
|
elif T is int64: 9223372036854775808'u64
|
|
|
|
else: uint64(high(T))
|
|
|
|
|
|
|
|
proc parseJsonNode(r: var JsonReader): JsonNode
|
|
|
|
{.gcsafe, raises: [IOError, JsonReaderError, Defect].}
|
|
|
|
|
|
|
|
proc readJsonNodeField(r: var JsonReader, field: var JsonNode) =
|
|
|
|
if field != nil:
|
|
|
|
r.raiseUnexpectedValue("Unexpected duplicated field name")
|
|
|
|
|
|
|
|
r.lexer.next()
|
|
|
|
r.skipToken tkColon
|
|
|
|
|
|
|
|
field = r.parseJsonNode()
|
|
|
|
|
|
|
|
proc parseJsonNode(r: var JsonReader): JsonNode =
|
|
|
|
const maxIntValue = maxAbsValue(BiggestInt)
|
|
|
|
|
|
|
|
case r.lexer.tok
|
|
|
|
of tkCurlyLe:
|
|
|
|
result = JsonNode(kind: JObject)
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkCurlyRi:
|
|
|
|
while r.lexer.tok == tkString:
|
|
|
|
r.readJsonNodeField(result.fields.mgetOrPut(r.lexer.strVal, nil))
|
|
|
|
if r.lexer.tok == tkComma:
|
|
|
|
r.lexer.next()
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
r.skipToken tkCurlyRi
|
|
|
|
|
|
|
|
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:
|
|
|
|
r.skipToken tkComma
|
|
|
|
# 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:
|
|
|
|
result = JsonNode(kind: JInt, num: cast[int64](uint64(0) - r.lexer.absIntVal))
|
|
|
|
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()
|
|
|
|
|
2020-06-30 19:11:56 +00:00
|
|
|
proc skipSingleJsValue(r: var JsonReader) =
|
|
|
|
case r.lexer.tok
|
|
|
|
of tkCurlyLe:
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkCurlyRi:
|
|
|
|
while true:
|
|
|
|
r.skipToken tkString
|
|
|
|
r.skipToken tkColon
|
|
|
|
r.skipSingleJsValue()
|
|
|
|
if r.lexer.tok == tkCurlyRi:
|
|
|
|
break
|
|
|
|
r.skipToken tkComma
|
|
|
|
# Skip over the last tkCurlyRi
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkBracketLe:
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkBracketRi:
|
|
|
|
while true:
|
|
|
|
r.skipSingleJsValue()
|
|
|
|
if r.lexer.tok == tkBracketRi:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
r.skipToken tkComma
|
|
|
|
# Skip over the last tkBracketRi
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkColon, tkComma, tkEof, tkError, tkBracketRi, tkCurlyRi:
|
|
|
|
r.raiseUnexpectedToken etValue
|
|
|
|
|
|
|
|
of tkString, tkInt, tkNegativeInt, tkFloat, tkTrue, tkFalse, tkNull:
|
|
|
|
r.lexer.next()
|
|
|
|
|
2020-07-21 18:59:16 +00:00
|
|
|
proc captureSingleJsValue(r: var JsonReader, output: var string) =
|
|
|
|
r.lexer.renderTok output
|
|
|
|
case r.lexer.tok
|
|
|
|
of tkCurlyLe:
|
|
|
|
r.lexer.next()
|
|
|
|
if r.lexer.tok != tkCurlyRi:
|
|
|
|
while true:
|
|
|
|
r.lexer.renderTok output
|
|
|
|
r.skipToken tkString
|
|
|
|
r.lexer.renderTok output
|
|
|
|
r.skipToken tkColon
|
|
|
|
r.captureSingleJsValue(output)
|
|
|
|
r.lexer.renderTok output
|
|
|
|
if r.lexer.tok == tkCurlyRi:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
r.skipToken tkComma
|
|
|
|
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:
|
|
|
|
r.skipToken tkComma
|
|
|
|
else:
|
|
|
|
output.add ']'
|
|
|
|
# Skip over the last tkBracketRi
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
of tkColon, tkComma, tkEof, tkError, tkBracketRi, tkCurlyRi:
|
|
|
|
r.raiseUnexpectedToken etValue
|
|
|
|
|
|
|
|
of tkString, tkInt, tkNegativeInt, tkFloat, tkTrue, tkFalse, tkNull:
|
|
|
|
r.lexer.next()
|
2020-07-17 20:58:02 +00:00
|
|
|
|
2019-06-24 14:38:37 +00:00
|
|
|
proc allocPtr[T](p: var ptr T) =
|
|
|
|
p = create(T)
|
|
|
|
|
|
|
|
proc allocPtr[T](p: var ref T) =
|
|
|
|
p = new(T)
|
|
|
|
|
2019-07-16 10:20:05 +00:00
|
|
|
iterator readArray*(r: var JsonReader, ElemType: typedesc): ElemType =
|
|
|
|
mixin readValue
|
|
|
|
|
|
|
|
r.skipToken tkBracketLe
|
|
|
|
if r.lexer.tok != tkBracketRi:
|
|
|
|
while true:
|
|
|
|
var res: ElemType
|
|
|
|
readValue(r, res)
|
|
|
|
yield res
|
|
|
|
if r.lexer.tok != tkComma: break
|
|
|
|
r.lexer.next()
|
|
|
|
r.skipToken tkBracketRi
|
|
|
|
|
2020-08-02 10:22:24 +00:00
|
|
|
iterator readObjectFields*(r: var JsonReader,
|
|
|
|
KeyType: type): KeyType =
|
2019-08-02 10:25:02 +00:00
|
|
|
mixin readValue
|
|
|
|
|
|
|
|
r.skipToken tkCurlyLe
|
|
|
|
if r.lexer.tok != tkCurlyRi:
|
|
|
|
while true:
|
|
|
|
var key: KeyType
|
|
|
|
readValue(r, key)
|
|
|
|
if r.lexer.tok != tkColon: break
|
|
|
|
r.lexer.next()
|
2020-08-02 10:22:24 +00:00
|
|
|
yield key
|
2019-08-02 10:25:02 +00:00
|
|
|
if r.lexer.tok != tkComma: break
|
|
|
|
r.lexer.next()
|
|
|
|
r.skipToken tkCurlyRi
|
|
|
|
|
2020-08-02 10:22:24 +00:00
|
|
|
iterator readObject*(r: var JsonReader,
|
|
|
|
KeyType: type,
|
|
|
|
ValueType: type): (KeyType, ValueType) =
|
|
|
|
mixin readValue
|
|
|
|
|
|
|
|
for fieldName in readObjectFields(r, KeyType):
|
|
|
|
var value: ValueType
|
|
|
|
readValue(r, value)
|
|
|
|
yield (fieldName, value)
|
|
|
|
|
2020-04-22 22:39:56 +00:00
|
|
|
proc isNotNilCheck[T](x: ref T not nil) {.compileTime.} = discard
|
|
|
|
proc isNotNilCheck[T](x: ptr T not nil) {.compileTime.} = discard
|
|
|
|
|
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
|
|
|
|
|
2020-06-22 14:42:50 +00:00
|
|
|
proc readValue*[T](r: var JsonReader, value: var T)
|
|
|
|
{.raises: [SerializationError, IOError, Defect].} =
|
2018-12-17 23:01:06 +00:00
|
|
|
mixin readValue
|
|
|
|
|
2019-11-04 18:42:34 +00:00
|
|
|
let tok {.used.} = r.lexer.tok
|
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:
|
2018-12-17 23:01:06 +00:00
|
|
|
r.requireToken tkString
|
|
|
|
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]:
|
|
|
|
r.requireToken tkString
|
|
|
|
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):
|
|
|
|
r.requireToken tkString
|
|
|
|
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:
|
|
|
|
case tok
|
|
|
|
of tkString:
|
2020-06-22 14:42:50 +00:00
|
|
|
try:
|
|
|
|
value.setParsed(r.lexer.strVal)
|
|
|
|
except ValueError as err:
|
|
|
|
const typeName = typetraits.name(T)
|
|
|
|
r.raiseUnexpectedValue("Expected valid '" & typeName & "' value")
|
2018-12-17 23:01:06 +00:00
|
|
|
of tkInt:
|
|
|
|
# TODO: validate that the value is in range
|
2020-03-24 18:08:35 +00:00
|
|
|
value = type(value)(r.lexer.absIntVal)
|
2018-12-17 23:01:06 +00:00
|
|
|
else:
|
2019-01-21 17:40:14 +00:00
|
|
|
r.raiseUnexpectedToken etEnum
|
2018-12-17 23:01:06 +00:00
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
elif value is SomeInteger:
|
|
|
|
type TargetType = type(value)
|
2020-03-24 18:08:35 +00:00
|
|
|
const maxValidValue = maxAbsValue(TargetType)
|
|
|
|
|
2020-07-24 19:49:30 +00:00
|
|
|
let isNegative = tok == tkNegativeInt
|
|
|
|
if r.lexer.absIntVal > maxValidValue + uint64(isNegative):
|
|
|
|
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:
|
|
|
|
when value is SomeSignedInt:
|
|
|
|
if r.lexer.absIntVal == maxValidValue:
|
|
|
|
# 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)
|
|
|
|
else:
|
|
|
|
value = -TargetType(r.lexer.absIntVal)
|
|
|
|
else:
|
|
|
|
r.raiseIntOverflow r.lexer.absIntVal, true
|
|
|
|
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:
|
|
|
|
r.skipToken tkBracketLe
|
|
|
|
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()
|
|
|
|
r.skipToken tkBracketRi
|
|
|
|
|
|
|
|
elif value is array:
|
|
|
|
r.skipToken tkBracketLe
|
|
|
|
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])
|
2018-12-17 23:01:06 +00:00
|
|
|
r.skipToken tkComma
|
2019-03-20 01:17:41 +00:00
|
|
|
readValue(r, value[high(value)])
|
2018-12-17 23:01:06 +00:00
|
|
|
r.skipToken tkBracketRi
|
|
|
|
|
|
|
|
elif value is (object or tuple):
|
2019-05-28 15:42:18 +00:00
|
|
|
type T = type(value)
|
2018-12-17 23:01:06 +00:00
|
|
|
r.skipToken tkCurlyLe
|
|
|
|
|
2018-12-28 01:02:10 +00:00
|
|
|
when T.totalSerializedFields > 0:
|
|
|
|
let fields = T.fieldReadersTable(JsonReader)
|
|
|
|
var expectedFieldPos = 0
|
|
|
|
while r.lexer.tok == tkString:
|
2019-08-12 14:53:04 +00:00
|
|
|
when T is tuple:
|
|
|
|
var reader = fields[][expectedFieldPos].reader
|
|
|
|
expectedFieldPos += 1
|
|
|
|
else:
|
|
|
|
var reader = findFieldReader(fields[], r.lexer.strVal, expectedFieldPos)
|
2018-12-17 23:01:06 +00:00
|
|
|
r.lexer.next()
|
2018-12-28 01:02:10 +00:00
|
|
|
r.skipToken tkColon
|
|
|
|
if reader != nil:
|
|
|
|
reader(value, r)
|
2020-07-17 20:58:02 +00:00
|
|
|
elif r.allowUnknownFields:
|
2020-06-30 19:11:56 +00:00
|
|
|
r.skipSingleJsValue()
|
2018-12-28 01:02:10 +00:00
|
|
|
else:
|
|
|
|
const typeName = typetraits.name(T)
|
2019-01-21 17:40:14 +00:00
|
|
|
r.raiseUnexpectedField(r.lexer.strVal, typeName)
|
2018-12-28 01:02:10 +00:00
|
|
|
if r.lexer.tok == tkComma:
|
|
|
|
r.lexer.next()
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2018-12-17 23:01:06 +00:00
|
|
|
r.skipToken tkCurlyRi
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
iterator readObjectFields*(r: var JsonReader): string =
|
|
|
|
for key in readObjectFields(r, string):
|
|
|
|
yield key
|
|
|
|
|