-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: encoding/json: allow defining which fields will be marshalled #45812
Comments
Can you write in this issue exactly what |
I added more information to the Solution Proposal part |
/cc @dsnet |
For prior art, this seems to have overlap with the |
Related #23304 |
How does this feature interoperate with the fact that |
I added another interface
I didn't consider the decoding in my module, but I think the custom Marshal/Unmarshaler interface should be used and ignore the defined values. |
Plumbing down options is a known problem in Even if the problem of plumbing down options was solved, there's the fundamental problem that most implementations will not support some of type of field mask, which makes support for this feature spotty at best. If the desire is to have semantically correct field mask, then it seems that a better solution is to pre-process JSON input when unmarshaling and post-process JSON output when marshaling to mask out certain fields. It won't be as efficient, but would be guaranteed to be correct. However, if we're willing to accept that this is a pre-process or post-process step, it doesn't seem like this is something that the standard |
cc @mvdan |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Similar issues: #22480, #11939, #45669
The idea of this proposal is to allow defining which columns will be marshal. It would be useful in many situations where we want to reduce the data size like HTTP responses, or marshalling to cache JSON values.
Currently, with the Standard library when we have something like this, the inner Structures are marshal into empty values.
https://play.golang.org/p/UVjAKNTMqW9
In the result
"a"
holds an object with empty values and there are case where thi.Solution Proposal
I copied the code from the standard library and did some modifications to allow this. https://github.com/jtorz/jsont
In my module I added the function
func MarshalFields(v interface{}, whitelist F) ([]byte, error)
.MarshalFields receives a
type F map[string]F
which works as a whitelist that holds the JSON keys that will be marshal if the key exists in the map, no matter the value, the field is added.In the next example only the
"name"
is added to the JSON, all other fields are ignored.https://play.golang.org/p/ghG6SDyXNLU
In this Example
Cases
I considered different scenarios like slices, nested structures, or self-referential structures.
The following cases will use the same structs definitions.
self-referential Slice (Recursive)
OUTPUT:
self-referential Slice (Non Recursive)
OUTPUT:
Nested Struct (Whole structure)
OUTPUT:
Nested Struct (Specific fields)
OUTPUT:
The text was updated successfully, but these errors were encountered: