So what you're asking for is a way to customize the existing omitempty. Certainly seems feasible. This should be a fully fledged proposal, though - see https://github.com/golang/proposal.
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.
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.
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: