jsonutils: add customization for toJson via ToJsonOptions; generalize symbolName; add symbolRank (#18029)
* jsonutils: add customization for toJson via `ToJsonOptions` * add enumutils.symbolRank * lookup table implementation for HoleyEnum * cleanup * changelog * fixup * Update lib/std/jsonutils.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
parent
6e0fe965da
commit
7f077a76fe
4 changed files with 112 additions and 14 deletions
|
|
@ -35,6 +35,13 @@ type Foo = ref object
|
|||
proc `==`(a, b: Foo): bool =
|
||||
a.id == b.id
|
||||
|
||||
type MyEnum = enum me0, me1 = "me1Alt", me2, me3, me4
|
||||
|
||||
proc `$`(a: MyEnum): string =
|
||||
# putting this here pending https://github.com/nim-lang/Nim/issues/13747
|
||||
if a == me2: "me2Modif"
|
||||
else: system.`$`(a)
|
||||
|
||||
template fn() =
|
||||
block: # toJson, jsonTo
|
||||
type Foo = distinct float
|
||||
|
|
@ -83,6 +90,12 @@ template fn() =
|
|||
doAssert b2.ord == 1 # explains the `1`
|
||||
testRoundtrip(a): """[1,2,3]"""
|
||||
|
||||
block: # ToJsonOptions
|
||||
let a = (me1, me2)
|
||||
doAssert $a.toJson() == "[1,2]"
|
||||
doAssert $a.toJson(ToJsonOptions(enumMode: joptEnumSymbol)) == """["me1","me2"]"""
|
||||
doAssert $a.toJson(ToJsonOptions(enumMode: joptEnumString)) == """["me1Alt","me2Modif"]"""
|
||||
|
||||
block: # set
|
||||
type Foo = enum f1, f2, f3, f4, f5
|
||||
type Goo = enum g1 = 10, g2 = 15, g3 = 17, g4
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue