Serialization for sets
This commit is contained in:
parent
f349761b76
commit
7922a83c4d
|
@ -108,6 +108,19 @@ proc allocPtr[T](p: var ptr T) =
|
|||
proc allocPtr[T](p: var ref T) =
|
||||
p = new(T)
|
||||
|
||||
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
|
||||
|
||||
proc readValue*(r: var JsonReader, value: var auto) =
|
||||
mixin readValue
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
import stew/shims/sets, ../../json_serialization/[reader, writer, lexer]
|
||||
export sets
|
||||
|
||||
type
|
||||
SetType = OrderedSet | HashSet | set
|
||||
|
||||
proc writeValue*(writer: var JsonWriter, value: SetType) =
|
||||
writer.writeIterable value
|
||||
|
||||
proc readValue*(reader: var JsonReader, value: var SetType) =
|
||||
type ElemType = type(value.items)
|
||||
value = init SetType
|
||||
for elem in readArray(reader, ElemType):
|
||||
value.incl elem
|
||||
|
|
@ -92,7 +92,7 @@ template endRecordField*(w: var JsonWriter) =
|
|||
endRecord(w)
|
||||
w.state = AfterField
|
||||
|
||||
proc writeArray[T](w: var JsonWriter, elements: openarray[T]) =
|
||||
proc writeIterable*(w: var JsonWriter, collection: auto) =
|
||||
mixin writeValue
|
||||
|
||||
append '['
|
||||
|
@ -102,8 +102,9 @@ proc writeArray[T](w: var JsonWriter, elements: openarray[T]) =
|
|||
w.nestingLevel += 2
|
||||
indent()
|
||||
|
||||
for i, e in elements:
|
||||
if i != 0:
|
||||
var first = true
|
||||
for e in collection:
|
||||
if not first:
|
||||
append ','
|
||||
if w.hasPrettyOutput:
|
||||
append '\n'
|
||||
|
@ -111,6 +112,7 @@ proc writeArray[T](w: var JsonWriter, elements: openarray[T]) =
|
|||
|
||||
w.state = RecordExpected
|
||||
w.writeValue(e)
|
||||
first = false
|
||||
|
||||
if w.hasPrettyOutput:
|
||||
append '\n'
|
||||
|
@ -119,6 +121,9 @@ proc writeArray[T](w: var JsonWriter, elements: openarray[T]) =
|
|||
|
||||
append ']'
|
||||
|
||||
proc writeArray[T](w: var JsonWriter, elements: openarray[T]) =
|
||||
writeIterable(w, elements)
|
||||
|
||||
proc writeValue*(w: var JsonWriter, value: auto) =
|
||||
template addChar(c) =
|
||||
append c
|
||||
|
|
|
@ -2,7 +2,7 @@ import
|
|||
strutils, unittest,
|
||||
serialization/testing/generic_suite,
|
||||
../json_serialization, ./utils,
|
||||
../json_serialization/std/options
|
||||
../json_serialization/std/[options, sets]
|
||||
|
||||
type
|
||||
Meter = distinct int
|
||||
|
@ -138,3 +138,14 @@ suite "toJson tests":
|
|||
Json.roundtripTest h1, """{"r":null,"o":{"x":1,"y":"2","distance":3}}"""
|
||||
Json.roundtripTest h2, """{"r":{"x":1,"y":"2","distance":3},"o":null}"""
|
||||
|
||||
test "Set types":
|
||||
type HoldsSet = object
|
||||
a: int
|
||||
s: HashSet[string]
|
||||
|
||||
var s1 = toSet([1, 2, 3, 1, 4, 2])
|
||||
var s2 = HoldsSet(a: 100, s: toSet(["a", "b", "c"]))
|
||||
|
||||
Json.roundtripTest s1
|
||||
Json.roundtripTest s2
|
||||
|
||||
|
|
Loading…
Reference in New Issue