Module marshal

This module contains procs for serialization and deseralization of arbitrary Nim data structures. The serialization format uses JSON. Warning: The serialization format could change in future!

Restriction: For objects their type is not serialized. This means essentially that it does not work if the object has some other runtime type than its compiletime type:

  A = object of RootObj
  B = object of A
    f: int

  a: ref A
  b: ref B

a = b
echo($$a[]) # produces "{}", not "{f: 0}"

# unmarshal
let c = to[B]("""{"f": 2}""")

# marshal
let s = $$c
Note: The to and $$ operations are available at compile-time!


streams, typeinfo, json, intsets, tables, unicode


proc load[T](s: Stream; data: var T)
loads data from the stream s. Raises EIO in case of an error.
proc store[T](s: Stream; data: T)
stores data into the stream s. Raises EIO in case of an error.
proc `$$`[T](x: T): string

returns a string representation of x.

Note: to serialize x to JSON use $(%x) from the json module

proc to[T](data: string): T
reads data and transforms it to a T.


  Foo = object
    id: int
    bar: string

let x = Foo(id: 1, bar: "baz")
let y = ($$x)
let z = to[Foo](y)

© 2006–2018 Andreas Rumpf
Licensed under the MIT License.