nim-serialization/serialization.nim

92 lines
3.3 KiB
Nim
Raw Normal View History

import
2018-12-17 23:00:00 +00:00
faststreams, serialization/object_serialization
export
2018-12-17 23:00:00 +00:00
faststreams, object_serialization
2018-12-17 23:00:00 +00:00
template serializationFormatImpl(Name: untyped,
Reader, Writer, PreferedOutput: distinct type,
mimeTypeName: static string = "") {.dirty.} =
# This indirection is required in order to be able to generate the
# `mimeType` accessor template. Without the indirection, the template
# mechanism of Nim will try to expand the `mimeType` param in the position
# of the `mimeType` template name which will result in error.
type Name* = object
template ReaderType*(T: type Name): type = Reader
template WriterType*(T: type Name): type = Writer
template PreferedOutputType*(T: type Name): type = PreferedOutput
template mimeType*(T: type Name): string = mimeTypeName
template serializationFormat*(Name: untyped,
Reader, Writer, PreferedOutput: distinct type,
mimeType: static string = "") =
serializationFormatImpl(Name, Reader, Writer, PreferedOutput, mimeType)
proc encodeImpl(writer: var auto, value: auto) =
2018-11-29 01:33:45 +00:00
mixin writeValue, getOutput
2018-12-17 23:00:00 +00:00
writer.writeValue value
template encode*(Format: type, value: auto, params: varargs[untyped]): auto =
mixin init, WriterType, PreferedOutputType
2018-12-17 23:00:00 +00:00
var s = init MemoryOutputStream[PreferedOutputType(Format)]
# TODO:
# Remove this when statement once the following bug is fixed:
# https://github.com/nim-lang/Nim/issues/9996
when astToStr(params) != "":
var writer = init(WriterType(Format), addr s, params)
else:
var writer = init(WriterType(Format), addr s)
encodeImpl(writer, value)
s.getOutput
proc readValue*(reader: var auto, T: type): T =
mixin readValue
reader.readValue(result)
template decode*(Format: distinct type,
input: openarray[byte] | string,
RecordType: distinct type,
params: varargs[untyped]): auto =
mixin init, ReaderType
2018-12-17 23:00:00 +00:00
var stream = memoryStream(input)
# TODO:
# Remove this when statement once the following bug is fixed:
# https://github.com/nim-lang/Nim/issues/9996
when astToStr(params) != "":
var reader = init(ReaderType(Format), stream, params)
else:
var reader = init(ReaderType(Format), stream)
reader.readValue(RecordType)
2018-12-17 23:00:00 +00:00
template loadFile*(Format: distinct type,
filename: string,
RecordType: distinct type,
params: varargs[untyped]): auto =
mixin init, ReaderType
2018-12-17 23:00:00 +00:00
var stream = openFile(filename)
# TODO:
# Remove this when statement once the following bug is fixed:
# https://github.com/nim-lang/Nim/issues/9996
when astToStr(params) != "":
var reader = init(ReaderType(Format), stream, params)
else:
var reader = init(ReaderType(Format), stream)
reader.readValue(RecordType)
2018-12-17 23:00:00 +00:00
template loadFile*[RecordType](Format: type,
filename: string,
record: var RecordType,
params: varargs[untyped]) =
record = loadFile(Format, filename, RecordType, params)
2018-12-17 23:00:00 +00:00
template saveFile*(Format: type, filename: string, args: varargs[untyped]) =
# TODO: This should use a proper output stream, instead of calling `encode`
writeFile(filename, Format.encode(args))
2018-11-29 01:33:45 +00:00