-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some enhancements to jsonutils.nim
#15133
Conversation
great, that was on my roadmap, thanks for tackling this.
so I'm suggesting this API, which is easier to extend: type Joptions* = ref object
allowMissingFields*: bool
proc initJoptions(): Joptions = ...
proc fromJson*[T](a: var T, b: JsonNode, opt = initJoptions()) = ... EDIT: see rest of discussions, this is what we ended up with (object instead of ref object): type Joptions* = object
allowMissingFields*: bool
proc initJoptions(): Joptions = ...
proc fromJson*[T](a: var T, b: JsonNode, opt = initJoptions()) = ... |
Why not at least a |
making it here's the modified API with object instead of ref object (but no type Joptions* = object
allowMissingFields*: bool
proc initJoptions(): Joptions = ...
proc fromJson*[T](a: var T, b: JsonNode, opt = initJoptions()) = ... |
Even better then. |
one final note: if you prefer you can split your PR in 2, to move the |
fe681f3
to
57d8fe2
Compare
57d8fe2
to
6bf4fd4
Compare
cbf1af4
to
d7813b6
Compare
d7813b6
to
335da1a
Compare
bf4ed42
to
cca3ba7
Compare
I also implemented the serialization and deserialization of |
cca3ba7
to
ac1752c
Compare
ac1752c
to
c20eea6
Compare
Hm. I just tested that my code is not working with nested variant objects. |
The version I approved was before the commit In particular, This negates the whole point of the json hooks I had introduced in jsonutils (#14563), and prevents using jsonFrom, jsonTo in those modules due to cyclic dependency issues. Having toJsonHook/fromHsonHook in collections is arguably better, it just adds 2 symbols to collections but doesn't cause extra dependencies or prevent use of jsonutils in those modules. See also #15133 (comment) above and #14549 (comment), it looks like we're going in circles...
|
It sounds to me like @Araq & @timotheecour have conflicting visions of future directions here leading to said circularity. So, again there should be a How-To-Marshal-in-Nim RFC to get more exposure and again concrete use cases would help clarify these conflicting theoretical concerns. |
I'm not against an RFC but I thought the conclusion had already been reached that serialization should not import "the world":
Adding (recursive) dependency on os+times+the other modules (see above) IMO makes this PR non-mergeable; it was perfectly fine before the commit |
Well, I read #14549 (comment) as @PMunch and @Araq both also wanting an RFC about the broader issues. |
@timotheecour None of these modules import jsonutils, which recursive/cyclic dependencies are you talking about?
|
I think that it is not so important whether all collections will depend on JSON or there will be a module dedicated to collections serialization importing all collections or will be separate modules for JSON serialization for each collection. Everything I want is to have capabilities to serialize/de-serialize collections and to allow missing/extra keys. This functionality was already started in the way proposed by Timothee in Feel free to revert the e61da21 if you again decide the other way, but I find the current situation with two separate APIs
both of them not complete to be worse than not an ideal solution or even a completely missing solution. |
@Clyybber the problem is two-fold; after the commit
that commit doesn't create a cyclic dependency (that wouldn't even be possible) but prevents future imports which would create cyclic dependencies. these problems were nonexistant before this commit. |
@Araq, @timotheecour This is not a completely fair comparison because even today there is no notion for JSON in the entire C++ standard library. But from the other side, the most modern languages which have such support like C# for example have dedicated modules for serialization. Maybe simply the too generic name
The other variant which could be acceptable for both of you as it was mentioned before is to have a separate module for JSON serialization for each collection type like |
as I mentioned earlier I would be totally ok with that; modules are cheap. |
@Araq @timotheecour If Araq also approves it I will make it that way. |
how about: |
@timotheecour Ok but let us wait for Andreas to approve the design and the names of the modules because I already feel a little bored from this issue and I hope this to be the last time I have to change the things. |
FWIW, this approach with a separate "bridge" module (e.g. I've made it a bit easier for the users to import a single module by re-exporting the the bridged modules from there: import json_serialization/std/tables
var t = initTable[string, int]() |
As mentioned before, using a more general term ("marshal" since "serialization" is potentially confusing and different from the current "marshal" module: lose-lose) instead of "json" might be more forward looking..that is if this module hook design can support other marshalled formats (e.g. some 10+x faster binary one someday). If it can only support json for whatever reasons then "json" is fine, though that seems like an unfortunately limited design for the stdlib. |
@c-blake Except if we won't also have separate modules for separate marshaling formats e.g. |
Ok. In that case I would recommend a subdirectory |
Ok. I find the structure proposed by c-blake to be appropriate, but I also think that the current generic functionality in |
To be clear I don't mean deprecation of the entire |
I'm writing an RFC that describes in detail how this feature should be designed. Once we agreed on the RFC, we can implement it and deprecate what doesn't fit. |
RFC is here, nim-lang/RFCs#247 |
As this is a working backward compatible solution and currently I don't have time to reimplement it according to the RFC, is there any chance to merge this pull request as a temporary solution until the RFC is implemented? |
Ok, will review it once again. |
* Add some enhancements to `jsonutils.nim` * Use `jsonutils.nim` hookable API to add a possibility to deserialize JSON arrays directly to `HashSet` and `OrderedSet` types and respectively to serialize those types to JSON arrays. * Also add a possibility to deserialize JSON `null` objects to Nim option objects and respectively to serialize Nim option object to JSON object if some or to JSON `null` object if none. * Move serialization/deserialization functionality for `Table` and `OrderedTable` types from `jsonutils.nim` to `tables.nim` via the hookable API. * Add object `jsonutils.Joptions` and parameter from its type to `jsonutils.fromJson` procedure to control whether to allow deserializing JSON objects to Nim objects when the JSON has some extra or missing keys. * Add unit tests for the added functionalities to `tjsonutils.nim`. * improve fromJsonFields * Add changelog entry for the jsonutils enhancements * Add TODO in `jsonutils.nim` * Added an entry to "Future directions" section in `jsonutils.nim` as suggestion for future support of serialization and de-serialization of nested variant objects. * Added currently disabled test case in `tjsonutils.nim` for testing serialization and de-serialization of nested variant objects. * Move JSON hooks to `jsonutils.nim` Move `fromJsonHook` and `toJsonHook` procedures for different types to `jsonutils.nim` module to avoid a dependency of collections modules to the `json.nim` module. The hooks are removed from the following modules: * `tables.nim` * `sets.nim` * `options.nim` * `strtabs.nim` * Add some tests about `StringTableRef` Add tests for `StringTableRef`'s `fromJsonHook` and `toJsonHook` to `tjsonutils.nim`. * Disable a warning in `jsonutils.nim` Mark `fun` template in `jsonutils` module with `{.used.}` pragma in order to disable `[XDeclaredButNotUsed]` hint. The template is actually used by the `initCaseObject` macro in the same module. Co-authored-by: Timothee Cour <[email protected]>
Use
jsonutils.nim
hookable API to add a possibility to deserialize JSON arrays directly toHashSet
andOrderedSet
types and respectively to serialize those types to JSON arrays.Also add a possibility to deserialize JSON
null
objects to Nim option objects and respectively to serialize Nim option object to JSON object ifisSome
or to JSONnull
object ifisNone
.Move serialization/deserialization functionality for
Table
andOrderedTable
types fromjsonutils.nim
totables.nim
via the hookable API.Add object
jsonutils.Joptions
and parameter from its type tojsonutils.fromJson
procedure to control whether to allow deserializing JSON objects to Nim objects when the JSON has some extra or missing keys.Add unit tests for the added functionalities to
tjsonutils.nim
.