Skip to content

Commit

Permalink
-d:nimPreviewJsonutilsHoleyEnum becomes the default
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Oct 3, 2024
1 parent d6a71a1 commit c64e45d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

## Changes affecting backward compatibility

- `-d:nimPreviewJsonutilsHoleyEnum` becomes the default, `jsonutils` now can serialize/deserialize
holey enums as regular enums (via `ord`) instead of as strings.

## Standard library additions and changes

Expand Down
15 changes: 6 additions & 9 deletions lib/std/jsonutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,8 @@ proc jsonTo*(b: JsonNode, T: typedesc, opt = Joptions()): T =
proc toJson*[T](a: T, opt = initToJsonOptions()): JsonNode =
## serializes `a` to json; uses `toJsonHook(a: T)` if it's in scope to
## customize serialization, see strtabs.toJsonHook for an example.
##
## .. note:: With `-d:nimPreviewJsonutilsHoleyEnum`, `toJson` now can
## serialize/deserialize holey enums as regular enums (via `ord`) instead of as strings.
## It is expected that this behavior becomes the new default in upcoming versions.
## `toJson` serialize/deserialize holey enums as regular enums (via `ord`)
## instead of as strings.
when compiles(toJsonHook(a, opt)): result = toJsonHook(a, opt)
elif compiles(toJsonHook(a)): result = toJsonHook(a)
elif T is object | tuple:
Expand Down Expand Up @@ -333,14 +331,13 @@ proc toJson*[T](a: T, opt = initToJsonOptions()): JsonNode =
elif T is enum:
case opt.enumMode
of joptEnumOrd:
when T is Ordinal or defined(nimPreviewJsonutilsHoleyEnum): %(a.ord)
else: toJson($a, opt)
result = %(a.ord)
of joptEnumSymbol:
when T is OrdinalEnum:
toJson(symbolName(a), opt)
result = toJson(symbolName(a), opt)
else:
toJson($a, opt)
of joptEnumString: toJson($a, opt)
result = toJson($a, opt)
of joptEnumString: result = toJson($a, opt)
elif T is Ordinal: result = %(a.ord)
elif T is cstring: (if a == nil: result = newJNull() else: result = % $a)
else: result = %a
Expand Down
1 change: 0 additions & 1 deletion tests/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ switch("define", "nimExperimentalLinenoiseExtra")
# preview APIs are expected to be the new default in upcoming versions
switch("define", "nimPreviewFloatRoundtrip")
#switch("define", "nimPreviewDotLikeOps") # deprecated?
switch("define", "nimPreviewJsonutilsHoleyEnum")
switch("define", "nimPreviewHashRef")
switch("define", "nimPreviewRangeDefault")
switch("define", "nimPreviewNonVarDestructor")
Expand Down

0 comments on commit c64e45d

Please sign in to comment.