-
-
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
Improve JSON serialisation of strtabs #14549
Conversation
This creates a more compact serialisation of strtabs that is more in line with the normal tables.
@@ -353,6 +353,14 @@ proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} = | |||
assert(obj.kind == JObject) | |||
obj.fields[key] = val | |||
|
|||
proc `%`*(table: StringTableRef): JsonNode = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be done inside strtabs
itself? The module can make use of external %
implementation afaik (and if it doesn't, we can make it do so)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well the json module already contains the implementation for Table
and OrderedTable
so it just felt more at home along with them. But I don't object to moving it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it does save us from having to export the mode
field :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but at least this field should be readable through a template anyways..
json.nim should use some concept like |
Not sure that would work as strtabs still needs to store its mode.. |
Then maybe it should be a |
Well they are serialiseable as they are now, aren't most things serialiseable? The issue here was just that strtabs stored the internal sequence of data which meant a lot of empty fields. Just have a look at one of the old logs (warning, takes a while to load). Most of that log is just |
Sure, my major gripe is a json.nim that imports all the stdlib collections, there should be a better way. At least it's better than everything depending on json though... |
Isn't just implementing all of these functions in |
By that logic every module would end up depending on the JSON module, which isn't great. Is there a way to check if the importer imports another module as well? |
|
Not sure how feasable it is, but I believe the best option is a pragma that conditionally checks if the importer has imported another module, and if so then define procedures that have this module available to them. This would mean the code could be moved into strtabs and look a bit something like this: proc `%`*(table: StringTableRef): JsonNode {.withImport: json.} =
## Generic constructor for JSON data. Creates a new ``JObject JsonNode``.
result = newJObject()
result["mode"] = %($table.mode)
var data = newJObject()
for k, v in table: data[k] = %v
result["data"] = data |
that's now fixed see #14563 (comment) (and again, with #11992 you'd pass a single symbol instead of having to pass 3 procs) |
Would it be possible to merge this for now so that strtabs could be serialised better, and then create an RFC where we can discuss the various ways of solving the |
Ok. |
after this merge, merely importing json results in the following extra modules:
I don't think that's good, given that
|
@timotheecour but we have dead code elimination anyway, so I don't see it being that bad |
Yes, the extra imports are bad, no-one is arguing the opposite (except Yardanico it seems :P). But this should be discussed elsewhere and implemented properly, however this has little bearing on One reason it's bad: if you want to bundle NimScript into your program and want to create a custom stdlib you'd have to rip things out of files in order to bring the size down. It's a small use-case, but in general I think it would be a good idea to keep the interconnectedness of libraries to a minimum. |
This reverts commit 7cb4ef2.
See what you think of my approach here: https://github.com/disruptek/jason |
@Yardanico
it still needs to be parsed and is not cheap, eg look at this: #14179 and it can lead to cyclic dependency issues |
This reverts commit 7cb4ef2.
This reverts commit 7cb4ef2.
…ependency on strtabs thanks to a hooking mechanism (#14563) * json custom serialization; application for strtabs * serialize using nesting * make toJson more feature complete * add since * Revert "Improve JSON serialisation of strtabs (#14549)" This reverts commit 7cb4ef2. * better approach via mixin * toJson, jsonTo * fix test * address comments * move to jsonutils * doc * cleanups * also test for js * also test for vm
This creates a more compact serialisation of strtabs that is more in
line with the normal tables.