-
-
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
doNotSerialize
, jsonName
pragmas for JSON serialization closes #8104, #10718, also fixes #11415
#11416
Conversation
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.
We might wish to do some bikeshedding on the name. noSerialize
is fine, but I wonder if we should include "JSON" in the name and maybe come up with a more generic format for this.
Maybe {.json: NoExport.}
. That would also allow us to do things like: {.json: "CustomFieldName".}
in the future and keep consistency. Of course we can stick with noSerialize
for now and deprecate it in the future for something more generic.
lib/pure/json.nim
Outdated
proc `%`*[T: object](o: T): JsonNode = | ||
## Construct JsonNode from tuples and objects. | ||
## An object field annotated with the `{.noserialize.}` pragma will not appear |
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.
## An object field annotated with the `{.noserialize.}` pragma will not appear | |
## | |
## An object field annotated with the `{.noserialize.}` pragma will not appear |
RST rendering will not put a line break unless you include an empty line like I have done above
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.
Oh, thanks! I'm not very experienced with RST.
lib/pure/json.nim
Outdated
@@ -363,10 +392,25 @@ proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} = | |||
assert(obj.kind == JObject) | |||
obj.fields[key] = val | |||
|
|||
template noserialize*() {.pragma.} |
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.
If possible please add documentation to this as well.
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.
Will do!
lib/pure/json.nim
Outdated
## .. code-block:: nim | ||
## import json | ||
## let number = 10 | ||
## let numJson = % number |
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.
Nit: no space after %
. Please change this below too.
lib/pure/json.nim
Outdated
## | ||
## .. code-block:: nim | ||
## import json | ||
## let number = 10 |
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.
I would simplify this and remove this variable, simply use the literal below.
lib/pure/json.nim
Outdated
## doAssert numJson.kind == JInt | ||
## | ||
## For objects it may be desirable to avoid serialization of one or more object | ||
## fields. This can be achieved by using the ``{.noserialize.}`` pragma. |
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.
Let's use camelCase here (and everywhere): {.noSerialize.}
Sorry, I overlooked your comment. I'll address the comments later today. I have no strong feelings about the pragma name. Just picked the first thing that came to mind. Although I might actually prefer the |
keep in mind i can have a "NoExport" field in my json, and this might make a bit confusing |
@alehander42 got any better suggestions? |
Ok, I addressed the comments. For now I only changed the name to camelCase instead of changing it completely. I don't want to be the person making that decision. |
I like |
Don't mean to make things more complicated, just adding to the discussion: If If you still want to just use a name, you could still do something like |
In our work-in-progress generic serialization library, I've used a pragma called https://github.com/status-im/nim-serialization/ |
@zah |
763ad2a
to
012a217
Compare
I renamed |
doNotSerialize
, jsonName
pragmas for JSON serialization closes #8104, #10718, also fixes #11415
Adding the `{.noserialize.}` pragma to an object field means the given field will be ignored when serialized via the `%` proc.
I'm a little puzzled about this. With the change introduced in the previous commit to add the `noserialize` option for JSON, the first example in the `tjsonmacro.nim` does not work anymore. The generic `Point[float]` cannot be serialized, because of a `node is not a symbol` error. In order to get the type def implementation under the given case, we have to also get the implementation of the underlying type. Maybe this should be fixed in a different way?
Addresses the other comments of the PR too.
cbb1c45
to
7f56c77
Compare
Due to @Phillips126 comment in #10718, I saw Araq's comment about adding a Since this PR is related anyways I thought I'd attempt to add this as well. Due to the addition of the The implementation isn't very pretty unfortunately for the deserialization, because the whole deserialization logic works on the type node obtained via edit: Damn, it seems this implementation is too broken right now. I realized that it'd fail e.g. for |
With #12391 now merged, I'll close this PR and rewrite it. Will probably create a new PR once everything is working. |
Any thoughts about a new version of this PR? |
@disruptek Sorry, I kinda forgot about it. I'll try to take a stab at it in the next few days! |
I just remembered why I put this on ice. It's because I was waiting for #11526 to be merged. I just started to reimplement this. So, I'll wait for a bit until the above PR is hopefully merged soon. |
bump. this would be quite useful. |
@Vindaar Well yes, you need to modify the .to procedure, you can check my custom implementation which is based on that PR - https://github.com/Yardanico/telenim/blob/master/src/telenim/json_custom.nim (called toCustom here) |
@Yardanico Uhm, if you already have a working version of this around, feel free to create a PR for this! |
#11526 is now merged :) |
This PR adds the ability to exclude certain object fields from JSON serialization via
%
, by annotating the field with the{.noserialize.}
pragma.After discussion on IRC:
https://irclogs.nim-lang.org/05-06-2019.html#19:15:24
@dom96 asked me to create a PR for this.
While working on it I stumbled upon an issue with custom pragmas on variant objects: #11415.
However, to not break things, I also had to extend how
typDef
is determined incustomPragmaNode
(see 5da931f). This is a problem for the first test case intjsonmacro.nim
:https://github.com/nim-lang/Nim/blob/devel/tests/stdlib/tjsonmacro.nim#L9-L45
Line 45 will fail with
node is not a symbol
. I couldn't really reproduce this without the%
proc change, which is why I didn't open an issue for this as well.Update: add
jsonName
I also added a
jsonName
pragma, to customize the (de)serialization name of an object field.This pragma closes #10718.