Skip to content

Commit 7f077a7

Browse files
timotheecourAraq
andauthored
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>
1 parent 6e0fe96 commit 7f077a7

File tree

4 files changed

+112
-14
lines changed

4 files changed

+112
-14
lines changed

changelog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@
118118
- `json.%`,`json.to`, `jsonutils.formJson`,`jsonutils.toJson` now work with `uint|uint64`
119119
instead of raising (as in 1.4) or giving wrong results (as in 1.2).
120120

121+
- `jsonutils` now handles `cstring` (including as Table key), and `set`.
122+
121123
- added `jsonutils.jsonTo` overload with `opt = Joptions()` param.
122124

123-
- `jsonutils` now handles `cstring` (including as Table key), and `set`.
125+
- `jsonutils.toJson` now supports customization via `ToJsonOptions`.
124126

125127
- Added an overload for the `collect` macro that inferes the container type based
126128
on the syntax of the last expression. Works with std seqs, tables and sets.
@@ -138,6 +140,7 @@
138140
- Added `std/enumutils` module. Added `genEnumCaseStmt` macro that generates case statement to parse string to enum.
139141
Added `items` for enums with holes.
140142
Added `symbolName` to return the enum symbol name ignoring the human readable name.
143+
Added `symbolRank` to return the index in which an enum member is listed in an enum.
141144

142145
- Added `typetraits.HoleyEnum` for enums with holes, `OrdinalEnum` for enums without holes.
143146

lib/std/enumutils.nim

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,67 @@ iterator items*[T: HoleyEnum](E: typedesc[T]): T =
8686
assert B[float].toSeq == [B[float].b0, B[float].b1]
8787
for a in enumFullRange(E): yield a
8888

89-
func symbolName*[T: OrdinalEnum](a: T): string =
89+
func span(T: typedesc[HoleyEnum]): int =
90+
(T.high.ord - T.low.ord) + 1
91+
92+
const invalidSlot = uint8.high
93+
94+
proc genLookup[T: typedesc[HoleyEnum]](_: T): auto =
95+
const n = span(T)
96+
var ret: array[n, uint8]
97+
var i = 0
98+
assert n <= invalidSlot.int
99+
for ai in mitems(ret): ai = invalidSlot
100+
for ai in items(T):
101+
ret[ai.ord - T.low.ord] = uint8(i)
102+
inc(i)
103+
return ret
104+
105+
func symbolRankImpl[T](a: T): int {.inline.} =
106+
const n = T.span
107+
const thres = 255 # must be <= `invalidSlot`, but this should be tuned.
108+
when n <= thres:
109+
const lookup = genLookup(T)
110+
let lookup2 {.global.} = lookup # xxx improve pending https://github.com/timotheecour/Nim/issues/553
111+
#[
112+
This could be optimized using a hash adapted to `T` (possible since it's known at CT)
113+
to get better key distribution before indexing into the lookup table table.
114+
]#
115+
{.noSideEffect.}: # because it's immutable
116+
let ret = lookup2[ord(a) - T.low.ord]
117+
if ret != invalidSlot: return ret.int
118+
else:
119+
var i = 0
120+
# we could also generate a case statement as optimization
121+
for ai in items(T):
122+
if ai == a: return i
123+
inc(i)
124+
raise newException(IndexDefect, $ord(a) & " invalid for " & $T)
125+
126+
template symbolRank*[T: enum](a: T): int =
127+
## Returns the index in which `a` is listed in `T`.
128+
##
129+
## The cost for a `HoleyEnum` is implementation defined, currently optimized
130+
## for small enums, otherwise is `O(T.enumLen)`.
131+
runnableExamples:
132+
type
133+
A = enum a0 = -3, a1 = 10, a2, a3 = (20, "f3Alt") # HoleyEnum
134+
B = enum b0, b1, b2 # OrdinalEnum
135+
C = enum c0 = 10, c1, c2 # OrdinalEnum
136+
assert a2.symbolRank == 2
137+
assert b2.symbolRank == 2
138+
assert c2.symbolRank == 2
139+
assert c2.ord == 12
140+
assert a2.ord == 11
141+
var invalid = 7.A
142+
doAssertRaises(IndexDefect): discard invalid.symbolRank
143+
when T is Ordinal: ord(a) - T.low.ord.static
144+
else: symbolRankImpl(a)
145+
146+
func symbolName*[T: enum](a: T): string =
90147
## Returns the symbol name of an enum.
148+
##
149+
## This uses `symbolRank`.
91150
runnableExamples:
92151
type B = enum
93152
b0 = (10, "kb0")
@@ -97,5 +156,7 @@ func symbolName*[T: OrdinalEnum](a: T): string =
97156
assert b.symbolName == "b0"
98157
assert $b == "kb0"
99158
static: assert B.high.symbolName == "b2"
159+
type C = enum c0 = -3, c1 = 4, c2 = 20 # HoleyEnum
160+
assert c1.symbolName == "c1"
100161
const names = enumNames(T)
101-
names[a.ord - T.low.ord]
162+
names[a.symbolRank]

lib/std/jsonutils.nim

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ add a way to customize serialization, for e.g.:
3131
]#
3232

3333
import macros
34+
from enumutils import symbolName
35+
from typetraits import OrdinalEnum
3436

3537
type
36-
Joptions* = object
38+
Joptions* = object # xxx rename FromJsonOptions
3739
## Options controlling the behavior of `fromJson`.
3840
allowExtraKeys*: bool
3941
## If `true` Nim's object to which the JSON is parsed is not required to
@@ -42,6 +44,17 @@ type
4244
## If `true` Nim's object to which JSON is parsed is allowed to have
4345
## fields without corresponding JSON keys.
4446
# in future work: a key rename could be added
47+
EnumMode* = enum
48+
joptEnumOrd
49+
joptEnumSymbol
50+
joptEnumString
51+
ToJsonOptions* = object
52+
enumMode*: EnumMode
53+
# xxx charMode
54+
55+
proc initToJsonOptions*(): ToJsonOptions =
56+
## initializes `ToJsonOptions` with sane options.
57+
ToJsonOptions(enumMode: joptEnumOrd)
4558

4659
proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
4760
proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
@@ -261,33 +274,41 @@ proc jsonTo*(b: JsonNode, T: typedesc, opt = Joptions()): T =
261274
## reverse of `toJson`
262275
fromJson(result, b, opt)
263276

264-
proc toJson*[T](a: T): JsonNode =
277+
proc toJson*[T](a: T, opt = initToJsonOptions()): JsonNode =
265278
## serializes `a` to json; uses `toJsonHook(a: T)` if it's in scope to
266279
## customize serialization, see strtabs.toJsonHook for an example.
267280
when compiles(toJsonHook(a)): result = toJsonHook(a)
268281
elif T is object | tuple:
269282
when T is object or isNamedTuple(T):
270283
result = newJObject()
271-
for k, v in a.fieldPairs: result[k] = toJson(v)
284+
for k, v in a.fieldPairs: result[k] = toJson(v, opt)
272285
else:
273286
result = newJArray()
274-
for v in a.fields: result.add toJson(v)
287+
for v in a.fields: result.add toJson(v, opt)
275288
elif T is ref | ptr:
276289
if system.`==`(a, nil): result = newJNull()
277-
else: result = toJson(a[])
290+
else: result = toJson(a[], opt)
278291
elif T is array | seq | set:
279292
result = newJArray()
280-
for ai in a: result.add toJson(ai)
281-
elif T is pointer: result = toJson(cast[int](a))
293+
for ai in a: result.add toJson(ai, opt)
294+
elif T is pointer: result = toJson(cast[int](a), opt)
282295
# edge case: `a == nil` could've also led to `newJNull()`, but this results
283296
# in simpler code for `toJson` and `fromJson`.
284-
elif T is distinct: result = toJson(a.distinctBase)
297+
elif T is distinct: result = toJson(a.distinctBase, opt)
285298
elif T is bool: result = %(a)
286299
elif T is SomeInteger: result = %a
287-
elif T is Ordinal: result = %(a.ord)
288300
elif T is enum:
289-
when defined(nimLegacyJsonutilsHoleyEnum): result = %a
290-
else: result = %(a.ord)
301+
case opt.enumMode
302+
of joptEnumOrd:
303+
when T is Ordinal or not defined(nimLegacyJsonutilsHoleyEnum): %(a.ord)
304+
else: toJson($a, opt)
305+
of joptEnumSymbol:
306+
when T is OrdinalEnum:
307+
toJson(symbolName(a), opt)
308+
else:
309+
toJson($a, opt)
310+
of joptEnumString: toJson($a, opt)
311+
elif T is Ordinal: result = %(a.ord)
291312
elif T is cstring: (if a == nil: result = newJNull() else: result = % $a)
292313
else: result = %a
293314

tests/stdlib/tjsonutils.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ type Foo = ref object
3535
proc `==`(a, b: Foo): bool =
3636
a.id == b.id
3737

38+
type MyEnum = enum me0, me1 = "me1Alt", me2, me3, me4
39+
40+
proc `$`(a: MyEnum): string =
41+
# putting this here pending https://github.com/nim-lang/Nim/issues/13747
42+
if a == me2: "me2Modif"
43+
else: system.`$`(a)
44+
3845
template fn() =
3946
block: # toJson, jsonTo
4047
type Foo = distinct float
@@ -83,6 +90,12 @@ template fn() =
8390
doAssert b2.ord == 1 # explains the `1`
8491
testRoundtrip(a): """[1,2,3]"""
8592

93+
block: # ToJsonOptions
94+
let a = (me1, me2)
95+
doAssert $a.toJson() == "[1,2]"
96+
doAssert $a.toJson(ToJsonOptions(enumMode: joptEnumSymbol)) == """["me1","me2"]"""
97+
doAssert $a.toJson(ToJsonOptions(enumMode: joptEnumString)) == """["me1Alt","me2Modif"]"""
98+
8699
block: # set
87100
type Foo = enum f1, f2, f3, f4, f5
88101
type Goo = enum g1 = 10, g2 = 15, g3 = 17, g4

0 commit comments

Comments
 (0)