2018-12-17 23:01:06 +00:00
|
|
|
import
|
2019-03-19 23:54:03 +00:00
|
|
|
strutils, typetraits, macros, strformat,
|
|
|
|
faststreams/input_stream, serialization/[object_serialization, errors],
|
2019-01-21 17:40:14 +00:00
|
|
|
types, lexer
|
|
|
|
|
|
|
|
export
|
2019-03-19 23:54:03 +00:00
|
|
|
types, errors
|
2018-12-17 23:01:06 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
JsonReader* = object
|
|
|
|
lexer: JsonLexer
|
|
|
|
|
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
|
|
|
|
|
2019-03-19 23:54:03 +00:00
|
|
|
CustomSerializationError* = object of JsonReaderError
|
2019-03-20 01:17:41 +00:00
|
|
|
deserializedField*: string
|
2019-03-19 23:54:03 +00:00
|
|
|
innerException*: ref CatchableError
|
|
|
|
|
2018-12-17 23:01:06 +00:00
|
|
|
ExpectedTokenCategory* = enum
|
|
|
|
etBool = "bool literal"
|
|
|
|
etInt = "integer"
|
|
|
|
etEnum = "enum value (int or string)"
|
|
|
|
etNumber = "number"
|
|
|
|
etString = "string"
|
|
|
|
etComma = "comma"
|
|
|
|
etBracketLe = "array start bracket"
|
|
|
|
etBracketRi = "array end bracker"
|
|
|
|
etCurrlyLe = "object start bracket"
|
|
|
|
etCurrlyRi = "object end bracket"
|
|
|
|
|
|
|
|
UnexpectedToken* = object of JsonReaderError
|
|
|
|
encountedToken*: TokKind
|
|
|
|
expectedToken*: ExpectedTokenCategory
|
|
|
|
|
2019-03-19 23:54:03 +00:00
|
|
|
method formatMsg*(err: ref JsonReaderError, filename: string): string =
|
|
|
|
fmt"{filename}({err.line}, {err.col}) Error while reading json file"
|
|
|
|
|
|
|
|
method formatMsg*(err: ref UnexpectedField, filename: string): string =
|
|
|
|
fmt"{filename}({err.line}, {err.col}) Unexpected field '{err.encounteredField}' while deserialing {err.deserializedType}"
|
|
|
|
|
|
|
|
method formatMsg*(err: ref UnexpectedToken, filename: string): string =
|
|
|
|
fmt"{filename}({err.line}, {err.col}) Unexpected token '{err.encountedToken}' in place of '{err.expectedToken}'"
|
|
|
|
|
|
|
|
method formatMsg*(err: ref CustomSerializationError, filename: string): string =
|
2019-03-29 12:49:47 +00:00
|
|
|
fmt"{filename}({err.line}, {err.col}) Custom serialization exception while deserializing '{err.deserializedField}': [{err.innerException.name}] {err.innerException.msg}"
|
2018-12-17 23:01:06 +00:00
|
|
|
|
2019-03-11 09:39:19 +00:00
|
|
|
template init*(T: type JsonReader, stream: ByteStreamVar, mode = defaultJsonMode): auto =
|
|
|
|
init JsonReader, AsciiStreamVar(stream), mode
|
2018-12-17 23:01:06 +00:00
|
|
|
|
|
|
|
proc assignLineNumber(ex: ref JsonReaderError, r: JsonReader) =
|
|
|
|
ex.line = r.lexer.line
|
|
|
|
ex.col = r.lexer.col
|
|
|
|
|
2019-01-21 17:40:14 +00:00
|
|
|
proc raiseUnexpectedToken(r: JsonReader, expected: ExpectedTokenCategory) =
|
2018-12-17 23:01:06 +00:00
|
|
|
var ex = new UnexpectedToken
|
|
|
|
ex.assignLineNumber(r)
|
|
|
|
ex.encountedToken = r.lexer.tok
|
|
|
|
ex.expectedToken = expected
|
|
|
|
raise ex
|
|
|
|
|
2019-01-21 17:40:14 +00:00
|
|
|
proc raiseUnexpectedField(r: JsonReader, fieldName, deserializedType: cstring) =
|
2018-12-17 23:01:06 +00:00
|
|
|
var ex = new UnexpectedField
|
|
|
|
ex.assignLineNumber(r)
|
|
|
|
ex.encounteredField = fieldName
|
|
|
|
ex.deserializedType = deserializedType
|
|
|
|
raise ex
|
|
|
|
|
2019-03-20 01:17:41 +00:00
|
|
|
proc readValueFailed*(r: JsonReader,
|
|
|
|
Record: type, fieldName: string, field: var auto,
|
|
|
|
err: ref CatchableError) =
|
2019-03-19 23:54:03 +00:00
|
|
|
var ex = new CustomSerializationError
|
|
|
|
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
|
|
|
|
|
|
|
|
proc init*(T: type JsonReader, stream: AsciiStreamVar, mode = defaultJsonMode): T =
|
|
|
|
result.lexer = JsonLexer.init(stream, mode)
|
|
|
|
result.lexer.next()
|
|
|
|
|
|
|
|
proc setParsed[T: enum](e: var T, s: string) =
|
|
|
|
e = parseEnum[T](s)
|
|
|
|
|
2019-01-21 17:40:14 +00:00
|
|
|
proc requireToken(r: JsonReader, tk: TokKind) =
|
|
|
|
if r.lexer.tok != tk:
|
|
|
|
r.raiseUnexpectedToken case tk
|
|
|
|
of tkString: etString
|
|
|
|
of tkInt: etInt
|
|
|
|
of tkComma: etComma
|
|
|
|
of tkBracketRi: etBracketRi
|
|
|
|
of tkBracketLe: etBracketLe
|
|
|
|
of tkCurlyRi: etCurrlyRi
|
|
|
|
of tkCurlyLe: etCurrlyLe
|
2019-03-13 23:39:10 +00:00
|
|
|
else: (doAssert false; etBool)
|
2019-01-21 17:40:14 +00:00
|
|
|
|
2018-12-17 23:01:06 +00:00
|
|
|
proc skipToken(r: var JsonReader, tk: TokKind) =
|
|
|
|
r.requireToken tk
|
|
|
|
r.lexer.next()
|
|
|
|
|
2019-03-19 23:54:03 +00:00
|
|
|
proc readValue*(r: var JsonReader, value: var auto) =
|
2018-12-17 23:01:06 +00:00
|
|
|
mixin readValue
|
|
|
|
|
|
|
|
let tok = r.lexer.tok
|
|
|
|
|
|
|
|
when value is string:
|
|
|
|
r.requireToken tkString
|
|
|
|
value = r.lexer.strVal
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
elif value is enum:
|
|
|
|
case tok
|
|
|
|
of tkString:
|
|
|
|
# TODO: don't proprage the `parseEnum` exception
|
|
|
|
value.setParsed(r.lexer.strVal)
|
|
|
|
of tkInt:
|
|
|
|
# TODO: validate that the value is in range
|
|
|
|
value = type(value)(r.lexer.intVal)
|
|
|
|
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)
|
|
|
|
r.requireToken tkInt
|
|
|
|
value = TargetType(r.lexer.intVal)
|
|
|
|
r.lexer.next()
|
|
|
|
|
|
|
|
elif value is SomeFloat:
|
|
|
|
case tok
|
|
|
|
of tkInt: value = float(r.lexer.intVal)
|
|
|
|
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:
|
|
|
|
let 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)
|
|
|
|
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:
|
|
|
|
const typeName = typetraits.name(value.type)
|
|
|
|
{.error: "Failed to convert to JSON an unsupported type: " & typeName.}
|