-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: encoding: add mechanism to let users control which xml/json fields are omitted at runtime? #23304
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
Comments
Can you elaborate what you mean by "if we should omit"? Also note that there already is a |
I mean for struct types, we have the ability to decide how to encode them, using MarshalJSON suppose i have a type MyType
but for example suppose that i want to ignore this field if the value of MyType.value is "" i cannot omit a struct because go cannot decide if a struct is empty, What we need is a method to implement which tells go if this struct is empty and ignore it |
So what you're asking for is a way to customize the existing Without a proposal and all the details, this particular issue is likely to get closed. Non-trivial changes to the standard library need good reason to be made, so you have to make a compelling argument for them with any evidence you can get your hands on. |
Yes i agree it's a proposal and not an issue, I will try write the proposal and post it in the given link Thks |
I think there’s already an issue created so that fields will be omitted if custom JSON marshalers return (nil, nil) and omitempty is toggled. Although, I may be misremembering the exact issue. |
@ericlagergren after a bit of googling, I think that might be #4554. An |
There are several issues on this topic and that’s one of them, yeah. If you search for ‘omitempty’ there’s a bunch of them.
… On Jan 2, 2018, at 10:15 AM, Daniel Martí ***@***.***> wrote:
@ericlagergren after a bit of googling, I think that might be #4554. An IsZero was discussed there, but the issue was closed automatically after years of inactivity.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Here's the CL I was thinking about: https://go-review.googlesource.com/#/c/23088/ |
I know there is several issues related to this topic, and there is no solution as far as i know. the issue is clear, we can add the attribute "omitempty", but struct fields are never empty. it is unfortunate that we have this in the standard library
we can customise the encoding, |
The encoding/json and encoding/xml packages cannot be all things to all people. If you need custom functionality, please feel free to copy them and modify to suit. We already have compile-time control of which fields are encoded. I don't believe we are going to add run-time control as well. Note that for any field you can change type T to *T and mark it omitempty and then at run-time choose whether to put a nil or an allocated value there. So in some sense they already support what you're asking for. |
We have the method MarshalJSON() which we can add to our objects in order to customise the serialisation, but there is no why to decide dynamically if we should omit.
How about adding IsValid as another method and lets have more control on the serialisation output,
People are trying to invent non-intuitive solutions like convert struct to map in order to counter this.
same goes for xml marshaler
The text was updated successfully, but these errors were encountered: